FieldDescriptor.php 6.9 KB

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