コード例 #1
0
 /**
  * @param \Protobuf\Compiler\Entity             $entity
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string[]
  */
 protected function generateRequiredFieldException(Entity $entity, FieldDescriptorProto $field)
 {
     $name = $field->getName();
     $label = $field->getLabel();
     $tag = $field->getNumber();
     $isRequired = $label === Label::LABEL_REQUIRED();
     if (!$isRequired) {
         return [];
     }
     $class = $entity->getNamespacedName();
     $format = 'Field "%s#%s" (tag %s) is required but has no value.';
     $message = var_export(sprintf($format, $class, $name, $tag), true);
     $body[] = 'if ($this->' . $name . ' === null) {';
     $body[] = '    throw new \\UnexpectedValueException(' . $message . ');';
     $body[] = '}';
     $body[] = null;
     return $body;
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public static function descriptor()
 {
     return \google\protobuf\DescriptorProto::fromArray(['name' => 'NamePart', 'field' => [\google\protobuf\FieldDescriptorProto::fromArray(['number' => 1, 'name' => 'name_part', 'type' => \google\protobuf\FieldDescriptorProto\Type::TYPE_STRING(), 'label' => \google\protobuf\FieldDescriptorProto\Label::LABEL_REQUIRED()]), \google\protobuf\FieldDescriptorProto::fromArray(['number' => 2, 'name' => 'is_extension', 'type' => \google\protobuf\FieldDescriptorProto\Type::TYPE_BOOL(), 'label' => \google\protobuf\FieldDescriptorProto\Label::LABEL_REQUIRED()])]]);
 }
コード例 #3
0
 /**
  * @param \Protobuf\Compiler\Entity $entity
  *
  * @return array
  */
 protected function generateDefaultValues(Entity $entity)
 {
     $descriptor = $entity->getDescriptor();
     $fields = $descriptor->getFieldList() ?: [];
     $size = count($fields);
     $lines = [];
     foreach ($fields as $i => $field) {
         $name = $field->getName();
         $comma = $i + 1 < $size ? ',' : '';
         $value = $this->getDefaultFieldValue($field);
         // required field throw InvalidArgumentException
         if ($field->getLabel() === Label::LABEL_REQUIRED()) {
             continue;
         }
         if ($field->getLabel() === Label::LABEL_REPEATED()) {
             $value = '[]';
         }
         $lines[] = "'{$name}' => {$value}" . $comma;
     }
     return $lines;
 }
コード例 #4
0
 /**
  * @param \Protobuf\Compiler\Entity            $entity
  * @param google\protobuf\FieldDescriptorProto $field
  *
  * @return \Zend\Code\Generator\GeneratorInterface
  */
 protected function generateSetterMethod(Entity $entity, FieldDescriptorProto $field)
 {
     $body = [];
     $fieldName = $field->getName();
     $fieldType = $field->getType();
     $fieldLabel = $field->getLabel();
     $typeHint = $this->getTypeHint($field);
     if ($fieldType === Type::TYPE_BYTES() && $fieldLabel !== Label::LABEL_REPEATED()) {
         $body[] = 'if ($value !== null && ! $value instanceof \\Protobuf\\Stream) {';
         $body[] = '    $value = \\Protobuf\\Stream::wrap($value);';
         $body[] = '}';
         $body[] = null;
         $typeHint = null;
     }
     $body[] = '$this->' . $fieldName . ' = $value;';
     $methodName = $this->getAccessorName('set', $field);
     $method = MethodGenerator::fromArray(['name' => $methodName, 'body' => implode(PHP_EOL, $body), 'parameters' => [['name' => 'value', 'type' => $typeHint]], 'docblock' => ['shortDescription' => "Set '{$fieldName}' value", 'tags' => [['name' => 'param', 'description' => $this->getDocBlockType($field) . ' $value']]]]);
     $method->getDocblock()->setWordWrap(false);
     if ($fieldLabel !== Label::LABEL_REQUIRED()) {
         $method->getParameters()['value']->setDefaultValue(null);
     }
     return $method;
 }