Esempio n. 1
0
 /**
  * {@inheritdoc}
  * @return Message
  */
 public function deserialize($data, array $options = [])
 {
     $this->options = $options;
     Assertion::keyIsset($data, Schema::PBJ_FIELD_NAME, sprintf('[%s::%s] Array provided must contain the [%s] key.', get_called_class(), __FUNCTION__, Schema::PBJ_FIELD_NAME));
     return $this->doDeserialize($data);
 }
Esempio n. 2
0
 /**
  * @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();
 }