1<?php 2 3// Protocol Buffers - Google's data interchange format 4// Copyright 2008 Google Inc. All rights reserved. 5// https://developers.google.com/protocol-buffers/ 6// 7// Redistribution and use in source and binary forms, with or without 8// modification, are permitted provided that the following conditions are 9// met: 10// 11// * Redistributions of source code must retain the above copyright 12// notice, this list of conditions and the following disclaimer. 13// * Redistributions in binary form must reproduce the above 14// copyright notice, this list of conditions and the following disclaimer 15// in the documentation and/or other materials provided with the 16// distribution. 17// * Neither the name of Google Inc. nor the names of its 18// contributors may be used to endorse or promote products derived from 19// this software without specific prior written permission. 20// 21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 33namespace Google\Protobuf\Internal; 34 35class Descriptor 36{ 37 use HasPublicDescriptorTrait; 38 39 private $full_name; 40 private $field = []; 41 private $json_to_field = []; 42 private $name_to_field = []; 43 private $index_to_field = []; 44 private $nested_type = []; 45 private $enum_type = []; 46 private $klass; 47 private $legacy_klass; 48 private $previous_klass; 49 private $options; 50 private $oneof_decl = []; 51 52 public function __construct() 53 { 54 $this->public_desc = new \Google\Protobuf\Descriptor($this); 55 } 56 57 public function addOneofDecl($oneof) 58 { 59 $this->oneof_decl[] = $oneof; 60 } 61 62 public function getOneofDecl() 63 { 64 return $this->oneof_decl; 65 } 66 67 public function setFullName($full_name) 68 { 69 $this->full_name = $full_name; 70 } 71 72 public function getFullName() 73 { 74 return $this->full_name; 75 } 76 77 public function addField($field) 78 { 79 $this->field[$field->getNumber()] = $field; 80 $this->json_to_field[$field->getJsonName()] = $field; 81 $this->name_to_field[$field->getName()] = $field; 82 $this->index_to_field[] = $field; 83 } 84 85 public function getField() 86 { 87 return $this->field; 88 } 89 90 public function addNestedType($desc) 91 { 92 $this->nested_type[] = $desc; 93 } 94 95 public function getNestedType() 96 { 97 return $this->nested_type; 98 } 99 100 public function addEnumType($desc) 101 { 102 $this->enum_type[] = $desc; 103 } 104 105 public function getEnumType() 106 { 107 return $this->enum_type; 108 } 109 110 public function getFieldByNumber($number) 111 { 112 if (!isset($this->field[$number])) { 113 return NULL; 114 } else { 115 return $this->field[$number]; 116 } 117 } 118 119 public function getFieldByJsonName($json_name) 120 { 121 if (!isset($this->json_to_field[$json_name])) { 122 return NULL; 123 } else { 124 return $this->json_to_field[$json_name]; 125 } 126 } 127 128 public function getFieldByName($name) 129 { 130 if (!isset($this->name_to_field[$name])) { 131 return NULL; 132 } else { 133 return $this->name_to_field[$name]; 134 } 135 } 136 137 public function getFieldByIndex($index) 138 { 139 if (count($this->index_to_field) <= $index) { 140 return NULL; 141 } else { 142 return $this->index_to_field[$index]; 143 } 144 } 145 146 public function setClass($klass) 147 { 148 $this->klass = $klass; 149 } 150 151 public function getClass() 152 { 153 return $this->klass; 154 } 155 156 public function setLegacyClass($klass) 157 { 158 $this->legacy_klass = $klass; 159 } 160 161 public function getLegacyClass() 162 { 163 return $this->legacy_klass; 164 } 165 166 public function setPreviouslyUnreservedClass($klass) 167 { 168 $this->previous_klass = $klass; 169 } 170 171 public function getPreviouslyUnreservedClass() 172 { 173 return $this->previous_klass; 174 } 175 176 public function setOptions($options) 177 { 178 $this->options = $options; 179 } 180 181 public function getOptions() 182 { 183 return $this->options; 184 } 185 186 public static function buildFromProto($proto, $file_proto, $containing) 187 { 188 $desc = new Descriptor(); 189 190 $message_name_without_package = ""; 191 $classname = ""; 192 $legacy_classname = ""; 193 $previous_classname = ""; 194 $fullname = ""; 195 GPBUtil::getFullClassName( 196 $proto, 197 $containing, 198 $file_proto, 199 $message_name_without_package, 200 $classname, 201 $legacy_classname, 202 $fullname, 203 $previous_classname); 204 $desc->setFullName($fullname); 205 $desc->setClass($classname); 206 $desc->setLegacyClass($legacy_classname); 207 $desc->setPreviouslyUnreservedClass($previous_classname); 208 $desc->setOptions($proto->getOptions()); 209 210 foreach ($proto->getField() as $field_proto) { 211 $desc->addField(FieldDescriptor::buildFromProto($field_proto)); 212 } 213 214 // Handle nested types. 215 foreach ($proto->getNestedType() as $nested_proto) { 216 $desc->addNestedType(Descriptor::buildFromProto( 217 $nested_proto, $file_proto, $message_name_without_package)); 218 } 219 220 // Handle nested enum. 221 foreach ($proto->getEnumType() as $enum_proto) { 222 $desc->addEnumType(EnumDescriptor::buildFromProto( 223 $enum_proto, $file_proto, $message_name_without_package)); 224 } 225 226 // Handle oneof fields. 227 $index = 0; 228 foreach ($proto->getOneofDecl() as $oneof_proto) { 229 $desc->addOneofDecl( 230 OneofDescriptor::buildFromProto($oneof_proto, $desc, $index)); 231 $index++; 232 } 233 234 return $desc; 235 } 236} 237