public function testWriteStream() { $source = Stream::create(); $target = Stream::create(); $writer = new StreamWriter($this->config); $writer->writeVarint($source, WireFormat::getFieldKey(1, WireFormat::WIRE_FIXED64)); $writer->writeDouble($source, 123456789.12345); $source->seek(0); $writer->writeStream($target, $source); $this->assertEquals((string) $source, (string) $target); }
/** * @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; }