Ejemplo n.º 1
0
 /**
  * @param Message $message
  * @return array
  *
  * @throws \Exception
  * @throws GdbotsPbjException
  */
 public function marshal(Message $message)
 {
     $schema = $message::schema();
     $message->validate();
     $payload = [];
     foreach ($schema->getFields() as $field) {
         $fieldName = $field->getName();
         if (!$message->has($fieldName)) {
             if ($message->hasClearedField($fieldName)) {
                 $payload[$fieldName] = ['NULL' => true];
             }
             continue;
         }
         $value = $message->get($fieldName);
         switch ($field->getRule()->getValue()) {
             case FieldRule::A_SINGLE_VALUE:
                 $payload[$fieldName] = $this->encodeValue($value, $field);
                 break;
             case FieldRule::A_SET:
                 $payload[$fieldName] = $this->encodeASetValue($value, $field);
                 break;
             case FieldRule::A_LIST:
                 $list = [];
                 foreach ($value as $v) {
                     $list[] = $this->encodeValue($v, $field);
                 }
                 $payload[$fieldName] = ['L' => $list];
                 break;
             case FieldRule::A_MAP:
                 $map = [];
                 foreach ($value as $k => $v) {
                     $map[$k] = $this->encodeValue($v, $field);
                 }
                 $payload[$fieldName] = ['M' => $map];
                 break;
             default:
                 break;
         }
     }
     return $payload;
 }
Ejemplo n.º 2
0
 /**
  * @param Message $message
  *
  * @return array
  */
 private function doSerialize(Message $message)
 {
     $schema = $message::schema();
     $message->validate();
     $payload = [];
     $includeAllFields = isset($this->options['includeAllFields']) && true === $this->options['includeAllFields'];
     foreach ($schema->getFields() as $field) {
         $fieldName = $field->getName();
         if (!$message->has($fieldName)) {
             if ($includeAllFields || $message->hasClearedField($fieldName)) {
                 $payload[$fieldName] = null;
             }
             continue;
         }
         $value = $message->get($fieldName);
         $type = $field->getType();
         switch ($field->getRule()->getValue()) {
             case FieldRule::A_SINGLE_VALUE:
                 $payload[$fieldName] = $type->encode($value, $field, $this);
                 break;
             case FieldRule::A_SET:
             case FieldRule::A_LIST:
                 $payload[$fieldName] = [];
                 foreach ($value as $v) {
                     $payload[$fieldName][] = $type->encode($v, $field, $this);
                 }
                 break;
             case FieldRule::A_MAP:
                 $payload[$fieldName] = [];
                 foreach ($value as $k => $v) {
                     $payload[$fieldName][$k] = $type->encode($v, $field, $this);
                 }
                 break;
             default:
                 break;
         }
     }
     return $payload;
 }