コード例 #1
0
ファイル: SignedBigIntType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var BigNumber $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\BigNumber', null, $field->getName());
     Assertion::true($value->isGreaterThanOrEqualTo('-9223372036854775808'), sprintf('Field [%s] cannot be less than [-9223372036854775808].', $field->getName()), $field->getName());
     Assertion::true($value->isLessThanOrEqualTo('9223372036854775807'), sprintf('Field [%s] cannot be greater than [9223372036854775807].', $field->getName()), $field->getName());
 }
コード例 #2
0
ファイル: UuidType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\UuidIdentifier', null, $field->getName());
     if ($field->hasClassName()) {
         Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     }
 }
コード例 #3
0
ファイル: BigIntType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var BigNumber $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\BigNumber', null, $field->getName());
     Assertion::true(!$value->isNegative(), sprintf('Field [%s] cannot be negative.', $field->getName()), $field->getName());
     Assertion::true($value->isLessThanOrEqualTo('18446744073709551615'), sprintf('Field [%s] cannot be greater than [18446744073709551615].', $field->getName()), $field->getName());
 }
コード例 #4
0
ファイル: PhpSerializer.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  *
  * @return Message
  */
 public function deserialize($data, array $options = [])
 {
     /** @var Message $message */
     $message = unserialize($data);
     Assertion::isInstanceOf($message, 'Gdbots\\Pbj\\Message');
     return $message;
 }
コード例 #5
0
ファイル: IntEnumType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Enum $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Common\\Enum', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     Assertion::integer($value->getValue(), null, $field->getName());
     Assertion::range($value->getValue(), $this->getMin(), $this->getMax(), null, $field->getName());
 }
コード例 #6
0
ファイル: IdentifierType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Identifier $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Identifier', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     $v = $value->toString();
     //Assertion::string($v, null, $field->getName());
     // intentionally using strlen to get byte length, not mb_strlen
     $length = strlen($v);
     $maxBytes = $this->getMaxBytes();
     $okay = $length > 0 && $length <= $maxBytes;
     Assertion::true($okay, sprintf('Field [%s] must be between [1] and [%d] bytes, [%d] bytes given.', $field->getName(), $maxBytes, $length), $field->getName());
 }
コード例 #7
0
ファイル: MessageType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Message $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\Message', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     if (!$field->hasAnyOfClassNames()) {
         return;
     }
     $classNames = $field->getAnyOfClassNames();
     if (empty($classNames)) {
         // means it can be "any message"
         return;
     }
     foreach ($classNames as $className) {
         if ($value instanceof $className) {
             return;
         }
     }
     Assertion::true(false, sprintf('Field [%s] must be an instance of at least one of: %s.', $field->getName(), implode(',', $classNames)), $field->getName());
 }
コード例 #8
0
ファイル: GeoPointType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var GeoPoint $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\GeoPoint', null, $field->getName());
 }
コード例 #9
0
ファイル: MessageRefType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var MessageRef $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\MessageRef', null, $field->getName());
 }
