コード例 #1
0
 /**
  * {@inheritdoc}
  */
 public function serializedSize(\Protobuf\ComputeSizeContext $context)
 {
     $calculator = $context->getSizeCalculator();
     $size = 0;
     if ($this->name !== null) {
         $size += 1;
         $size += $calculator->computeStringSize($this->name);
     }
     if ($this->number !== null) {
         $size += 1;
         $size += $calculator->computeVarintSize($this->number);
     }
     if ($this->label !== null) {
         $size += 1;
         $size += $calculator->computeVarintSize($this->label->value());
     }
     if ($this->type !== null) {
         $size += 1;
         $size += $calculator->computeVarintSize($this->type->value());
     }
     if ($this->type_name !== null) {
         $size += 1;
         $size += $calculator->computeStringSize($this->type_name);
     }
     if ($this->extendee !== null) {
         $size += 1;
         $size += $calculator->computeStringSize($this->extendee);
     }
     if ($this->default_value !== null) {
         $size += 1;
         $size += $calculator->computeStringSize($this->default_value);
     }
     if ($this->oneof_index !== null) {
         $size += 1;
         $size += $calculator->computeVarintSize($this->oneof_index);
     }
     if ($this->json_name !== null) {
         $size += 1;
         $size += $calculator->computeStringSize($this->json_name);
     }
     if ($this->options !== null) {
         $innerSize = $this->options->serializedSize($context);
         $size += 1;
         $size += $innerSize;
         $size += $calculator->computeVarintSize($innerSize);
     }
     if ($this->extensions !== null) {
         $size += $this->extensions->serializedSize($context);
     }
     return $size;
 }
    public function testGenerateWritePackedEnumRepeatedStatement()
    {
        $options = new FieldOptions();
        $context = $this->createContext([['name' => 'simple.proto', 'package' => 'ProtobufCompilerTest.Protos', 'values' => ['messages' => [['name' => 'Simple', 'fields' => [1 => ['status', Field::TYPE_ENUM, Field::LABEL_REPEATED, 'ProtobufCompilerTest.Protos.Type']]], ['name' => 'Type', 'fields' => []]]]]]);
        $entity = $context->getEntity('ProtobufCompilerTest.Protos.Simple');
        $generator = new WriteFieldStatementGenerator($context);
        $descritor = $entity->getDescriptor();
        $field = $descritor->getFieldList()[0];
        $options->setPacked(true);
        $field->setOptions($options);
        $actual = $generator->generateFieldWriteStatement($entity, $field);
        $expected = <<<'CODE'
$innerSize   = 0;
$calculator  = $sizeContext->getSizeCalculator();

foreach ($this->status as $val) {
    $innerSize += $calculator->computeVarintSize($val->value());
}

$writer->writeVarint($stream, 10);
$writer->writeVarint($stream, $innerSize);

foreach ($this->status as $val) {
    $writer->writeVarint($stream, $val->value());
}
CODE;
        $this->assertEquals($expected, implode(PHP_EOL, $actual));
    }
コード例 #3
0
    public function testGenerateReadPackedEnumStatement()
    {
        $options = new FieldOptions();
        $context = $this->createContext([['name' => 'simple.proto', 'package' => 'ProtobufCompilerTest.Protos', 'values' => ['messages' => [['name' => 'Simple', 'fields' => [1 => ['status', Field::TYPE_ENUM, Field::LABEL_REPEATED, 'ProtobufCompilerTest.Protos.Type']]], ['name' => 'Type', 'fields' => []]]]]]);
        $generator = new ReadFieldStatementGenerator($context);
        $entity = $context->getEntity($this->messageClass);
        $descritor = $entity->getDescriptor();
        $field = $descritor->getFieldList()[0];
        $options->setPacked(true);
        $field->setOptions($options);
        $actual = $generator->generateFieldReadStatement($entity, $field);
        $expected = <<<'CODE'
$innerSize  = $reader->readVarint($stream);
$innerLimit = $stream->tell() + $innerSize;

if ($this->status === null) {
    $this->status = new \Protobuf\EnumCollection();
}

while ($stream->tell() < $innerLimit) {
    $this->status->add(\ProtobufCompilerTest\Protos\Type::valueOf($reader->readVarint($stream)));
}

continue;
CODE;
        $this->assertEquals($expected, implode(PHP_EOL, $actual));
    }
コード例 #4
0
 /**
  * @param integer $number
  * @param string  $name
  * @param integer $type
  * @param integer $label
  * @param string  $typeName
  * @param array   $values
  *
  * @return \google\protobuf\FieldDescriptorProto
  */
 protected function createFieldDescriptorProto($number, $name, $type, $label, $typeName = null, array $values = [])
 {
     $field = new FieldDescriptorProto();
     $options = isset($values['options']) ? $values['options'] : null;
     $field->setName($name);
     $field->setNumber($number);
     $field->setTypeName($typeName);
     $field->setType(FieldDescriptorProto\Type::valueOf($type));
     $field->setLabel(FieldDescriptorProto\Label::valueOf($label));
     if (isset($values['default'])) {
         $field->setDefaultValue($values['default']);
     }
     if ($options !== null) {
         $fieldOptions = new FieldOptions();
         if (isset($options['packed'])) {
             $fieldOptions->setPacked($options['packed']);
         }
         $field->setOptions($fieldOptions);
     }
     return $field;
 }