diff options
Diffstat (limited to 'php/src/Google/Protobuf')
39 files changed, 1354 insertions, 828 deletions
diff --git a/php/src/Google/Protobuf/Internal/Descriptor.php b/php/src/Google/Protobuf/Internal/Descriptor.php new file mode 100644 index 00000000..f8d24e45 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/Descriptor.php @@ -0,0 +1,167 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class Descriptor +{ + + private $full_name; + private $field = []; + private $nested_type = []; + private $enum_type = []; + private $klass; + private $options; + private $oneof_decl = []; + + public function addOneofDecl($oneof) + { + $this->oneof_decl[] = $oneof; + } + + public function getOneofDecl() + { + return $this->oneof_decl; + } + + public function setFullName($full_name) + { + $this->full_name = $full_name; + } + + public function getFullName() + { + return $this->full_name; + } + + public function addField($field) + { + $this->field[$field->getNumber()] = $field; + } + + public function getField() + { + return $this->field; + } + + public function addNestedType($desc) + { + $this->nested_type[] = $desc; + } + + public function getNestedType() + { + return $this->nested_type; + } + + public function addEnumType($desc) + { + $this->enum_type[] = $desc; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function getFieldByNumber($number) + { + if (!isset($this->field[$number])) { + return NULL; + } else { + return $this->field[$number]; + } + } + + public function setClass($klass) + { + $this->klass = $klass; + } + + public function getClass() + { + return $this->klass; + } + + public function setOptions($options) + { + $this->options = $options; + } + + public function getOptions() + { + return $this->options; + } + + public static function buildFromProto($proto, $file_proto, $containing) + { + $desc = new Descriptor(); + + $message_name_without_package = ""; + $classname = ""; + $fullname = ""; + GPBUtil::getFullClassName( + $proto, + $containing, + $file_proto, + $message_name_without_package, + $classname, + $fullname); + $desc->setFullName($fullname); + $desc->setClass($classname); + $desc->setOptions($proto->getOptions()); + + foreach ($proto->getField() as $field_proto) { + $desc->addField(FieldDescriptor::buildFromProto($field_proto)); + } + + // Handle nested types. + foreach ($proto->getNestedType() as $nested_proto) { + $desc->addNestedType(Descriptor::buildFromProto( + $nested_proto, $file_proto, $message_name_without_package)); + } + + // Handle nested enum. + foreach ($proto->getEnumType() as $enum_proto) { + $desc->addEnumType(EnumDescriptor::buildFromProto( + $enum_proto, $file_proto, $message_name_without_package)); + } + + // Handle oneof fields. + foreach ($proto->getOneofDecl() as $oneof_proto) { + $desc->addOneofDecl( + OneofDescriptor::buildFromProto($oneof_proto, $desc)); + } + + return $desc; + } +} diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto.php b/php/src/Google/Protobuf/Internal/DescriptorProto.php index 948c5876..c72c0962 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto.php @@ -97,6 +97,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -120,6 +122,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class); $this->field = $arr; $this->has_field = true; + + return $this; } public function hasField() @@ -143,6 +147,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class); $this->extension = $arr; $this->has_extension = true; + + return $this; } public function hasExtension() @@ -166,6 +172,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class); $this->nested_type = $arr; $this->has_nested_type = true; + + return $this; } public function hasNestedType() @@ -189,6 +197,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class); $this->enum_type = $arr; $this->has_enum_type = true; + + return $this; } public function hasEnumType() @@ -212,6 +222,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ExtensionRange::class); $this->extension_range = $arr; $this->has_extension_range = true; + + return $this; } public function hasExtensionRange() @@ -235,6 +247,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\OneofDescriptorProto::class); $this->oneof_decl = $arr; $this->has_oneof_decl = true; + + return $this; } public function hasOneofDecl() @@ -258,6 +272,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MessageOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() @@ -281,6 +297,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto_ReservedRange::class); $this->reserved_range = $arr; $this->has_reserved_range = true; + + return $this; } public function hasReservedRange() @@ -314,6 +332,8 @@ class DescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING); $this->reserved_name = $arr; $this->has_reserved_name = true; + + return $this; } public function hasReservedName() diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php index 738a1738..b5e5453e 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto_ExtensionRange.php @@ -48,6 +48,8 @@ class DescriptorProto_ExtensionRange extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->start = $var; $this->has_start = true; + + return $this; } public function hasStart() @@ -71,6 +73,8 @@ class DescriptorProto_ExtensionRange extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->end = $var; $this->has_end = true; + + return $this; } public function hasEnd() diff --git a/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php b/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php index be36b8aa..e5b7b05a 100644 --- a/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php +++ b/php/src/Google/Protobuf/Internal/DescriptorProto_ReservedRange.php @@ -70,6 +70,8 @@ class DescriptorProto_ReservedRange extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->start = $var; $this->has_start = true; + + return $this; } public function hasStart() @@ -101,6 +103,8 @@ class DescriptorProto_ReservedRange extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->end = $var; $this->has_end = true; + + return $this; } public function hasEnd() diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptor.php b/php/src/Google/Protobuf/Internal/EnumDescriptor.php new file mode 100644 index 00000000..7360a477 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/EnumDescriptor.php @@ -0,0 +1,56 @@ +<?php + +namespace Google\Protobuf\Internal; + +class EnumDescriptor +{ + + private $klass; + private $full_name; + private $value; + + public function setFullName($full_name) + { + $this->full_name = $full_name; + } + + public function getFullName() + { + return $this->full_name; + } + + public function addValue($number, $value) + { + $this->value[$number] = $value; + } + + public function setClass($klass) + { + $this->klass = $klass; + } + + public function getClass() + { + return $this->klass; + } + + public static function buildFromProto($proto, $file_proto, $containing) + { + $desc = new EnumDescriptor(); + + $enum_name_without_package = ""; + $classname = ""; + $fullname = ""; + GPBUtil::getFullClassName( + $proto, + $containing, + $file_proto, + $enum_name_without_package, + $classname, + $fullname); + $desc->setFullName($fullname); + $desc->setClass($classname); + + return $desc; + } +} diff --git a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php index 73f6edbd..bf597436 100644 --- a/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumDescriptorProto.php @@ -57,6 +57,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -80,6 +82,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumValueDescriptorProto::class); $this->value = $arr; $this->has_value = true; + + return $this; } public function hasValue() @@ -103,6 +107,8 @@ class EnumDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() diff --git a/php/src/Google/Protobuf/Internal/EnumOptions.php b/php/src/Google/Protobuf/Internal/EnumOptions.php index 4fa0bce7..cfa0cb3e 100644 --- a/php/src/Google/Protobuf/Internal/EnumOptions.php +++ b/php/src/Google/Protobuf/Internal/EnumOptions.php @@ -79,6 +79,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->allow_alias = $var; $this->has_allow_alias = true; + + return $this; } public function hasAllowAlias() @@ -116,6 +118,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -147,6 +151,8 @@ class EnumOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php b/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php new file mode 100644 index 00000000..e65a4e8d --- /dev/null +++ b/php/src/Google/Protobuf/Internal/EnumValueDescriptor.php @@ -0,0 +1,37 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class EnumValueDescriptor +{ +} diff --git a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php index 94dc36ec..43eee73f 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/EnumValueDescriptorProto.php @@ -57,6 +57,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -80,6 +82,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->number = $var; $this->has_number = true; + + return $this; } public function hasNumber() @@ -103,6 +107,8 @@ class EnumValueDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\EnumValueOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() diff --git a/php/src/Google/Protobuf/Internal/EnumValueOptions.php b/php/src/Google/Protobuf/Internal/EnumValueOptions.php index 232a6738..d66c7684 100644 --- a/php/src/Google/Protobuf/Internal/EnumValueOptions.php +++ b/php/src/Google/Protobuf/Internal/EnumValueOptions.php @@ -73,6 +73,8 @@ class EnumValueOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -104,6 +106,8 @@ class EnumValueOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptor.php b/php/src/Google/Protobuf/Internal/FieldDescriptor.php new file mode 100644 index 00000000..6c91950f --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FieldDescriptor.php @@ -0,0 +1,236 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class FieldDescriptor +{ + + private $name; + private $setter; + private $getter; + private $number; + private $label; + private $type; + private $message_type; + private $enum_type; + private $packed; + private $is_map; + private $oneof_index = -1; + + public function setOneofIndex($index) + { + $this->oneof_index = $index; + } + + public function getOneofIndex() + { + return $this->oneof_index; + } + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } + + public function setSetter($setter) + { + $this->setter = $setter; + } + + public function getSetter() + { + return $this->setter; + } + + public function setGetter($getter) + { + $this->getter = $getter; + } + + public function getGetter() + { + return $this->getter; + } + + public function setNumber($number) + { + $this->number = $number; + } + + public function getNumber() + { + return $this->number; + } + + public function setLabel($label) + { + $this->label = $label; + } + + public function getLabel() + { + return $this->label; + } + + public function isRepeated() + { + return $this->label === GPBLabel::REPEATED; + } + + public function setType($type) + { + $this->type = $type; + } + + public function getType() + { + return $this->type; + } + + public function setMessageType($message_type) + { + $this->message_type = $message_type; + } + + public function getMessageType() + { + return $this->message_type; + } + + public function setEnumType($enum_type) + { + $this->enum_type = $enum_type; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function setPacked($packed) + { + $this->packed = $packed; + } + + public function getPacked() + { + return $this->packed; + } + + public function isPackable() + { + return $this->isRepeated() && self::isTypePackable($this->type); + } + + public function isMap() + { + return $this->getType() == GPBType::MESSAGE && + !is_null($this->getMessageType()->getOptions()) && + $this->getMessageType()->getOptions()->getMapEntry(); + } + + private static function isTypePackable($field_type) + { + return ($field_type !== GPBType::STRING && + $field_type !== GPBType::GROUP && + $field_type !== GPBType::MESSAGE && + $field_type !== GPBType::BYTES); + } + + public static function getFieldDescriptor( + $name, + $label, + $type, + $number, + $oneof_index, + $packed, + $type_name = null) + { + $field = new FieldDescriptor(); + $field->setName($name); + $camel_name = implode('', array_map('ucwords', explode('_', $name))); + $field->setGetter('get' . $camel_name); + $field->setSetter('set' . $camel_name); + $field->setType($type); + $field->setNumber($number); + $field->setLabel($label); + $field->setPacked($packed); + $field->setOneofIndex($oneof_index); + + // At this time, the message/enum type may have not been added to pool. + // So we use the type name as place holder and will replace it with the + // actual descriptor in cross building. + switch ($type) { + case GPBType::MESSAGE: + $field->setMessageType($type_name); + break; + case GPBType::ENUM: + $field->setEnumType($type_name); + break; + default: + break; + } + + return $field; + } + + public static function buildFromProto($proto) + { + $type_name = null; + switch ($proto->getType()) { + case GPBType::MESSAGE: + case GPBType::GROUP: + case GPBType::ENUM: + $type_name = $proto->getTypeName(); + break; + default: + break; + } + + $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; + $packed = false; + $options = $proto->getOptions(); + if ($options !== null) { + $packed = $options->getPacked(); + } + + return FieldDescriptor::getFieldDescriptor( + $proto->getName(), $proto->getLabel(), $proto->getType(), + $proto->getNumber(), $oneof_index, $packed, $type_name); + } +} diff --git a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php index 6ae2cd41..2af9c0a0 100644 --- a/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FieldDescriptorProto.php @@ -130,6 +130,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -153,6 +155,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->number = $var; $this->has_number = true; + + return $this; } public function hasNumber() @@ -176,6 +180,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldDescriptorProto_Label::class); $this->label = $var; $this->has_label = true; + + return $this; } public function hasLabel() @@ -209,6 +215,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldDescriptorProto_Type::class); $this->type = $var; $this->has_type = true; + + return $this; } public function hasType() @@ -248,6 +256,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->type_name = $var; $this->has_type_name = true; + + return $this; } public function hasTypeName() @@ -281,6 +291,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->extendee = $var; $this->has_extendee = true; + + return $this; } public function hasExtendee() @@ -320,6 +332,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->default_value = $var; $this->has_default_value = true; + + return $this; } public function hasDefaultValue() @@ -353,6 +367,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->oneof_index = $var; $this->has_oneof_index = true; + + return $this; } public function hasOneofIndex() @@ -390,6 +406,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->json_name = $var; $this->has_json_name = true; + + return $this; } public function hasJsonName() @@ -413,6 +431,8 @@ class FieldDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FieldOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() diff --git a/php/src/Google/Protobuf/Internal/FieldOptions.php b/php/src/Google/Protobuf/Internal/FieldOptions.php index 8db7ed67..b30caa75 100644 --- a/php/src/Google/Protobuf/Internal/FieldOptions.php +++ b/php/src/Google/Protobuf/Internal/FieldOptions.php @@ -156,6 +156,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldOptions_CType::class); $this->ctype = $var; $this->has_ctype = true; + + return $this; } public function hasCtype() @@ -195,6 +197,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->packed = $var; $this->has_packed = true; + + return $this; } public function hasPacked() @@ -242,6 +246,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FieldOptions_JSType::class); $this->jstype = $var; $this->has_jstype = true; + + return $this; } public function hasJstype() @@ -319,6 +325,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->lazy = $var; $this->has_lazy = true; + + return $this; } public function hasLazy() @@ -356,6 +364,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -387,6 +397,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->weak = $var; $this->has_weak = true; + + return $this; } public function hasWeak() @@ -418,6 +430,8 @@ class FieldOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/FileDescriptor.php b/php/src/Google/Protobuf/Internal/FileDescriptor.php new file mode 100644 index 00000000..038da38c --- /dev/null +++ b/php/src/Google/Protobuf/Internal/FileDescriptor.php @@ -0,0 +1,89 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class FileDescriptor +{ + + private $package; + private $message_type = []; + private $enum_type = []; + + public function setPackage($package) + { + $this->package = $package; + } + + public function getPackage() + { + return $this->package; + } + + public function getMessageType() + { + return $this->message_type; + } + + public function addMessageType($desc) + { + $this->message_type[] = $desc; + } + + public function getEnumType() + { + return $this->enum_type; + } + + public function addEnumType($desc) + { + $this->enum_type[]= $desc; + } + + public static function buildFromProto($proto) + { + $file = new FileDescriptor(); + $file->setPackage($proto->getPackage()); + foreach ($proto->getMessageType() as $message_proto) { + $file->addMessageType(Descriptor::buildFromProto( + $message_proto, $proto, "")); + } + foreach ($proto->getEnumType() as $enum_proto) { + $file->addEnumType( + EnumDescriptor::buildFromProto( + $enum_proto, + $proto, + "")); + } + return $file; + } +} diff --git a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php index 0363d9e9..b229522a 100644 --- a/php/src/Google/Protobuf/Internal/FileDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/FileDescriptorProto.php @@ -147,6 +147,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -178,6 +180,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->package = $var; $this->has_package = true; + + return $this; } public function hasPackage() @@ -209,6 +213,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING); $this->dependency = $arr; $this->has_dependency = true; + + return $this; } public function hasDependency() @@ -240,6 +246,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32); $this->public_dependency = $arr; $this->has_public_dependency = true; + + return $this; } public function hasPublicDependency() @@ -273,6 +281,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32); $this->weak_dependency = $arr; $this->has_weak_dependency = true; + + return $this; } public function hasWeakDependency() @@ -304,6 +314,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\DescriptorProto::class); $this->message_type = $arr; $this->has_message_type = true; + + return $this; } public function hasMessageType() @@ -327,6 +339,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\EnumDescriptorProto::class); $this->enum_type = $arr; $this->has_enum_type = true; + + return $this; } public function hasEnumType() @@ -350,6 +364,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\ServiceDescriptorProto::class); $this->service = $arr; $this->has_service = true; + + return $this; } public function hasService() @@ -373,6 +389,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FieldDescriptorProto::class); $this->extension = $arr; $this->has_extension = true; + + return $this; } public function hasExtension() @@ -396,6 +414,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\FileOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() @@ -433,6 +453,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\SourceCodeInfo::class); $this->source_code_info = $var; $this->has_source_code_info = true; + + return $this; } public function hasSourceCodeInfo() @@ -466,6 +488,8 @@ class FileDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->syntax = $var; $this->has_syntax = true; + + return $this; } public function hasSyntax() diff --git a/php/src/Google/Protobuf/Internal/FileDescriptorSet.php b/php/src/Google/Protobuf/Internal/FileDescriptorSet.php index 0bcc8051..8271ee66 100644 --- a/php/src/Google/Protobuf/Internal/FileDescriptorSet.php +++ b/php/src/Google/Protobuf/Internal/FileDescriptorSet.php @@ -48,6 +48,8 @@ class FileDescriptorSet extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\FileDescriptorProto::class); $this->file = $arr; $this->has_file = true; + + return $this; } public function hasFile() diff --git a/php/src/Google/Protobuf/Internal/FileOptions.php b/php/src/Google/Protobuf/Internal/FileOptions.php index 22653a4f..332da3dc 100644 --- a/php/src/Google/Protobuf/Internal/FileOptions.php +++ b/php/src/Google/Protobuf/Internal/FileOptions.php @@ -188,6 +188,17 @@ class FileOptions extends \Google\Protobuf\Internal\Message private $has_php_class_prefix = false; /** * <pre> + * Use this option to change the namespace of php generated classes. Default + * is empty. When this option is empty, the package name will be used for + * determining the namespace. + * </pre> + * + * <code>optional string php_namespace = 41;</code> + */ + private $php_namespace = ''; + private $has_php_namespace = false; + /** + * <pre> * The parser stores options it doesn't recognize here. See above. * </pre> * @@ -231,6 +242,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->java_package = $var; $this->has_java_package = true; + + return $this; } public function hasJavaPackage() @@ -270,6 +283,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->java_outer_classname = $var; $this->has_java_outer_classname = true; + + return $this; } public function hasJavaOuterClassname() @@ -311,6 +326,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->java_multiple_files = $var; $this->has_java_multiple_files = true; + + return $this; } public function hasJavaMultipleFiles() @@ -342,6 +359,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->java_generate_equals_and_hash = $var; $this->has_java_generate_equals_and_hash = true; + + return $this; } public function hasJavaGenerateEqualsAndHash() @@ -383,6 +402,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->java_string_check_utf8 = $var; $this->has_java_string_check_utf8 = true; + + return $this; } public function hasJavaStringCheckUtf8() @@ -406,6 +427,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\FileOptions_OptimizeMode::class); $this->optimize_for = $var; $this->has_optimize_for = true; + + return $this; } public function hasOptimizeFor() @@ -445,6 +468,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->go_package = $var; $this->has_go_package = true; + + return $this; } public function hasGoPackage() @@ -492,6 +517,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->cc_generic_services = $var; $this->has_cc_generic_services = true; + + return $this; } public function hasCcGenericServices() @@ -515,6 +542,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->java_generic_services = $var; $this->has_java_generic_services = true; + + return $this; } public function hasJavaGenericServices() @@ -538,6 +567,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->py_generic_services = $var; $this->has_py_generic_services = true; + + return $this; } public function hasPyGenericServices() @@ -575,6 +606,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -608,6 +641,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->cc_enable_arenas = $var; $this->has_cc_enable_arenas = true; + + return $this; } public function hasCcEnableArenas() @@ -641,6 +676,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->objc_class_prefix = $var; $this->has_objc_class_prefix = true; + + return $this; } public function hasObjcClassPrefix() @@ -672,6 +709,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->csharp_namespace = $var; $this->has_csharp_namespace = true; + + return $this; } public function hasCsharpNamespace() @@ -709,6 +748,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->swift_prefix = $var; $this->has_swift_prefix = true; + + return $this; } public function hasSwiftPrefix() @@ -742,6 +783,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->php_class_prefix = $var; $this->has_php_class_prefix = true; + + return $this; } public function hasPhpClassPrefix() @@ -751,6 +794,43 @@ class FileOptions extends \Google\Protobuf\Internal\Message /** * <pre> + * Use this option to change the namespace of php generated classes. Default + * is empty. When this option is empty, the package name will be used for + * determining the namespace. + * </pre> + * + * <code>optional string php_namespace = 41;</code> + */ + public function getPhpNamespace() + { + return $this->php_namespace; + } + + /** + * <pre> + * Use this option to change the namespace of php generated classes. Default + * is empty. When this option is empty, the package name will be used for + * determining the namespace. + * </pre> + * + * <code>optional string php_namespace = 41;</code> + */ + public function setPhpNamespace($var) + { + GPBUtil::checkString($var, True); + $this->php_namespace = $var; + $this->has_php_namespace = true; + + return $this; + } + + public function hasPhpNamespace() + { + return $this->has_php_namespace; + } + + /** + * <pre> * The parser stores options it doesn't recognize here. See above. * </pre> * @@ -773,6 +853,8 @@ class FileOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/GPBUtil.php b/php/src/Google/Protobuf/Internal/GPBUtil.php index 0e66ae6f..8c97e9fa 100644 --- a/php/src/Google/Protobuf/Internal/GPBUtil.php +++ b/php/src/Google/Protobuf/Internal/GPBUtil.php @@ -67,7 +67,6 @@ class GPBUtil } } - public static function checkString(&$var, $check_utf8) { if (is_array($var) || is_object($var)) { @@ -242,4 +241,103 @@ class GPBUtil { return new Uint64($value); } + + public static function getClassNamePrefix( + $classname, + $file_proto) + { + $option = $file_proto->getOptions(); + $prefix = is_null($option) ? "" : $option->getPhpClassPrefix(); + if ($prefix !== "") { + return $prefix; + } + + $reserved_words = array("Empty", "ECHO", "ARRAY"); + foreach ($reserved_words as $reserved_word) { + if ($classname === $reserved_word) { + if ($file_proto->getPackage() === "google.protobuf") { + return "GPB"; + } else { + return "PB"; + } + } + } + + return ""; + } + + public static function getClassNameWithoutPackage( + $name, + $file_proto) + { + $classname = implode('_', array_map('ucwords', explode('.', $name))); + return static::getClassNamePrefix($classname, $file_proto) . $classname; + } + + public static function getFullClassName( + $proto, + $containing, + $file_proto, + &$message_name_without_package, + &$classname, + &$fullname) + { + // Full name needs to start with '.'. + $message_name_without_package = $proto->getName(); + if ($containing !== "") { + $message_name_without_package = + $containing . "." . $message_name_without_package; + } + + $package = $file_proto->getPackage(); + if ($package === "") { + $fullname = "." . $message_name_without_package; + } else { + $fullname = "." . $package . "." . $message_name_without_package; + } + + $class_name_without_package = + static::getClassNameWithoutPackage($message_name_without_package, $file_proto); + + $option = $file_proto->getOptions(); + if (!is_null($option) && $option->hasPhpNamespace()) { + $namespace = $option->getPhpNamespace(); + if ($namespace !== "") { + $classname = $namespace . "\\" . $class_name_without_package; + return; + } else { + $classname = $class_name_without_package; + return; + } + } + + if ($package === "") { + $classname = $class_name_without_package; + } else { + $classname = + implode('\\', array_map('ucwords', explode('.', $package))). + "\\".$class_name_without_package; + } + } + + public static function combineInt32ToInt64($high, $low) + { + $isNeg = $high < 0; + if ($isNeg) { + $high = ~$high; + $low = ~$low; + $low++; + if (!$low) { + $high++; + } + } + $result = bcadd(bcmul($high, 4294967296), $low); + if ($low < 0) { + $result = bcadd($result, 4294967296); + } + if ($isNeg) { + $result = bcsub(0, $result); + } + return $result; + } } diff --git a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php index 450854f1..1e666f31 100644 --- a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php +++ b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo.php @@ -64,6 +64,8 @@ class GeneratedCodeInfo extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\GeneratedCodeInfo_Annotation::class); $this->annotation = $arr; $this->has_annotation = true; + + return $this; } public function hasAnnotation() diff --git a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php index ed22cc38..8d227e1c 100644 --- a/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php +++ b/php/src/Google/Protobuf/Internal/GeneratedCodeInfo_Annotation.php @@ -88,6 +88,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32); $this->path = $arr; $this->has_path = true; + + return $this; } public function hasPath() @@ -119,6 +121,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->source_file = $var; $this->has_source_file = true; + + return $this; } public function hasSourceFile() @@ -152,6 +156,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->begin = $var; $this->has_begin = true; + + return $this; } public function hasBegin() @@ -187,6 +193,8 @@ class GeneratedCodeInfo_Annotation extends \Google\Protobuf\Internal\Message GPBUtil::checkInt32($var); $this->end = $var; $this->has_end = true; + + return $this; } public function hasEnd() diff --git a/php/src/Google/Protobuf/Internal/InputStream.php b/php/src/Google/Protobuf/Internal/InputStream.php index 8012a225..f84e1aee 100644 --- a/php/src/Google/Protobuf/Internal/InputStream.php +++ b/php/src/Google/Protobuf/Internal/InputStream.php @@ -34,27 +34,6 @@ namespace Google\Protobuf\Internal; use Google\Protobuf\Internal\Uint64; -function combineInt32ToInt64($high, $low) -{ - $isNeg = $high < 0; - if ($isNeg) { - $high = ~$high; - $low = ~$low; - $low++; - if (!$low) { - $high++; - } - } - $result = bcadd(bcmul($high, 4294967296), $low); - if ($low < 0) { - $result = bcadd($result, 4294967296); - } - if ($isNeg) { - $result = bcsub(0, $result); - } - return $result; -} - class InputStream { @@ -192,7 +171,7 @@ class InputStream $count += 1; } while ($b & 0x80); - $var = combineInt32ToInt64($high, $low); + $var = GPBUtil::combineInt32ToInt64($high, $low); } else { $result = 0; $shift = 0; @@ -265,7 +244,7 @@ class InputStream } $high = unpack('V', $data)[1]; if (PHP_INT_SIZE == 4) { - $var = combineInt32ToInt64($high, $low); + $var = GPBUtil::combineInt32ToInt64($high, $low); } else { $var = ($high << 32) | $low; } diff --git a/php/src/Google/Protobuf/Internal/MapField.php b/php/src/Google/Protobuf/Internal/MapField.php index 55cc12ce..12f09d61 100644 --- a/php/src/Google/Protobuf/Internal/MapField.php +++ b/php/src/Google/Protobuf/Internal/MapField.php @@ -38,131 +38,6 @@ namespace Google\Protobuf\Internal; /** - * MapFieldIter is used to iterate MapField. It is also need for the foreach - * syntax. - */ -class MapFieldIter implements \Iterator -{ - - /** - * @ignore - */ - private $container; - - /** - * Create iterator instance for MapField. - * - * @param MapField The MapField instance for which this iterator is - * created. - * @ignore - */ - public function __construct($container) - { - $this->container = $container; - } - - /** - * Reset the status of the iterator - * - * @return void - */ - public function rewind() - { - return reset($this->container); - } - - /** - * Return the element at the current position. - * - * @return object The element at the current position. - */ - public function current() - { - return current($this->container); - } - - /** - * Return the current key. - * - * @return object The current key. - */ - public function key() - { - return key($this->container); - } - - /** - * Move to the next position. - * - * @return void - */ - public function next() - { - return next($this->container); - } - - /** - * Check whether there are more elements to iterate. - * - * @return bool True if there are more elements to iterate. - */ - public function valid() - { - return key($this->container) !== null; - } -} - -/** - * @ignore - */ -function checkKey($key_type, &$key) -{ - switch ($key_type) { - case GPBType::INT32: - GPBUtil::checkInt32($key); - break; - case GPBType::UINT32: - GPBUtil::checkUint32($key); - break; - case GPBType::INT64: - GPBUtil::checkInt64($key); - break; - case GPBType::UINT64: - GPBUtil::checkUint64($key); - break; - case GPBType::FIXED64: - GPBUtil::checkUint64($key); - break; - case GPBType::FIXED32: - GPBUtil::checkUint32($key); - break; - case GPBType::SFIXED64: - GPBUtil::checkInt64($key); - break; - case GPBType::SFIXED32: - GPBUtil::checkInt32($key); - break; - case GPBType::SINT64: - GPBUtil::checkInt64($key); - break; - case GPBType::SINT32: - GPBUtil::checkInt32($key); - break; - case GPBType::BOOL: - GPBUtil::checkBool($key); - break; - case GPBType::STRING: - GPBUtil::checkString($key, true); - break; - default: - trigger_error( - "Given type cannot be map key.", - E_USER_ERROR); - break; - } -} - -/** * MapField is used by generated protocol message classes to manipulate map * fields. It can be used like native PHP array. */ @@ -255,7 +130,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetSet($key, $value) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); switch ($this->value_type) { case GPBType::INT32: @@ -306,7 +181,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetUnset($key) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); unset($this->container[$key]); } @@ -321,7 +196,7 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable */ public function offsetExists($key) { - checkKey($this->key_type, $key); + $this->checkKey($this->key_type, $key); return isset($this->container[$key]); } @@ -344,4 +219,54 @@ class MapField implements \ArrayAccess, \IteratorAggregate, \Countable { return count($this->container); } + + /** + * @ignore + */ + private function checkKey($key_type, &$key) + { + switch ($key_type) { + case GPBType::INT32: + GPBUtil::checkInt32($key); + break; + case GPBType::UINT32: + GPBUtil::checkUint32($key); + break; + case GPBType::INT64: + GPBUtil::checkInt64($key); + break; + case GPBType::UINT64: + GPBUtil::checkUint64($key); + break; + case GPBType::FIXED64: + GPBUtil::checkUint64($key); + break; + case GPBType::FIXED32: + GPBUtil::checkUint32($key); + break; + case GPBType::SFIXED64: + GPBUtil::checkInt64($key); + break; + case GPBType::SFIXED32: + GPBUtil::checkInt32($key); + break; + case GPBType::SINT64: + GPBUtil::checkInt64($key); + break; + case GPBType::SINT32: + GPBUtil::checkInt32($key); + break; + case GPBType::BOOL: + GPBUtil::checkBool($key); + break; + case GPBType::STRING: + GPBUtil::checkString($key, true); + break; + default: + trigger_error( + "Given type cannot be map key.", + E_USER_ERROR); + break; + } + } } diff --git a/php/src/Google/Protobuf/Internal/MapFieldIter.php b/php/src/Google/Protobuf/Internal/MapFieldIter.php new file mode 100644 index 00000000..a0388d92 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/MapFieldIter.php @@ -0,0 +1,113 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/** + * MapField and MapFieldIter are used by generated protocol message classes to + * manipulate map fields. + */ + +namespace Google\Protobuf\Internal; + +/** + * MapFieldIter is used to iterate MapField. It is also need for the foreach + * syntax. + */ +class MapFieldIter implements \Iterator +{ + + /** + * @ignore + */ + private $container; + + /** + * Create iterator instance for MapField. + * + * @param MapField The MapField instance for which this iterator is + * created. + * @ignore + */ + public function __construct($container) + { + $this->container = $container; + } + + /** + * Reset the status of the iterator + * + * @return void + */ + public function rewind() + { + return reset($this->container); + } + + /** + * Return the element at the current position. + * + * @return object The element at the current position. + */ + public function current() + { + return current($this->container); + } + + /** + * Return the current key. + * + * @return object The current key. + */ + public function key() + { + return key($this->container); + } + + /** + * Move to the next position. + * + * @return void + */ + public function next() + { + return next($this->container); + } + + /** + * Check whether there are more elements to iterate. + * + * @return bool True if there are more elements to iterate. + */ + public function valid() + { + return key($this->container) !== null; + } +}
\ No newline at end of file diff --git a/php/src/Google/Protobuf/Internal/Message.php b/php/src/Google/Protobuf/Internal/Message.php index cd15e0f0..9ba249a0 100644 --- a/php/src/Google/Protobuf/Internal/Message.php +++ b/php/src/Google/Protobuf/Internal/Message.php @@ -373,7 +373,7 @@ class Message $getter = $field->getGetter(); while ($input->bytesUntilLimit() > 0) { self::parseFieldFromStreamNoTag($input, $field, $value); - $this->$getter()[] = $value; + $this->appendHelper($field, $value); } $input->popLimit($limit); return; @@ -382,11 +382,9 @@ class Message } if ($field->isMap()) { - $getter = $field->getGetter(); - $this->$getter()[$value->getKey()] = $value->getValue(); + $this->kvUpdateHelper($field, $value->getKey(), $value->getValue()); } else if ($field->isRepeated()) { - $getter = $field->getGetter(); - $this->$getter()[] = $value; + $this->appendHelper($field, $value); } else { $setter = $field->getSetter(); $this->$setter($value); @@ -533,9 +531,10 @@ class Message $klass = $value_field->getMessageType()->getClass(); $copy = new $klass; $copy->mergeFrom($value); - $this->$getter()[$key] = $copy; + + $this->kvUpdateHelper($field, $key, $copy); } else { - $this->$getter()[$key] = $value; + $this->kvUpdateHelper($field, $key, $value); } } } @@ -546,9 +545,9 @@ class Message $klass = $field->getMessageType()->getClass(); $copy = new $klass; $copy->mergeFrom($tmp); - $this->$getter()[] = $copy; + $this->appendHelper($field, $copy); } else { - $this->$getter()[] = $tmp; + $this->appendHelper($field, $tmp); } } } @@ -739,6 +738,13 @@ class Message */ private function existField($field) { + $oneof_index = $field->getOneofIndex(); + if ($oneof_index !== -1) { + $oneof = $this->desc->getOneofDecl()[$oneof_index]; + $oneof_name = $oneof->getName(); + return $this->$oneof_name->getNumber() === $field->getNumber(); + } + $getter = $field->getGetter(); $value = $this->$getter(); return $value !== $this->defaultValue($field); @@ -889,4 +895,30 @@ class Message } return $size; } + + private function appendHelper($field, $append_value) + { + $getter = $field->getGetter(); + $setter = $field->getSetter(); + + $field_arr_value = $this->$getter(); + $field_arr_value[] = $append_value; + + if (!is_object($field_arr_value)) { + $this->$setter($field_arr_value); + } + } + + private function kvUpdateHelper($field, $update_key, $update_value) + { + $getter = $field->getGetter(); + $setter = $field->getSetter(); + + $field_arr_value = $this->$getter(); + $field_arr_value[$update_key] = $update_value; + + if (!is_object($field_arr_value)) { + $this->$setter($field_arr_value); + } + } } diff --git a/php/src/Google/Protobuf/Internal/MessageOptions.php b/php/src/Google/Protobuf/Internal/MessageOptions.php index 747f3294..a5835358 100644 --- a/php/src/Google/Protobuf/Internal/MessageOptions.php +++ b/php/src/Google/Protobuf/Internal/MessageOptions.php @@ -155,6 +155,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->message_set_wire_format = $var; $this->has_message_set_wire_format = true; + + return $this; } public function hasMessageSetWireFormat() @@ -190,6 +192,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->no_standard_descriptor_accessor = $var; $this->has_no_standard_descriptor_accessor = true; + + return $this; } public function hasNoStandardDescriptorAccessor() @@ -227,6 +231,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -292,6 +298,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->map_entry = $var; $this->has_map_entry = true; + + return $this; } public function hasMapEntry() @@ -323,6 +331,8 @@ class MessageOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php index 3d8df7af..c3f9f064 100644 --- a/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/MethodDescriptorProto.php @@ -85,6 +85,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -118,6 +120,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->input_type = $var; $this->has_input_type = true; + + return $this; } public function hasInputType() @@ -141,6 +145,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->output_type = $var; $this->has_output_type = true; + + return $this; } public function hasOutputType() @@ -164,6 +170,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\MethodOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() @@ -195,6 +203,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->client_streaming = $var; $this->has_client_streaming = true; + + return $this; } public function hasClientStreaming() @@ -226,6 +236,8 @@ class MethodDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->server_streaming = $var; $this->has_server_streaming = true; + + return $this; } public function hasServerStreaming() diff --git a/php/src/Google/Protobuf/Internal/MethodOptions.php b/php/src/Google/Protobuf/Internal/MethodOptions.php index 6dca5854..e8d36d3c 100644 --- a/php/src/Google/Protobuf/Internal/MethodOptions.php +++ b/php/src/Google/Protobuf/Internal/MethodOptions.php @@ -78,6 +78,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -101,6 +103,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkEnum($var, \Google\Protobuf\Internal\MethodOptions_IdempotencyLevel::class); $this->idempotency_level = $var; $this->has_idempotency_level = true; + + return $this; } public function hasIdempotencyLevel() @@ -132,6 +136,8 @@ class MethodOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptor.php b/php/src/Google/Protobuf/Internal/OneofDescriptor.php new file mode 100644 index 00000000..04988737 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/OneofDescriptor.php @@ -0,0 +1,67 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +namespace Google\Protobuf\Internal; + +class OneofDescriptor +{ + + private $name; + private $fields; + + public function setName($name) + { + $this->name = $name; + } + + public function getName() + { + return $this->name; + } + + public function addField(&$field) + { + $this->fields[] = $field; + } + + public function getFields() + { + return $this->fields; + } + + public static function buildFromProto($oneof_proto) + { + $oneof = new OneofDescriptor(); + $oneof->setName($oneof_proto->getName()); + return $oneof; + } +} diff --git a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php index e5fbe370..744ca638 100644 --- a/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/OneofDescriptorProto.php @@ -52,6 +52,8 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -75,6 +77,8 @@ class OneofDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\OneofOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() diff --git a/php/src/Google/Protobuf/Internal/OneofOptions.php b/php/src/Google/Protobuf/Internal/OneofOptions.php index b61325d2..b3cd51ed 100644 --- a/php/src/Google/Protobuf/Internal/OneofOptions.php +++ b/php/src/Google/Protobuf/Internal/OneofOptions.php @@ -55,6 +55,8 @@ class OneofOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php index 2ad4709a..797b3b3a 100644 --- a/php/src/Google/Protobuf/Internal/RepeatedField.php +++ b/php/src/Google/Protobuf/Internal/RepeatedField.php @@ -41,86 +41,6 @@ use Google\Protobuf\Internal\GPBType; use Google\Protobuf\Internal\GPBUtil; /** - * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the - * foreach syntax. - */ -class RepeatedFieldIter implements \Iterator -{ - - /** - * @ignore - */ - private $position; - /** - * @ignore - */ - private $container; - - /** - * Create iterator instance for RepeatedField. - * - * @param RepeatedField The RepeatedField instance for which this iterator - * is created. - * @ignore - */ - public function __construct($container) - { - $this->position = 0; - $this->container = $container; - } - - /** - * Reset the status of the iterator - * - * @return void - */ - public function rewind() - { - $this->position = 0; - } - - /** - * Return the element at the current position. - * - * @return object The element at the current position. - */ - public function current() - { - return $this->container[$this->position]; - } - - /** - * Return the current position. - * - * @return integer The current position. - */ - public function key() - { - return $this->position; - } - - /** - * Move to the next position. - * - * @return void - */ - public function next() - { - ++$this->position; - } - - /** - * Check whether there are more elements to iterate. - * - * @return bool True if there are more elements to iterate. - */ - public function valid() - { - return isset($this->container[$this->position]); - } -} - -/** * RepeatedField is used by generated protocol message classes to manipulate * repeated fields. It can be used like native PHP array. */ diff --git a/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php new file mode 100644 index 00000000..2b6f8230 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/RepeatedFieldIter.php @@ -0,0 +1,118 @@ +<?php + +// Protocol Buffers - Google's data interchange format +// Copyright 2008 Google Inc. All rights reserved. +// https://developers.google.com/protocol-buffers/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +/** + * RepeatedField and RepeatedFieldIter are used by generated protocol message + * classes to manipulate repeated fields. + */ + +namespace Google\Protobuf\Internal; + +/** + * RepeatedFieldIter is used to iterate RepeatedField. It is also need for the + * foreach syntax. + */ +class RepeatedFieldIter implements \Iterator +{ + + /** + * @ignore + */ + private $position; + /** + * @ignore + */ + private $container; + + /** + * Create iterator instance for RepeatedField. + * + * @param RepeatedField The RepeatedField instance for which this iterator + * is created. + * @ignore + */ + public function __construct($container) + { + $this->position = 0; + $this->container = $container; + } + + /** + * Reset the status of the iterator + * + * @return void + */ + public function rewind() + { + $this->position = 0; + } + + /** + * Return the element at the current position. + * + * @return object The element at the current position. + */ + public function current() + { + return $this->container[$this->position]; + } + + /** + * Return the current position. + * + * @return integer The current position. + */ + public function key() + { + return $this->position; + } + + /** + * Move to the next position. + * + * @return void + */ + public function next() + { + ++$this->position; + } + + /** + * Check whether there are more elements to iterate. + * + * @return bool True if there are more elements to iterate. + */ + public function valid() + { + return isset($this->container[$this->position]); + } +} diff --git a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php index 47776202..7c85728a 100644 --- a/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php +++ b/php/src/Google/Protobuf/Internal/ServiceDescriptorProto.php @@ -57,6 +57,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name = $var; $this->has_name = true; + + return $this; } public function hasName() @@ -80,6 +82,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\MethodDescriptorProto::class); $this->method = $arr; $this->has_method = true; + + return $this; } public function hasMethod() @@ -103,6 +107,8 @@ class ServiceDescriptorProto extends \Google\Protobuf\Internal\Message GPBUtil::checkMessage($var, \Google\Protobuf\Internal\ServiceOptions::class); $this->options = $var; $this->has_options = true; + + return $this; } public function hasOptions() diff --git a/php/src/Google/Protobuf/Internal/ServiceOptions.php b/php/src/Google/Protobuf/Internal/ServiceOptions.php index 62323dba..0f3a8d74 100644 --- a/php/src/Google/Protobuf/Internal/ServiceOptions.php +++ b/php/src/Google/Protobuf/Internal/ServiceOptions.php @@ -73,6 +73,8 @@ class ServiceOptions extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->deprecated = $var; $this->has_deprecated = true; + + return $this; } public function hasDeprecated() @@ -104,6 +106,8 @@ class ServiceOptions extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption::class); $this->uninterpreted_option = $arr; $this->has_uninterpreted_option = true; + + return $this; } public function hasUninterpretedOption() diff --git a/php/src/Google/Protobuf/Internal/SourceCodeInfo.php b/php/src/Google/Protobuf/Internal/SourceCodeInfo.php index eab60880..7eef3424 100644 --- a/php/src/Google/Protobuf/Internal/SourceCodeInfo.php +++ b/php/src/Google/Protobuf/Internal/SourceCodeInfo.php @@ -180,6 +180,8 @@ class SourceCodeInfo extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\SourceCodeInfo_Location::class); $this->location = $arr; $this->has_location = true; + + return $this; } public function hasLocation() diff --git a/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php b/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php index 5a02b26c..cf23e38b 100644 --- a/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php +++ b/php/src/Google/Protobuf/Internal/SourceCodeInfo_Location.php @@ -182,6 +182,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32); $this->path = $arr; $this->has_path = true; + + return $this; } public function hasPath() @@ -221,6 +223,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::INT32); $this->span = $arr; $this->has_span = true; + + return $this; } public function hasSpan() @@ -322,6 +326,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->leading_comments = $var; $this->has_leading_comments = true; + + return $this; } public function hasLeadingComments() @@ -345,6 +351,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->trailing_comments = $var; $this->has_trailing_comments = true; + + return $this; } public function hasTrailingComments() @@ -368,6 +376,8 @@ class SourceCodeInfo_Location extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::STRING); $this->leading_detached_comments = $arr; $this->has_leading_detached_comments = true; + + return $this; } public function hasLeadingDetachedComments() diff --git a/php/src/Google/Protobuf/Internal/UninterpretedOption.php b/php/src/Google/Protobuf/Internal/UninterpretedOption.php index 28655019..08e071d4 100644 --- a/php/src/Google/Protobuf/Internal/UninterpretedOption.php +++ b/php/src/Google/Protobuf/Internal/UninterpretedOption.php @@ -87,6 +87,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message $arr = GPBUtil::checkRepeatedField($var, \Google\Protobuf\Internal\GPBType::MESSAGE, \Google\Protobuf\Internal\UninterpretedOption_NamePart::class); $this->name = $arr; $this->has_name = true; + + return $this; } public function hasName() @@ -120,6 +122,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->identifier_value = $var; $this->has_identifier_value = true; + + return $this; } public function hasIdentifierValue() @@ -143,6 +147,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkUint64($var); $this->positive_int_value = $var; $this->has_positive_int_value = true; + + return $this; } public function hasPositiveIntValue() @@ -166,6 +172,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkInt64($var); $this->negative_int_value = $var; $this->has_negative_int_value = true; + + return $this; } public function hasNegativeIntValue() @@ -189,6 +197,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkDouble($var); $this->double_value = $var; $this->has_double_value = true; + + return $this; } public function hasDoubleValue() @@ -212,6 +222,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, False); $this->string_value = $var; $this->has_string_value = true; + + return $this; } public function hasStringValue() @@ -235,6 +247,8 @@ class UninterpretedOption extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->aggregate_value = $var; $this->has_aggregate_value = true; + + return $this; } public function hasAggregateValue() diff --git a/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php b/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php index 86484d23..eb27754c 100644 --- a/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php +++ b/php/src/Google/Protobuf/Internal/UninterpretedOption_NamePart.php @@ -56,6 +56,8 @@ class UninterpretedOption_NamePart extends \Google\Protobuf\Internal\Message GPBUtil::checkString($var, True); $this->name_part = $var; $this->has_name_part = true; + + return $this; } public function hasNamePart() @@ -79,6 +81,8 @@ class UninterpretedOption_NamePart extends \Google\Protobuf\Internal\Message GPBUtil::checkBool($var); $this->is_extension = $var; $this->has_is_extension = true; + + return $this; } public function hasIsExtension() diff --git a/php/src/Google/Protobuf/descriptor.php b/php/src/Google/Protobuf/descriptor.php deleted file mode 100644 index fb69eda0..00000000 --- a/php/src/Google/Protobuf/descriptor.php +++ /dev/null @@ -1,587 +0,0 @@ -<?php - -// Protocol Buffers - Google's data interchange format -// Copyright 2008 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -namespace Google\Protobuf\Internal; - -use Google\Protobuf\Internal\GPBType; -use Google\Protobuf\Internal\MessageOptions; - -class FileDescriptor -{ - - private $package; - private $message_type = []; - private $enum_type = []; - - public function setPackage($package) - { - $this->package = $package; - } - - public function getPackage() - { - return $this->package; - } - - public function getMessageType() - { - return $this->message_type; - } - - public function addMessageType($desc) - { - $this->message_type[] = $desc; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function addEnumType($desc) - { - $this->enum_type[]= $desc; - } - - public static function buildFromProto($proto) - { - $file = new FileDescriptor(); - $file->setPackage($proto->getPackage()); - foreach ($proto->getMessageType() as $message_proto) { - $file->addMessageType(Descriptor::buildFromProto( - $message_proto, $proto, "")); - } - foreach ($proto->getEnumType() as $enum_proto) { - $file->getEnumType()[] = - $file->addEnumType( - EnumDescriptor::buildFromProto( - $enum_proto, - $proto, - "")); - } - return $file; - } -} - -class Descriptor -{ - - private $full_name; - private $field = []; - private $nested_type = []; - private $enum_type = []; - private $klass; - private $options; - private $oneof_decl = []; - - public function addOneofDecl($oneof) - { - $this->oneof_decl[] = $oneof; - } - - public function getOneofDecl() - { - return $this->oneof_decl; - } - - public function setFullName($full_name) - { - $this->full_name = $full_name; - } - - public function getFullName() - { - return $this->full_name; - } - - public function addField($field) - { - $this->field[$field->getNumber()] = $field; - } - - public function getField() - { - return $this->field; - } - - public function addNestedType($desc) - { - $this->nested_type[] = $desc; - } - - public function getNestedType() - { - return $this->nested_type; - } - - public function addEnumType($desc) - { - $this->enum_type[] = $desc; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function getFieldByNumber($number) - { - if (!isset($this->field[$number])) { - return NULL; - } else { - return $this->field[$number]; - } - } - - public function setClass($klass) - { - $this->klass = $klass; - } - - public function getClass() - { - return $this->klass; - } - - public function setOptions($options) - { - $this->options = $options; - } - - public function getOptions() - { - return $this->options; - } - - public static function buildFromProto($proto, $file_proto, $containing) - { - $desc = new Descriptor(); - - $message_name_without_package = ""; - $classname = ""; - $fullname = ""; - getFullClassName( - $proto, - $containing, - $file_proto, - $message_name_without_package, - $classname, - $fullname); - $desc->setFullName($fullname); - $desc->setClass($classname); - $desc->setOptions($proto->getOptions()); - - foreach ($proto->getField() as $field_proto) { - $desc->addField(FieldDescriptor::buildFromProto($field_proto)); - } - - // Handle nested types. - foreach ($proto->getNestedType() as $nested_proto) { - $desc->addNestedType(Descriptor::buildFromProto( - $nested_proto, $file_proto, $message_name_without_package)); - } - - // Handle nested enum. - foreach ($proto->getEnumType() as $enum_proto) { - $desc->addEnumType(EnumDescriptor::buildFromProto( - $enum_proto, $file_proto, $message_name_without_package)); - } - - // Handle oneof fields. - foreach ($proto->getOneofDecl() as $oneof_proto) { - $desc->addOneofDecl( - OneofDescriptor::buildFromProto($oneof_proto, $desc)); - } - - return $desc; - } -} - -function getClassNamePrefix( - $classname, - $file_proto) -{ - $option = $file_proto->getOptions(); - $prefix = is_null($option) ? "" : $option->getPhpClassPrefix(); - if ($prefix !== "") { - return $prefix; - } - - $reserved_words = array("Empty"); - foreach ($reserved_words as $reserved_word) { - if ($classname === $reserved_word) { - if ($file_proto->getPackage() === "google.protobuf") { - return "GPB"; - } else { - return "PB"; - } - } - } - - return ""; -} - -function getClassNameWithoutPackage( - $name, - $file_proto) -{ - $classname = implode('_', array_map('ucwords', explode('.', $name))); - return getClassNamePrefix($classname, $file_proto) . $classname; -} - -function getFullClassName( - $proto, - $containing, - $file_proto, - &$message_name_without_package, - &$classname, - &$fullname) -{ - // Full name needs to start with '.'. - $message_name_without_package = $proto->getName(); - if ($containing !== "") { - $message_name_without_package = - $containing . "." . $message_name_without_package; - } - - $package = $file_proto->getPackage(); - if ($package === "") { - $fullname = "." . $message_name_without_package; - } else { - $fullname = "." . $package . "." . $message_name_without_package; - } - - $class_name_without_package = - getClassNameWithoutPackage($message_name_without_package, $file_proto); - if ($package === "") { - $classname = $class_name_without_package; - } else { - $classname = - implode('\\', array_map('ucwords', explode('.', $package))). - "\\".$class_name_without_package; - } -} - -class OneofDescriptor -{ - - private $name; - private $fields; - - public function setName($name) - { - $this->name = $name; - } - - public function getName() - { - return $this->name; - } - - public function addField(&$field) - { - $this->fields[] = $field; - } - - public function getFields() - { - return $this->fields; - } - - public static function buildFromProto($oneof_proto) - { - $oneof = new OneofDescriptor(); - $oneof->setName($oneof_proto->getName()); - return $oneof; - } -} - - -class EnumDescriptor -{ - - private $klass; - private $full_name; - private $value; - - public function setFullName($full_name) - { - $this->full_name = $full_name; - } - - public function getFullName() - { - return $this->full_name; - } - - public function addValue($number, $value) - { - $this->value[$number] = $value; - } - - public function setClass($klass) - { - $this->klass = $klass; - } - - public function getClass() - { - return $this->klass; - } - - public static function buildFromProto($proto, $file_proto, $containing) - { - $desc = new EnumDescriptor(); - - $enum_name_without_package = ""; - $classname = ""; - $fullname = ""; - getFullClassName( - $proto, - $containing, - $file_proto, - $enum_name_without_package, - $classname, - $fullname); - $desc->setFullName($fullname); - $desc->setClass($classname); - - return $desc; - } -} - -class EnumValueDescriptor -{ -} - -class FieldDescriptor -{ - - private $name; - private $setter; - private $getter; - private $number; - private $label; - private $type; - private $message_type; - private $enum_type; - private $packed; - private $is_map; - private $oneof_index = -1; - - public function setOneofIndex($index) - { - $this->oneof_index = $index; - } - - public function getOneofIndex() - { - return $this->oneof_index; - } - - public function setName($name) - { - $this->name = $name; - } - - public function getName() - { - return $this->name; - } - - public function setSetter($setter) - { - $this->setter = $setter; - } - - public function getSetter() - { - return $this->setter; - } - - public function setGetter($getter) - { - $this->getter = $getter; - } - - public function getGetter() - { - return $this->getter; - } - - public function setNumber($number) - { - $this->number = $number; - } - - public function getNumber() - { - return $this->number; - } - - public function setLabel($label) - { - $this->label = $label; - } - - public function getLabel() - { - return $this->label; - } - - public function isRepeated() - { - return $this->label === GPBLabel::REPEATED; - } - - public function setType($type) - { - $this->type = $type; - } - - public function getType() - { - return $this->type; - } - - public function setMessageType($message_type) - { - $this->message_type = $message_type; - } - - public function getMessageType() - { - return $this->message_type; - } - - public function setEnumType($enum_type) - { - $this->enum_type = $enum_type; - } - - public function getEnumType() - { - return $this->enum_type; - } - - public function setPacked($packed) - { - $this->packed = $packed; - } - - public function getPacked() - { - return $this->packed; - } - - public function isPackable() - { - return $this->isRepeated() && self::isTypePackable($this->type); - } - - public function isMap() - { - return $this->getType() == GPBType::MESSAGE && - !is_null($this->getMessageType()->getOptions()) && - $this->getMessageType()->getOptions()->getMapEntry(); - } - - private static function isTypePackable($field_type) - { - return ($field_type !== GPBType::STRING && - $field_type !== GPBType::GROUP && - $field_type !== GPBType::MESSAGE && - $field_type !== GPBType::BYTES); - } - - public static function getFieldDescriptor( - $name, - $label, - $type, - $number, - $oneof_index, - $packed, - $type_name = null) - { - $field = new FieldDescriptor(); - $field->setName($name); - $camel_name = implode('', array_map('ucwords', explode('_', $name))); - $field->setGetter('get' . $camel_name); - $field->setSetter('set' . $camel_name); - $field->setType($type); - $field->setNumber($number); - $field->setLabel($label); - $field->setPacked($packed); - $field->setOneofIndex($oneof_index); - - // At this time, the message/enum type may have not been added to pool. - // So we use the type name as place holder and will replace it with the - // actual descriptor in cross building. - switch ($type) { - case GPBType::MESSAGE: - $field->setMessageType($type_name); - break; - case GPBType::ENUM: - $field->setEnumType($type_name); - break; - default: - break; - } - - return $field; - } - - public static function buildFromProto($proto) - { - $type_name = null; - switch ($proto->getType()) { - case GPBType::MESSAGE: - case GPBType::GROUP: - case GPBType::ENUM: - $type_name = $proto->getTypeName(); - break; - default: - break; - } - - $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; - $packed = false; - $options = $proto->getOptions(); - if ($options !== null) { - $packed = $options->getPacked(); - } - - return FieldDescriptor::getFieldDescriptor( - $proto->getName(), $proto->getLabel(), $proto->getType(), - $proto->getNumber(), $oneof_index, $packed, $type_name); - } -} |