コード例 #10
0
ファイル: PhpArraySerializer.php プロジェクト: gdbots/pbj-php
 /**
  * @param array $data
  * @return Message
  *
  * @throws \Exception
  * @throws GdbotsPbjException
  */
 private function doDeserialize(array $data)
 {
     $schemaId = SchemaId::fromString((string) $data[Schema::PBJ_FIELD_NAME]);
     $className = MessageResolver::resolveId($schemaId);
     /** @var Message $message */
     $message = new $className();
     Assertion::isInstanceOf($message, 'Gdbots\\Pbj\\Message');
     if ($message::schema()->getCurieMajor() !== $schemaId->getCurieMajor()) {
         throw new InvalidResolvedSchema($message::schema(), $schemaId, $className);
     }
     $schema = $message::schema();
     foreach ($data as $fieldName => $value) {
         if (!$schema->hasField($fieldName)) {
             continue;
         }
         if (null === $value) {
             $message->clear($fieldName);
             continue;
         }
         $field = $schema->getField($fieldName);
         $type = $field->getType();
         switch ($field->getRule()->getValue()) {
             case FieldRule::A_SINGLE_VALUE:
                 $message->set($fieldName, $type->decode($value, $field, $this));
                 break;
             case FieldRule::A_SET:
             case FieldRule::A_LIST:
                 Assertion::isArray($value, sprintf('Field [%s] must be an array.', $fieldName), $fieldName);
                 $values = [];
                 foreach ($value as $v) {
                     $values[] = $type->decode($v, $field, $this);
                 }
                 if ($field->isASet()) {
                     $message->addToSet($fieldName, $values);
                 } else {
                     $message->addToList($fieldName, $values);
                 }
                 break;
             case FieldRule::A_MAP:
                 Assertion::true(ArrayUtils::isAssoc($value), sprintf('Field [%s] must be an associative array.', $fieldName), $fieldName);
                 foreach ($value as $k => $v) {
                     $message->addToMap($fieldName, $k, $type->decode($v, $field, $this));
                 }
                 break;
             default:
                 break;
         }
     }
     return $message->set(Schema::PBJ_FIELD_NAME, $schema->getId()->toString())->populateDefaults();
 }
コード例 #11
0
ファイル: MicrotimeType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Microtime $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Microtime', null, $field->getName());
 }
コード例 #12
0
ファイル: DateTimeType.php プロジェクト: gdbots/pbj-php
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var \DateTime $value */
     Assertion::isInstanceOf($value, 'DateTime', null, $field->getName());
 }
コード例 #13
0
ファイル: ItemMarshaler.php プロジェクト: gdbots/pbj-php
 /**
  * @param array $data
  * @return Message
  *
  * @throws \Exception
  * @throws GdbotsPbjException
  */
 private function doUnmarshal(array $data)
 {
     Assertion::keyIsset($data['M'], Schema::PBJ_FIELD_NAME, sprintf('[%s::%s] Array provided must contain the [%s] key.', get_called_class(), __FUNCTION__, Schema::PBJ_FIELD_NAME));
     $schemaId = SchemaId::fromString((string) $data['M'][Schema::PBJ_FIELD_NAME]['S']);
     $className = MessageResolver::resolveId($schemaId);
     /** @var Message $message */
     $message = new $className();
     Assertion::isInstanceOf($message, 'Gdbots\\Pbj\\Message');
     if ($message::schema()->getCurieMajor() !== $schemaId->getCurieMajor()) {
         throw new InvalidResolvedSchema($message::schema(), $schemaId, $className);
     }
     $schema = $message::schema();
     foreach ($data['M'] as $fieldName => $dynamoValue) {
         if (!$schema->hasField($fieldName)) {
             continue;
         }
         $dynamoType = key($dynamoValue);
         $value = current($dynamoValue);
         if ('NULL' === $dynamoType) {
             $message->clear($fieldName);
             continue;
         }
         $field = $schema->getField($fieldName);
         $type = $field->getType();
         switch ($field->getRule()->getValue()) {
             case FieldRule::A_SINGLE_VALUE:
                 $message->set($fieldName, $type->decode($value, $field, $this));
                 break;
             case FieldRule::A_SET:
             case FieldRule::A_LIST:
                 $values = [];
                 if ('L' === $dynamoType) {
                     foreach ($value as $v) {
                         $values[] = $type->decode(isset($v['M']) ? $v['M'] : current($v), $field, $this);
                     }
                 } else {
                     foreach ($value as $v) {
                         $values[] = $type->decode($v, $field, $this);
                     }
                 }
                 if ($field->isASet()) {
                     $message->addToSet($fieldName, $values);
                 } else {
                     $message->addToList($fieldName, $values);
                 }
                 break;
             case FieldRule::A_MAP:
                 foreach ($value as $k => $v) {
                     $message->addToMap($fieldName, $k, $type->decode(current($v), $field, $this));
                 }
                 break;
             default:
                 break;
         }
     }
     return $message->set(Schema::PBJ_FIELD_NAME, $schema->getId()->toString())->populateDefaults();
 }