示例#1
0
 /**
  * @param Message $type
  * @param string Field $field
  */
 public function __construct(Message $type, Field $field)
 {
     $this->type = $type;
     $this->schema = $type->schema();
     $this->field = $field;
     parent::__construct(sprintf('Required field [%s] must be set on message [%s].', $this->field->getName(), $this->schema->getClassName()));
 }
示例#2
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;
 }
示例#3
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;
 }