/**
  * @param \Protobuf\Compiler\Entity             $entity
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string[]
  */
 public function generateFieldWriteStatement(Entity $entity, FieldDescriptorProto $field)
 {
     $body = [];
     $name = $field->getName();
     $type = $field->getType();
     $rule = $field->getLabel();
     $tag = $field->getNumber();
     $options = $field->getOptions();
     $variable = $this->targetVar ?: '$this->' . $name;
     $isPack = $options ? $options->getPacked() : false;
     $wire = $isPack ? WireFormat::WIRE_LENGTH : WireFormat::getWireType($type->value(), null);
     $key = WireFormat::getFieldKey($tag, $wire);
     if ($rule === Label::LABEL_REPEATED() && $isPack) {
         $itemValSttm = $type === Type::TYPE_ENUM() ? '$val->value()' : '$val';
         $body[] = '$innerSize   = 0;';
         $body[] = '$calculator  = $sizeContext->getSizeCalculator();';
         $body[] = null;
         $body[] = 'foreach (' . $variable . ' as $val) {';
         $body[] = '    $innerSize += ' . $this->generateValueSizeStatement($type->value(), $itemValSttm) . ';';
         $body[] = '}';
         $body[] = null;
         $body[] = '$writer->writeVarint($stream, ' . $key . ');';
         $body[] = '$writer->writeVarint($stream, $innerSize);';
         $body[] = null;
         $body[] = 'foreach (' . $variable . ' as $val) {';
         $body[] = '    ' . $this->generateWriteScalarStatement($type->value(), $itemValSttm) . ';';
         $body[] = '}';
         return $body;
     }
     if ($type === Type::TYPE_MESSAGE() && $rule === Label::LABEL_REPEATED()) {
         $body[] = 'foreach (' . $variable . ' as $val) {';
         $body[] = '    $writer->writeVarint($stream, ' . $key . ');';
         $body[] = '    $writer->writeVarint($stream, $val->serializedSize($sizeContext));';
         $body[] = '    $val->writeTo($context);';
         $body[] = '}';
         return $body;
     }
     if ($type === Type::TYPE_ENUM() && $rule === LABEL::LABEL_REPEATED()) {
         $body[] = 'foreach (' . $variable . ' as $val) {';
         $body[] = '    $writer->writeVarint($stream, ' . $key . ');';
         $body[] = '    ' . $this->generateWriteScalarStatement($type->value(), '$val->value()') . ';';
         $body[] = '}';
         return $body;
     }
     if ($rule === Label::LABEL_REPEATED()) {
         $body[] = 'foreach (' . $variable . ' as $val) {';
         $body[] = '    $writer->writeVarint($stream, ' . $key . ');';
         $body[] = '    ' . $this->generateWriteScalarStatement($type->value(), '$val') . ';';
         $body[] = '}';
         return $body;
     }
     if ($type === Type::TYPE_ENUM()) {
         $body[] = sprintf('$writer->writeVarint($stream, %s);', $key);
         $body[] = $this->generateWriteScalarStatement($type->value(), $variable . '->value()') . ';';
         return $body;
     }
     if ($type !== Type::TYPE_MESSAGE()) {
         $body[] = sprintf('$writer->writeVarint($stream, %s);', $key);
         $body[] = $this->generateWriteScalarStatement($type->value(), $variable) . ';';
         return $body;
     }
     $body[] = '$writer->writeVarint($stream, ' . $key . ');';
     $body[] = '$writer->writeVarint($stream, ' . $variable . '->serializedSize($sizeContext));';
     $body[] = $variable . '->writeTo($context);';
     return $body;
 }
 /**
  * @param \Protobuf\Compiler\Entity             $entity
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string[]
  */
 public function generateFieldReadStatement(Entity $entity, FieldDescriptorProto $field)
 {
     $body = [];
     $reference = null;
     $type = $field->getType();
     $name = $field->getName();
     $rule = $field->getLabel();
     $tag = $field->getNumber();
     $options = $field->getOptions();
     $isPack = $options ? $options->getPacked() : false;
     $variable = $this->targetVar ?: '$this->' . $name;
     $breakSttm = $this->getBreakStatement($variable);
     if ($field->hasTypeName()) {
         $typeName = $field->getTypeName();
         $typeEntity = $this->getEntity($typeName);
         $reference = $typeEntity->getNamespacedName();
     }
     if (!$isPack) {
         $body[] = sprintf('\\Protobuf\\WireFormat::assertWireType($wire, %s);', $type->value());
         $body[] = null;
     }
     if ($rule === Label::LABEL_REPEATED() && $isPack) {
         $readSttm = $type === Type::TYPE_ENUM() ? $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . ')' : $this->generateReadScalarStatement($type->value());
         $body[] = '$innerSize  = $reader->readVarint($stream);';
         $body[] = '$innerLimit = $stream->tell() + $innerSize;';
         $body[] = null;
         $body[] = 'if (' . $variable . ' === null) {';
         $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
         $body[] = '}';
         $body[] = null;
         $body[] = 'while ($stream->tell() < $innerLimit) {';
         $body[] = '    ' . $variable . '->add(' . $readSttm . ');';
         $body[] = '}';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     if ($type === Type::TYPE_MESSAGE() && $rule === Label::LABEL_REPEATED()) {
         $body[] = '$innerSize    = $reader->readVarint($stream);';
         $body[] = '$innerMessage = new ' . $reference . '();';
         $body[] = null;
         $body[] = 'if (' . $variable . ' === null) {';
         $body[] = '    ' . $variable . ' = new \\Protobuf\\MessageCollection();';
         $body[] = '}';
         $body[] = null;
         $body[] = $variable . '->add($innerMessage);';
         $body[] = null;
         $body[] = '$context->setLength($innerSize);';
         $body[] = '$innerMessage->readFrom($context);';
         $body[] = '$context->setLength($length);';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     if ($type === Type::TYPE_ENUM() && $rule === LABEL::LABEL_REPEATED()) {
         $body[] = 'if (' . $variable . ' === null) {';
         $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
         $body[] = '}';
         $body[] = null;
         $body[] = $variable . '->add(' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . '));';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     if ($type === Type::TYPE_MESSAGE()) {
         $body[] = '$innerSize    = $reader->readVarint($stream);';
         $body[] = '$innerMessage = new ' . $reference . '();';
         $body[] = null;
         $body[] = $variable . ' = $innerMessage;';
         $body[] = null;
         $body[] = '$context->setLength($innerSize);';
         $body[] = '$innerMessage->readFrom($context);';
         $body[] = '$context->setLength($length);';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     if ($type === Type::TYPE_ENUM()) {
         $body[] = $variable . ' = ' . $reference . '::valueOf(' . $this->generateReadScalarStatement($type->value()) . ');';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     if ($rule !== LABEL::LABEL_REPEATED()) {
         $body[] = $variable . ' = ' . $this->generateReadScalarStatement($type->value()) . ';';
         $body[] = null;
         $body[] = $breakSttm;
         return $body;
     }
     $body[] = 'if (' . $variable . ' === null) {';
     $body[] = '    ' . $variable . ' = new ' . $this->getCollectionClassName($field) . '();';
     $body[] = '}';
     $body[] = null;
     $body[] = $variable . '->add(' . $this->generateReadScalarStatement($type->value()) . ');';
     $body[] = null;
     $body[] = $breakSttm;
     return $body;
 }
Exemplo n.º 3
0
 protected function getJavaDocType(proto\FieldDescriptorProto $field)
 {
     switch ($field->getType()) {
         case Protobuf::TYPE_DOUBLE:
         case Protobuf::TYPE_FLOAT:
             return 'float';
         case Protobuf::TYPE_INT64:
         case Protobuf::TYPE_UINT64:
         case Protobuf::TYPE_INT32:
         case Protobuf::TYPE_FIXED64:
         case Protobuf::TYPE_FIXED32:
         case Protobuf::TYPE_UINT32:
         case Protobuf::TYPE_SFIXED32:
         case Protobuf::TYPE_SFIXED64:
         case Protobuf::TYPE_SINT32:
         case Protobuf::TYPE_SINT64:
             return 'int';
         case Protobuf::TYPE_BOOL:
             return 'boolean';
         case Protobuf::TYPE_STRING:
             return 'string';
         case Protobuf::TYPE_MESSAGE:
             return '\\' . $this->normalizeNS($field->getTypeName());
         case Protobuf::TYPE_BYTES:
             return 'string';
         case Protobuf::TYPE_ENUM:
             return 'int - \\' . $this->normalizeNS($field->getTypeName());
         case Protobuf::TYPE_GROUP:
         default:
             return 'unknown';
     }
 }
Exemplo n.º 4
0
 /**
  * Obtain a JavaDoc style type for the given field (int, float, string)
  *
  * @param proto\FieldDescriptorProto $field
  * return string
  */
 protected function doctype(proto\FieldDescriptorProto $field)
 {
     switch ($field->getType()) {
         case Protobuf\Protobuf::TYPE_DOUBLE:
         case Protobuf\Protobuf::TYPE_FLOAT:
             return 'float';
         case Protobuf\Protobuf::TYPE_INT64:
         case Protobuf\Protobuf::TYPE_UINT64:
         case Protobuf\Protobuf::TYPE_INT32:
         case Protobuf\Protobuf::TYPE_FIXED64:
         case Protobuf\Protobuf::TYPE_FIXED32:
         case Protobuf\Protobuf::TYPE_UINT32:
         case Protobuf\Protobuf::TYPE_SFIXED32:
         case Protobuf\Protobuf::TYPE_SFIXED64:
         case Protobuf\Protobuf::TYPE_SINT32:
         case Protobuf\Protobuf::TYPE_SINT64:
             return 'int';
         case Protobuf\Protobuf::TYPE_BOOL:
             return 'boolean';
         case Protobuf\Protobuf::TYPE_STRING:
             return 'string';
         case Protobuf\Protobuf::TYPE_MESSAGE:
             return $this->ns($field->type_name);
         case Protobuf\Protobuf::TYPE_BYTES:
             return 'string';
         case Protobuf\Protobuf::TYPE_ENUM:
             return 'int - ' . $this->ns($field->type_name);
         default:
             return '*unknown*';
     }
 }
Exemplo n.º 5
0
 public static function isEnum(\google\protobuf\FieldDescriptorProto $field)
 {
     return self::TYPE_ENUM == $field->getType();
 }
 /**
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string
  */
 protected function getDefaultFieldValue(FieldDescriptorProto $field)
 {
     $type = $field->getType();
     $value = $field->getDefaultValue();
     if ($value === null) {
         return 'null';
     }
     if ($type === Type::TYPE_ENUM()) {
         $typeName = $field->getTypeName();
         $refEntity = $this->getEntity($typeName);
         $reference = $refEntity->getNamespacedName();
         $const = $reference . '::' . $value . '()';
         return $const;
     }
     if ($type === Type::TYPE_BOOL()) {
         $value = filter_var($value, FILTER_VALIDATE_BOOLEAN);
     }
     return var_export($value, true);
 }
Exemplo n.º 7
0
 public function defaultValueAsString(FieldDescriptorProto $field)
 {
     if (!$field->hasDefaultValue()) {
         return 'null';
     }
     $default_value = null;
     switch ($field->getType()) {
         case \ProtocolBuffers::TYPE_DOUBLE:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FLOAT:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_INT64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_UINT64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_INT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FIXED64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_FIXED32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_BOOL:
             return $field->getDefaultValue() == "true" ? "true" : "false";
         case \ProtocolBuffers::TYPE_STRING:
             return "\"" . addcslashes($field->getDefaultValue(), "\"\n\r\$") . "\"";
         case \ProtocolBuffers::TYPE_GROUP:
             return;
         case \ProtocolBuffers::TYPE_MESSAGE:
             return "null";
             break;
         case \ProtocolBuffers::TYPE_BYTES:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_UINT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_ENUM:
             $value = $field->getTypeName();
             $descriptor = MessagePool::get($value);
             if (!FieldDescriptorProto\Label::isRepeated($field)) {
                 $def = $field->getDefaultValue();
                 if (!empty($def)) {
                     $value = str_replace(".", "\\", $descriptor->full_name) . "::" . $field->getDefaultValue();
                 } else {
                     $value = "null";
                 }
                 return $value;
             } else {
                 return "array()";
             }
         case \ProtocolBuffers::TYPE_SFIXED32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SFIXED64:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SINT32:
             $default_value = $field->getDefaultValue();
             break;
         case \ProtocolBuffers::TYPE_SINT64:
             $default_value = $field->getDefaultValue();
             break;
     }
     if (!$default_value && $default_value !== 0) {
         return "null";
     } else {
         return $default_value;
     }
 }
Exemplo n.º 8
0
 public function getJsDoc(proto\FieldDescriptorProto $field)
 {
     switch ($field->getType()) {
         case Protobuf::TYPE_DOUBLE:
         case Protobuf::TYPE_FLOAT:
             return 'Float';
         case Protobuf::TYPE_INT64:
         case Protobuf::TYPE_UINT64:
         case Protobuf::TYPE_INT32:
         case Protobuf::TYPE_FIXED64:
         case Protobuf::TYPE_FIXED32:
         case Protobuf::TYPE_UINT32:
         case Protobuf::TYPE_SFIXED32:
         case Protobuf::TYPE_SFIXED64:
         case Protobuf::TYPE_SINT32:
         case Protobuf::TYPE_SINT64:
             return 'Int';
         case Protobuf::TYPE_BOOL:
             return 'Boolean';
         case Protobuf::TYPE_STRING:
             return 'String';
         case Protobuf::TYPE_MESSAGE:
             return $this->normalizeNS($field->getTypeName());
         case Protobuf::TYPE_BYTES:
             return 'String';
         case Protobuf::TYPE_ENUM:
             return 'Int (' . $this->normalizeNS($field->getTypeName()) . ')';
         case Protobuf::TYPE_GROUP:
         default:
             return 'unknown';
     }
 }
 /**
  * @param \google\protobuf\FieldDescriptorProto $field
  *
  * @return string[]
  */
 protected function generateFieldBody(FieldDescriptorProto $field)
 {
     $lines = [];
     $name = $field->getName();
     $number = $field->getNumber();
     $typeName = $field->getTypeName();
     $extendee = $field->getExtendee();
     $type = $field->getType()->name();
     $label = $field->getLabel()->name();
     $default = $this->getDefaultFieldValue($field);
     $values = ['number' => var_export($number, true), 'name' => var_export($name, true), 'type' => '\\google\\protobuf\\FieldDescriptorProto\\Type::' . $type . '()', 'label' => '\\google\\protobuf\\FieldDescriptorProto\\Label::' . $label . '()'];
     if ($extendee) {
         $values['extendee'] = var_export($extendee, true);
     }
     if ($typeName) {
         $values['type_name'] = var_export($typeName, true);
     }
     if ($field->hasDefaultValue()) {
         $values['default_value'] = $default;
     }
     $lines[] = '\\google\\protobuf\\FieldDescriptorProto::fromArray([';
     $lines = array_merge($lines, $this->generateArrayLines($values));
     $lines[] = ']),';
     return $lines;
 }
 /**
  * @param \Protobuf\Compiler\Entity            $entity
  * @param google\protobuf\FieldDescriptorProto $field
  *
  * @return \Zend\Code\Generator\GeneratorInterface
  */
 protected function generateAddMethod(Entity $entity, FieldDescriptorProto $field)
 {
     $fieldName = $field->getName();
     $fieldType = $field->getType();
     $collClass = $this->getCollectionClassName($field);
     $methodName = 'add' . $this->getClassifiedName($field);
     $typeHint = $fieldType !== Type::TYPE_BYTES() ? $this->getDoctype($field) : null;
     $lines[] = 'if ($this->' . $fieldName . ' === null) {';
     $lines[] = '    $this->' . $fieldName . ' = new ' . $collClass . '();';
     $lines[] = '}';
     $lines[] = null;
     $lines[] = $fieldType !== Type::TYPE_BYTES() ? '$this->' . $fieldName . '->add($value);' : '$this->' . $fieldName . '->add(\\Protobuf\\Stream::wrap($value));';
     return MethodGenerator::fromArray(['name' => $methodName, 'body' => implode(PHP_EOL, $lines), 'parameters' => [['name' => 'value', 'type' => $typeHint]], 'docblock' => ['shortDescription' => "Add a new element to '{$fieldName}'", 'tags' => [['name' => 'param', 'description' => $this->getDoctype($field) . ' $value']]]]);
 }