FieldDescriptor.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. private $name;
  35. private $json_name;
  36. private $setter;
  37. private $getter;
  38. private $number;
  39. private $label;
  40. private $type;
  41. private $message_type;
  42. private $enum_type;
  43. private $packed;
  44. private $is_map;
  45. private $oneof_index = -1;
  46. public function setOneofIndex($index)
  47. {
  48. $this->oneof_index = $index;
  49. }
  50. public function getOneofIndex()
  51. {
  52. return $this->oneof_index;
  53. }
  54. public function setName($name)
  55. {
  56. $this->name = $name;
  57. }
  58. public function getName()
  59. {
  60. return $this->name;
  61. }
  62. public function setJsonName($json_name)
  63. {
  64. $this->json_name = $json_name;
  65. }
  66. public function getJsonName()
  67. {
  68. return $this->json_name;
  69. }
  70. public function setSetter($setter)
  71. {
  72. $this->setter = $setter;
  73. }
  74. public function getSetter()
  75. {
  76. return $this->setter;
  77. }
  78. public function setGetter($getter)
  79. {
  80. $this->getter = $getter;
  81. }
  82. public function getGetter()
  83. {
  84. return $this->getter;
  85. }
  86. public function setNumber($number)
  87. {
  88. $this->number = $number;
  89. }
  90. public function getNumber()
  91. {
  92. return $this->number;
  93. }
  94. public function setLabel($label)
  95. {
  96. $this->label = $label;
  97. }
  98. public function getLabel()
  99. {
  100. return $this->label;
  101. }
  102. public function isRepeated()
  103. {
  104. return $this->label === GPBLabel::REPEATED;
  105. }
  106. public function setType($type)
  107. {
  108. $this->type = $type;
  109. }
  110. public function getType()
  111. {
  112. return $this->type;
  113. }
  114. public function setMessageType($message_type)
  115. {
  116. $this->message_type = $message_type;
  117. }
  118. public function getMessageType()
  119. {
  120. return $this->message_type;
  121. }
  122. public function setEnumType($enum_type)
  123. {
  124. $this->enum_type = $enum_type;
  125. }
  126. public function getEnumType()
  127. {
  128. return $this->enum_type;
  129. }
  130. public function setPacked($packed)
  131. {
  132. $this->packed = $packed;
  133. }
  134. public function getPacked()
  135. {
  136. return $this->packed;
  137. }
  138. public function isPackable()
  139. {
  140. return $this->isRepeated() && self::isTypePackable($this->type);
  141. }
  142. public function isMap()
  143. {
  144. return $this->getType() == GPBType::MESSAGE &&
  145. !is_null($this->getMessageType()->getOptions()) &&
  146. $this->getMessageType()->getOptions()->getMapEntry();
  147. }
  148. private static function isTypePackable($field_type)
  149. {
  150. return ($field_type !== GPBType::STRING &&
  151. $field_type !== GPBType::GROUP &&
  152. $field_type !== GPBType::MESSAGE &&
  153. $field_type !== GPBType::BYTES);
  154. }
  155. public static function getFieldDescriptor($proto)
  156. {
  157. $type_name = null;
  158. $type = $proto->getType();
  159. switch ($type) {
  160. case GPBType::MESSAGE:
  161. case GPBType::GROUP:
  162. case GPBType::ENUM:
  163. $type_name = $proto->getTypeName();
  164. break;
  165. default:
  166. break;
  167. }
  168. $oneof_index = $proto->hasOneofIndex() ? $proto->getOneofIndex() : -1;
  169. $packed = false;
  170. $options = $proto->getOptions();
  171. if ($options !== null) {
  172. $packed = $options->getPacked();
  173. }
  174. $field = new FieldDescriptor();
  175. $field->setName($proto->getName());
  176. $json_name = $proto->hasJsonName() ? $proto->getJsonName() :
  177. lcfirst(implode('', array_map('ucwords', explode('_', $proto->getName()))));
  178. if ($proto->hasJsonName()) {
  179. $json_name = $proto->getJsonName();
  180. } else {
  181. $proto_name = $proto->getName();
  182. $json_name = implode('', array_map('ucwords', explode('_', $proto_name)));
  183. if ($proto_name[0] !== "_" && !ctype_upper($proto_name[0])) {
  184. $json_name = lcfirst($json_name);
  185. }
  186. }
  187. $field->setJsonName($json_name);
  188. $camel_name = implode('', array_map('ucwords', explode('_', $proto->getName())));
  189. $field->setGetter('get' . $camel_name);
  190. $field->setSetter('set' . $camel_name);
  191. $field->setType($proto->getType());
  192. $field->setNumber($proto->getNumber());
  193. $field->setLabel($proto->getLabel());
  194. $field->setPacked($packed);
  195. $field->setOneofIndex($oneof_index);
  196. // At this time, the message/enum type may have not been added to pool.
  197. // So we use the type name as place holder and will replace it with the
  198. // actual descriptor in cross building.
  199. switch ($type) {
  200. case GPBType::MESSAGE:
  201. $field->setMessageType($type_name);
  202. break;
  203. case GPBType::ENUM:
  204. $field->setEnumType($type_name);
  205. break;
  206. default:
  207. break;
  208. }
  209. return $field;
  210. }
  211. public static function buildFromProto($proto)
  212. {
  213. return FieldDescriptor::getFieldDescriptor($proto);
  214. }
  215. }