/** * @param Schema $schema * @param SchemaId $resolvedSchemaId * @param string $resolvedClassName */ public function __construct(Schema $schema, SchemaId $resolvedSchemaId, $resolvedClassName) { $this->schema = $schema; $this->resolvedSchemaId = $resolvedSchemaId; $this->resolvedClassName = $resolvedClassName; parent::__construct(sprintf('Schema id [%s] with curie [%s] was resolved to [%s] but ' . 'that message has a curie of [%s]. They must match.', $this->resolvedSchemaId->toString(), $this->resolvedSchemaId->getCurieMajor(), $resolvedClassName, $this->schema->getId()->getCurieMajor())); }
/** * Convenience method to return the name of the method that should * exist to handle this message. * * For example, an ImportUserV1 message would be handled by: * SomeClass::importUserV1(ImportUserV1 $command) * * @param bool $withMajor * @return string */ public function getHandlerMethodName($withMajor = true) { if (true === $withMajor) { return lcfirst($this->classShortName); } return lcfirst(str_replace('V' . $this->id->getVersion()->getMajor(), '', $this->classShortName)); }
public function testResolveQName() { $schemaId = SchemaId::fromString('pbj:acme:blog:node:article:1-0-0'); MessageResolver::register($schemaId, 'Fake'); $curie = MessageResolver::resolveQName(SchemaQName::fromString('acme:article')); $this->assertSame($schemaId->getCurie(), $curie); }
/** * @param SchemaId $id * @return SchemaCurie */ public static function fromId(SchemaId $id) { $curie = substr(str_replace(':' . $id->getVersion()->toString(), '', $id->toString()), 4); if (isset(self::$instances[$curie])) { return self::$instances[$curie]; } self::$instances[$curie] = new self($id->getVendor(), $id->getPackage(), $id->getCategory(), $id->getMessage()); return self::$instances[$curie]; }
/** * Returns the fully qualified php class name to be used for the provided schema id. * * @param SchemaId $id * @return string * @throws NoMessageForSchemaId */ public static function resolveId(SchemaId $id) { $curieMajor = $id->getCurieMajor(); if (isset(self::$resolved[$curieMajor])) { return self::$resolved[$curieMajor]; } if (isset(self::$messages[$curieMajor])) { $className = self::$messages[$curieMajor]; self::$resolved[$curieMajor] = $className; return $className; } $curie = $id->getCurie()->toString(); if (isset(self::$messages[$curie])) { $className = self::$messages[$curie]; self::$resolved[$curieMajor] = $className; self::$resolved[$curie] = $className; return $className; } throw new NoMessageForSchemaId($id); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:pbjx:mixin:request:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:get-node-batch-response:1-0-0'); }
/** * @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 getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:index-node-batch:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:publishable:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:eme:accounts:mixin:account-ref:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:expire-node:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:eme:collector:mixin:collectable:1-0-0'); }
/** * @param SchemaId $id * @return SchemaQName */ public static function fromId(SchemaId $id) { return self::fromCurie($id->getCurie()); }
/** * @param SchemaId $schemaId */ public function __construct(SchemaId $schemaId) { $this->schemaId = $schemaId; parent::__construct(sprintf('MessageResolver is unable to resolve schema id [%s] ' . 'using curie [%s] to a class name.', $schemaId->toString(), $schemaId->getCurieMajor())); }
/** * @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(); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:get-node-request:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:enrichments:mixin:time-parting:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:ncr:mixin:node-scheduled:1-0-0'); }
/** * {@inheritdoc} */ public function getId() { return SchemaId::fromString('pbj:gdbots:enrichments:mixin:ip-to-geo:1-0-0'); }