1ffe3c632Sopenharmony_ci<?php 2ffe3c632Sopenharmony_ci 3ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format 4ffe3c632Sopenharmony_ci// Copyright 2008 Google Inc. All rights reserved. 5ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/ 6ffe3c632Sopenharmony_ci// 7ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without 8ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are 9ffe3c632Sopenharmony_ci// met: 10ffe3c632Sopenharmony_ci// 11ffe3c632Sopenharmony_ci// * Redistributions of source code must retain the above copyright 12ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer. 13ffe3c632Sopenharmony_ci// * Redistributions in binary form must reproduce the above 14ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer 15ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the 16ffe3c632Sopenharmony_ci// distribution. 17ffe3c632Sopenharmony_ci// * Neither the name of Google Inc. nor the names of its 18ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from 19ffe3c632Sopenharmony_ci// this software without specific prior written permission. 20ffe3c632Sopenharmony_ci// 21ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32ffe3c632Sopenharmony_ci 33ffe3c632Sopenharmony_cinamespace Google\Protobuf\Internal; 34ffe3c632Sopenharmony_ci 35ffe3c632Sopenharmony_ciclass FieldDescriptor 36ffe3c632Sopenharmony_ci{ 37ffe3c632Sopenharmony_ci use HasPublicDescriptorTrait; 38ffe3c632Sopenharmony_ci 39ffe3c632Sopenharmony_ci private $name; 40ffe3c632Sopenharmony_ci private $json_name; 41ffe3c632Sopenharmony_ci private $setter; 42ffe3c632Sopenharmony_ci private $getter; 43ffe3c632Sopenharmony_ci private $number; 44ffe3c632Sopenharmony_ci private $label; 45ffe3c632Sopenharmony_ci private $type; 46ffe3c632Sopenharmony_ci private $message_type; 47ffe3c632Sopenharmony_ci private $enum_type; 48ffe3c632Sopenharmony_ci private $packed; 49ffe3c632Sopenharmony_ci private $is_map; 50ffe3c632Sopenharmony_ci private $oneof_index = -1; 51ffe3c632Sopenharmony_ci 52ffe3c632Sopenharmony_ci public function __construct() 53ffe3c632Sopenharmony_ci { 54ffe3c632Sopenharmony_ci $this->public_desc = new \Google\Protobuf\FieldDescriptor($this); 55ffe3c632Sopenharmony_ci } 56ffe3c632Sopenharmony_ci 57ffe3c632Sopenharmony_ci public function setOneofIndex($index) 58ffe3c632Sopenharmony_ci { 59ffe3c632Sopenharmony_ci $this->oneof_index = $index; 60ffe3c632Sopenharmony_ci } 61ffe3c632Sopenharmony_ci 62ffe3c632Sopenharmony_ci public function getOneofIndex() 63ffe3c632Sopenharmony_ci { 64ffe3c632Sopenharmony_ci return $this->oneof_index; 65ffe3c632Sopenharmony_ci } 66ffe3c632Sopenharmony_ci 67ffe3c632Sopenharmony_ci public function setName($name) 68ffe3c632Sopenharmony_ci { 69ffe3c632Sopenharmony_ci $this->name = $name; 70ffe3c632Sopenharmony_ci } 71ffe3c632Sopenharmony_ci 72ffe3c632Sopenharmony_ci public function getName() 73ffe3c632Sopenharmony_ci { 74ffe3c632Sopenharmony_ci return $this->name; 75ffe3c632Sopenharmony_ci } 76ffe3c632Sopenharmony_ci 77ffe3c632Sopenharmony_ci public function setJsonName($json_name) 78ffe3c632Sopenharmony_ci { 79ffe3c632Sopenharmony_ci $this->json_name = $json_name; 80ffe3c632Sopenharmony_ci } 81ffe3c632Sopenharmony_ci 82ffe3c632Sopenharmony_ci public function getJsonName() 83ffe3c632Sopenharmony_ci { 84ffe3c632Sopenharmony_ci return $this->json_name; 85ffe3c632Sopenharmony_ci } 86ffe3c632Sopenharmony_ci 87ffe3c632Sopenharmony_ci public function setSetter($setter) 88ffe3c632Sopenharmony_ci { 89ffe3c632Sopenharmony_ci $this->setter = $setter; 90ffe3c632Sopenharmony_ci } 91ffe3c632Sopenharmony_ci 92ffe3c632Sopenharmony_ci public function getSetter() 93ffe3c632Sopenharmony_ci { 94ffe3c632Sopenharmony_ci return $this->setter; 95ffe3c632Sopenharmony_ci } 96ffe3c632Sopenharmony_ci 97ffe3c632Sopenharmony_ci public function setGetter($getter) 98ffe3c632Sopenharmony_ci { 99ffe3c632Sopenharmony_ci $this->getter = $getter; 100ffe3c632Sopenharmony_ci } 101ffe3c632Sopenharmony_ci 102ffe3c632Sopenharmony_ci public function getGetter() 103ffe3c632Sopenharmony_ci { 104ffe3c632Sopenharmony_ci return $this->getter; 105ffe3c632Sopenharmony_ci } 106ffe3c632Sopenharmony_ci 107ffe3c632Sopenharmony_ci public function setNumber($number) 108ffe3c632Sopenharmony_ci { 109ffe3c632Sopenharmony_ci $this->number = $number; 110ffe3c632Sopenharmony_ci } 111ffe3c632Sopenharmony_ci 112ffe3c632Sopenharmony_ci public function getNumber() 113ffe3c632Sopenharmony_ci { 114ffe3c632Sopenharmony_ci return $this->number; 115ffe3c632Sopenharmony_ci } 116ffe3c632Sopenharmony_ci 117ffe3c632Sopenharmony_ci public function setLabel($label) 118ffe3c632Sopenharmony_ci { 119ffe3c632Sopenharmony_ci $this->label = $label; 120ffe3c632Sopenharmony_ci } 121ffe3c632Sopenharmony_ci 122ffe3c632Sopenharmony_ci public function getLabel() 123ffe3c632Sopenharmony_ci { 124ffe3c632Sopenharmony_ci return $this->label; 125ffe3c632Sopenharmony_ci } 126ffe3c632Sopenharmony_ci 127ffe3c632Sopenharmony_ci public function isRepeated() 128ffe3c632Sopenharmony_ci { 129ffe3c632Sopenharmony_ci return $this->label === GPBLabel::REPEATED; 130ffe3c632Sopenharmony_ci } 131ffe3c632Sopenharmony_ci 132ffe3c632Sopenharmony_ci public function setType($type) 133ffe3c632Sopenharmony_ci { 134ffe3c632Sopenharmony_ci $this->type = $type; 135ffe3c632Sopenharmony_ci } 136ffe3c632Sopenharmony_ci 137ffe3c632Sopenharmony_ci public function getType() 138ffe3c632Sopenharmony_ci { 139ffe3c632Sopenharmony_ci return $this->type; 140ffe3c632Sopenharmony_ci } 141ffe3c632Sopenharmony_ci 142ffe3c632Sopenharmony_ci public function setMessageType($message_type) 143ffe3c632Sopenharmony_ci { 144ffe3c632Sopenharmony_ci $this->message_type = $message_type; 145ffe3c632Sopenharmony_ci } 146ffe3c632Sopenharmony_ci 147ffe3c632Sopenharmony_ci public function getMessageType() 148ffe3c632Sopenharmony_ci { 149ffe3c632Sopenharmony_ci return $this->message_type; 150ffe3c632Sopenharmony_ci } 151ffe3c632Sopenharmony_ci 152ffe3c632Sopenharmony_ci public function setEnumType($enum_type) 153ffe3c632Sopenharmony_ci { 154ffe3c632Sopenharmony_ci $this->enum_type = $enum_type; 155ffe3c632Sopenharmony_ci } 156ffe3c632Sopenharmony_ci 157ffe3c632Sopenharmony_ci public function getEnumType() 158ffe3c632Sopenharmony_ci { 159ffe3c632Sopenharmony_ci return $this->enum_type; 160ffe3c632Sopenharmony_ci } 161ffe3c632Sopenharmony_ci 162ffe3c632Sopenharmony_ci public function setPacked($packed) 163ffe3c632Sopenharmony_ci { 164ffe3c632Sopenharmony_ci $this->packed = $packed; 165ffe3c632Sopenharmony_ci } 166ffe3c632Sopenharmony_ci 167ffe3c632Sopenharmony_ci public function getPacked() 168ffe3c632Sopenharmony_ci { 169ffe3c632Sopenharmony_ci return $this->packed; 170ffe3c632Sopenharmony_ci } 171ffe3c632Sopenharmony_ci 172ffe3c632Sopenharmony_ci public function isPackable() 173ffe3c632Sopenharmony_ci { 174ffe3c632Sopenharmony_ci return $this->isRepeated() && self::isTypePackable($this->type); 175ffe3c632Sopenharmony_ci } 176ffe3c632Sopenharmony_ci 177ffe3c632Sopenharmony_ci public function isMap() 178ffe3c632Sopenharmony_ci { 179ffe3c632Sopenharmony_ci return $this->getType() == GPBType::MESSAGE && 180ffe3c632Sopenharmony_ci !is_null($this->getMessageType()->getOptions()) && 181ffe3c632Sopenharmony_ci $this->getMessageType()->getOptions()->getMapEntry(); 182ffe3c632Sopenharmony_ci } 183ffe3c632Sopenharmony_ci 184ffe3c632Sopenharmony_ci public function isTimestamp() 185ffe3c632Sopenharmony_ci { 186ffe3c632Sopenharmony_ci return $this->getType() == GPBType::MESSAGE && 187ffe3c632Sopenharmony_ci $this->getMessageType()->getClass() === "Google\Protobuf\Timestamp"; 188ffe3c632Sopenharmony_ci } 189ffe3c632Sopenharmony_ci 190ffe3c632Sopenharmony_ci public function isWrapperType() 191ffe3c632Sopenharmony_ci { 192ffe3c632Sopenharmony_ci if ($this->getType() == GPBType::MESSAGE) { 193ffe3c632Sopenharmony_ci $class = $this->getMessageType()->getClass(); 194ffe3c632Sopenharmony_ci return in_array($class, [ 195ffe3c632Sopenharmony_ci "Google\Protobuf\DoubleValue", 196ffe3c632Sopenharmony_ci "Google\Protobuf\FloatValue", 197ffe3c632Sopenharmony_ci "Google\Protobuf\Int64Value", 198ffe3c632Sopenharmony_ci "Google\Protobuf\UInt64Value", 199ffe3c632Sopenharmony_ci "Google\Protobuf\Int32Value", 200ffe3c632Sopenharmony_ci "Google\Protobuf\UInt32Value", 201ffe3c632Sopenharmony_ci "Google\Protobuf\BoolValue", 202ffe3c632Sopenharmony_ci "Google\Protobuf\StringValue", 203ffe3c632Sopenharmony_ci "Google\Protobuf\BytesValue", 204ffe3c632Sopenharmony_ci ]); 205ffe3c632Sopenharmony_ci } 206ffe3c632Sopenharmony_ci return false; 207ffe3c632Sopenharmony_ci } 208ffe3c632Sopenharmony_ci 209ffe3c632Sopenharmony_ci private static function isTypePackable($field_type) 210ffe3c632Sopenharmony_ci { 211ffe3c632Sopenharmony_ci return ($field_type !== GPBType::STRING && 212ffe3c632Sopenharmony_ci $field_type !== GPBType::GROUP && 213ffe3c632Sopenharmony_ci $field_type !== GPBType::MESSAGE && 214ffe3c632Sopenharmony_ci $field_type !== GPBType::BYTES); 215ffe3c632Sopenharmony_ci } 216ffe3c632Sopenharmony_ci 217ffe3c632Sopenharmony_ci public static function getFieldDescriptor($proto) 218ffe3c632Sopenharmony_ci { 219ffe3c632Sopenharmony_ci $type_name = null; 220ffe3c632Sopenharmony_ci $type = $proto->getType(); 221ffe3c632Sopenharmony_ci switch ($type) { 222ffe3c632Sopenharmony_ci case GPBType::MESSAGE: 223ffe3c632Sopenharmony_ci case GPBType::GROUP: 224ffe3c632Sopenharmony_ci case GPBType::ENUM: 225ffe3c632Sopenharmony_ci $type_name = $proto->getTypeName(); 226ffe3c632Sopenharmony_ci break; 227ffe3c632Sopenharmony_ci default: 228ffe3c632Sopenharmony_ci break; 229ffe3c632Sopenharmony_ci } 230ffe3c632Sopenharmony_ci 231ffe3c632Sopenharmony_ci $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1; 232ffe3c632Sopenharmony_ci // TODO: once proto2 is supported, this default should be false 233ffe3c632Sopenharmony_ci // for proto2. 234ffe3c632Sopenharmony_ci if ($proto->getLabel() === GPBLabel::REPEATED && 235ffe3c632Sopenharmony_ci $proto->getType() !== GPBType::MESSAGE && 236ffe3c632Sopenharmony_ci $proto->getType() !== GPBType::GROUP && 237ffe3c632Sopenharmony_ci $proto->getType() !== GPBType::STRING && 238ffe3c632Sopenharmony_ci $proto->getType() !== GPBType::BYTES) { 239ffe3c632Sopenharmony_ci $packed = true; 240ffe3c632Sopenharmony_ci } else { 241ffe3c632Sopenharmony_ci $packed = false; 242ffe3c632Sopenharmony_ci } 243ffe3c632Sopenharmony_ci $options = $proto->getOptions(); 244ffe3c632Sopenharmony_ci if ($options !== null) { 245ffe3c632Sopenharmony_ci $packed = $options->getPacked(); 246ffe3c632Sopenharmony_ci } 247ffe3c632Sopenharmony_ci 248ffe3c632Sopenharmony_ci $field = new FieldDescriptor(); 249ffe3c632Sopenharmony_ci $field->setName($proto->getName()); 250ffe3c632Sopenharmony_ci 251ffe3c632Sopenharmony_ci $json_name = $proto->hasJsonName() ? $proto->getJsonName() : 252ffe3c632Sopenharmony_ci lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName())))); 253ffe3c632Sopenharmony_ci if ($proto->hasJsonName()) { 254ffe3c632Sopenharmony_ci $json_name = $proto->getJsonName(); 255ffe3c632Sopenharmony_ci } else { 256ffe3c632Sopenharmony_ci $proto_name = $proto->getName(); 257ffe3c632Sopenharmony_ci $json_name = implode('', array_map('ucwords', explode('_', $proto_name))); 258ffe3c632Sopenharmony_ci if ($proto_name[0] !== "_" && !ctype_upper($proto_name[0])) { 259ffe3c632Sopenharmony_ci $json_name = lcfirst($json_name); 260ffe3c632Sopenharmony_ci } 261ffe3c632Sopenharmony_ci } 262ffe3c632Sopenharmony_ci $field->setJsonName($json_name); 263ffe3c632Sopenharmony_ci 264ffe3c632Sopenharmony_ci $camel_name = implode('', array_map('ucwords', explode('_', $proto->getName()))); 265ffe3c632Sopenharmony_ci $field->setGetter('get' . $camel_name); 266ffe3c632Sopenharmony_ci $field->setSetter('set' . $camel_name); 267ffe3c632Sopenharmony_ci $field->setType($proto->getType()); 268ffe3c632Sopenharmony_ci $field->setNumber($proto->getNumber()); 269ffe3c632Sopenharmony_ci $field->setLabel($proto->getLabel()); 270ffe3c632Sopenharmony_ci $field->setPacked($packed); 271ffe3c632Sopenharmony_ci $field->setOneofIndex($oneof_index); 272ffe3c632Sopenharmony_ci 273ffe3c632Sopenharmony_ci // At this time, the message/enum type may have not been added to pool. 274ffe3c632Sopenharmony_ci // So we use the type name as place holder and will replace it with the 275ffe3c632Sopenharmony_ci // actual descriptor in cross building. 276ffe3c632Sopenharmony_ci switch ($type) { 277ffe3c632Sopenharmony_ci case GPBType::MESSAGE: 278ffe3c632Sopenharmony_ci $field->setMessageType($type_name); 279ffe3c632Sopenharmony_ci break; 280ffe3c632Sopenharmony_ci case GPBType::ENUM: 281ffe3c632Sopenharmony_ci $field->setEnumType($type_name); 282ffe3c632Sopenharmony_ci break; 283ffe3c632Sopenharmony_ci default: 284ffe3c632Sopenharmony_ci break; 285ffe3c632Sopenharmony_ci } 286ffe3c632Sopenharmony_ci 287ffe3c632Sopenharmony_ci return $field; 288ffe3c632Sopenharmony_ci } 289ffe3c632Sopenharmony_ci 290ffe3c632Sopenharmony_ci public static function buildFromProto($proto) 291ffe3c632Sopenharmony_ci { 292ffe3c632Sopenharmony_ci return FieldDescriptor::getFieldDescriptor($proto); 293ffe3c632Sopenharmony_ci } 294ffe3c632Sopenharmony_ci} 295