/** * {@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()); }
/** * {@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()); } }
/** * {@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()); }
/** * {@inheritdoc} * * @return Message */ public function deserialize($data, array $options = []) { /** @var Message $message */ $message = unserialize($data); Assertion::isInstanceOf($message, 'Gdbots\\Pbj\\Message'); return $message; }
/** * {@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()); }
/** * {@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()); }
/** * {@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()); }
/** * {@inheritdoc} */ public function guard($value, Field $field) { /** @var GeoPoint $value */ Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\GeoPoint', null, $field->getName()); }
/** * {@inheritdoc} */ public function guard($value, Field $field) { /** @var MessageRef $value */ Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\MessageRef', null, $field->getName()); }
/** * @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(); }
/** * {@inheritdoc} */ public function guard($value, Field $field) { /** @var Microtime $value */ Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Microtime', null, $field->getName()); }
/** * {@inheritdoc} */ public function guard($value, Field $field) { /** @var \DateTime $value */ Assertion::isInstanceOf($value, 'DateTime', null, $field->getName()); }
/** * @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(); }