/** * @return Schema */ protected static function defineSchema() { $fields = []; /** @var T\Type $class */ foreach (self::getAllTypes() as $type => $class) { switch ($type) { case 'Identifier': $fields[] = Fb::create($type, $class::create())->asAMap()->className('Gdbots\\Pbj\\WellKnown\\TimeUuidIdentifier')->build(); break; case 'IntEnum': $fields[] = Fb::create($type, $class::create())->asAMap()->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\IntEnum')->build(); break; case 'StringEnum': $fields[] = Fb::create($type, $class::create())->asAMap()->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\StringEnum')->build(); break; case 'Message': $fields[] = Fb::create($type, $class::create())->asAMap()->className('Gdbots\\Tests\\Pbj\\Fixtures\\NestedMessage')->build(); break; default: $fields[] = Fb::create($type, $class::create())->asAMap()->build(); } } $schema = new Schema('pbj:gdbots:tests.pbj:fixtures:maps-message:1-0-0', __CLASS__, $fields); MessageResolver::registerSchema($schema); return $schema; }
public function testCanCreateAllMessages() { /** @var \Gdbots\Pbj\Message $className */ foreach (MessageResolver::all() as $curie => $className) { $message = $className::create(); $this->assertInstanceOf($className, $message); $this->assertInstanceOf('Gdbots\\Pbj\\Message', $message); } }
/** * @return Schema */ protected static function defineSchema() { $schema = new Schema('pbj:gdbots:tests.pbj:fixtures:email-message:1-0-0', __CLASS__, [Fb::create('id', T\TimeUuidType::create())->required()->build(), Fb::create('from_name', T\StringType::create())->build(), Fb::create('from_email', T\StringType::create())->required()->format('email')->build(), Fb::create('subject', T\StringType::create())->withDefault(function (EmailMessage $message = null) { if (!$message) { return null; } return implode(',', $message->get('labels', [])) . ' test'; })->build(), Fb::create('body', T\StringType::create())->build(), Fb::create('priority', T\IntEnumType::create())->required()->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\Priority')->withDefault(Priority::NORMAL)->build(), Fb::create('sent', T\BooleanType::create())->build(), Fb::create('date_sent', T\DateTimeType::create())->build(), Fb::create('microtime_sent', T\MicrotimeType::create())->build(), Fb::create('provider', T\StringEnumType::create())->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\Provider')->withDefault(Provider::GMAIL())->build(), Fb::create('labels', T\StringType::create())->format(Format::HASHTAG())->asASet()->build(), Fb::create('nested', T\MessageType::create())->className('Gdbots\\Tests\\Pbj\\Fixtures\\NestedMessage')->build(), Fb::create('enum_in_set', T\StringEnumType::create())->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\Provider')->asASet()->build(), Fb::create('enum_in_list', T\StringEnumType::create())->className('Gdbots\\Tests\\Pbj\\Fixtures\\Enum\\Provider')->asAList()->build(), Fb::create('any_of_message', T\MessageType::create())->className('Gdbots\\Pbj\\Message')->asAList()->build(), Fb::create('dynamic_fields', T\DynamicFieldType::create())->asAList()->build()]); MessageResolver::registerSchema($schema); return $schema; }
/** * @expectedException \Gdbots\Pbj\Exception\NoMessageForQName */ public function testResolveInvalidQName() { $curie = MessageResolver::resolveQName(SchemaQName::fromString('acme:video')); }
<?php /** * DO NOT EDIT THIS FILE as it will be overwritten by Pbj Compiler process. * * Registers all of the pbj schemas with the MessageResolver. * * This file has been auto-generated by the Pbj Compiler. */ \Gdbots\Pbj\MessageResolver::registerMap(['gdbots:pbjx:event:event-execution-failed' => 'Gdbots\\Schemas\\Pbjx\\Event\\EventExecutionFailedV1', 'gdbots:pbjx:request:request-failed-response' => 'Gdbots\\Schemas\\Pbjx\\Request\\RequestFailedResponseV1', 'gdbots:ncr:command:create-edge' => 'Gdbots\\Schemas\\Ncr\\Command\\CreateEdgeV1', 'gdbots:ncr:event:edge-created' => 'Gdbots\\Schemas\\Ncr\\Event\\EdgeCreatedV1', 'gdbots:ncr:command:delete-edge' => 'Gdbots\\Schemas\\Ncr\\Command\\DeleteEdgeV1', 'gdbots:ncr:event:edge-deleted' => 'Gdbots\\Schemas\\Ncr\\Event\\EdgeDeletedV1', 'gdbots:contexts::app' => 'Gdbots\\Schemas\\Contexts\\AppV1', 'gdbots:contexts::cloud' => 'Gdbots\\Schemas\\Contexts\\CloudV1', 'gdbots:contexts::user-agent' => 'Gdbots\\Schemas\\Contexts\\UserAgentV1', 'gdbots:geo::address' => 'Gdbots\\Schemas\\Geo\\AddressV1', 'gdbots:pbjx::envelope' => 'Gdbots\\Schemas\\Pbjx\\EnvelopeV1']);
/** * @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(); }
/** * @return Schema */ protected static function defineSchema() { $schema = new Schema('pbj:gdbots:tests.pbj:fixtures:nested-message:1-0-0', __CLASS__, [Fb::create('test1', T\StringType::create())->build(), Fb::create('test2', T\IntType::create())->asASet()->build(), Fb::create('location', T\GeoPointType::create())->build(), Fb::create('refs', T\MessageRefType::create())->asASet()->build()]); MessageResolver::registerSchema($schema); return $schema; }
/** * @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(); }
<?php /** * DO NOT EDIT THIS FILE as it will be overwritten by Pbj Compiler process. * * Registers all of the pbj schemas with the MessageResolver. * * This file has been auto-generated by the Pbj Compiler. */ \Gdbots\Pbj\MessageResolver::registerMap(['eme:accounts:node:account' => 'Eme\\Schemas\\Accounts\\Node\\AccountV1', 'eme:accounts:request:get-account-request' => 'Eme\\Schemas\\Accounts\\Request\\GetAccountRequestV1', 'eme:accounts:request:get-account-response' => 'Eme\\Schemas\\Accounts\\Request\\GetAccountResponseV1', 'eme:solicits:command:import-submission' => 'Eme\\Schemas\\Solicits\\Command\\ImportSubmissionV1', 'eme:solicits:command:send-submission' => 'Eme\\Schemas\\Solicits\\Command\\SendSubmissionV1', 'eme:solicits:edge:submissions' => 'Eme\\Schemas\\Solicits\\Edge\\SubmissionsV1', 'eme:solicits:event:submission-received' => 'Eme\\Schemas\\Solicits\\Event\\SubmissionReceivedV1', 'eme:solicits:node:solicit' => 'Eme\\Schemas\\Solicits\\Node\\SolicitV1', 'eme:solicits:request:search-submissions-request' => 'Eme\\Schemas\\Solicits\\Request\\SearchSubmissionsRequestV1', 'eme:solicits:request:search-submissions-response' => 'Eme\\Schemas\\Solicits\\Request\\SearchSubmissionsResponseV1', 'gdbots:ncr:command:create-edge' => 'Gdbots\\Schemas\\Ncr\\Command\\CreateEdgeV1', 'gdbots:ncr:command:delete-edge' => 'Gdbots\\Schemas\\Ncr\\Command\\DeleteEdgeV1', 'gdbots:ncr:event:edge-created' => 'Gdbots\\Schemas\\Ncr\\Event\\EdgeCreatedV1', 'gdbots:ncr:event:edge-deleted' => 'Gdbots\\Schemas\\Ncr\\Event\\EdgeDeletedV1', 'gdbots:pbjx:event:event-execution-failed' => 'Gdbots\\Schemas\\Pbjx\\Event\\EventExecutionFailedV1', 'gdbots:pbjx:request:echo-request' => 'Gdbots\\Schemas\\Pbjx\\Request\\EchoRequestV1', 'gdbots:pbjx:request:echo-response' => 'Gdbots\\Schemas\\Pbjx\\Request\\EchoResponseV1', 'gdbots:pbjx:request:request-failed-response' => 'Gdbots\\Schemas\\Pbjx\\Request\\RequestFailedResponseV1', 'gdbots:analytics:tracker:keen' => 'Gdbots\\Schemas\\Analytics\\Tracker\\KeenV1', 'eme:solicits:request:get-submission-history-request' => 'Eme\\Schemas\\Solicits\\Request\\GetSubmissionHistoryRequestV1', 'eme:solicits:request:get-submission-history-response' => 'Eme\\Schemas\\Solicits\\Request\\GetSubmissionHistoryResponseV1', 'eme:users:node:user' => 'Eme\\Schemas\\Users\\Node\\UserV1', 'gdbots:contexts::app' => 'Gdbots\\Schemas\\Contexts\\AppV1', 'gdbots:contexts::cloud' => 'Gdbots\\Schemas\\Contexts\\CloudV1', 'gdbots:contexts::user-agent' => 'Gdbots\\Schemas\\Contexts\\UserAgentV1', 'gdbots:geo::address' => 'Gdbots\\Schemas\\Geo\\AddressV1', 'gdbots:pbjx::envelope' => 'Gdbots\\Schemas\\Pbjx\\EnvelopeV1', 'gdbots:pbjx:command:check-health' => 'Gdbots\\Schemas\\Pbjx\\Command\\CheckHealthV1', 'gdbots:pbjx:event:health-checked' => 'Gdbots\\Schemas\\Pbjx\\Event\\HealthCheckedV1']);