FieldDescriptor.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2017 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;
  32. use Google\Protobuf\Internal\GetPublicDescriptorTrait;
  33. use Google\Protobuf\Internal\GPBType;
  34. class FieldDescriptor
  35. {
  36. use GetPublicDescriptorTrait;
  37. private $internal_desc;
  38. /**
  39. * @internal
  40. */
  41. public function __construct($internal_desc)
  42. {
  43. $this->internal_desc = $internal_desc;
  44. }
  45. /**
  46. * @return string Field name
  47. */
  48. public function getName()
  49. {
  50. return $this->internal_desc->getName();
  51. }
  52. /**
  53. * @return int Protobuf field number
  54. */
  55. public function getNumber()
  56. {
  57. return $this->internal_desc->getNumber();
  58. }
  59. /**
  60. * @return int
  61. */
  62. public function getLabel()
  63. {
  64. return $this->internal_desc->getLabel();
  65. }
  66. /**
  67. * @return int
  68. */
  69. public function getType()
  70. {
  71. return $this->internal_desc->getType();
  72. }
  73. /**
  74. * @return Descriptor Returns a descriptor for the field type if the field type is a message, otherwise throws \Exception
  75. * @throws \Exception
  76. */
  77. public function getMessageType()
  78. {
  79. if ($this->getType() == GPBType::MESSAGE) {
  80. return $this->getPublicDescriptor($this->internal_desc->getMessageType());
  81. } else {
  82. throw new \Exception("Cannot get message type for non-message field '" . $this->getName() . "'");
  83. }
  84. }
  85. /**
  86. * @return EnumDescriptor Returns an enum descriptor if the field type is an enum, otherwise throws \Exception
  87. * @throws \Exception
  88. */
  89. public function getEnumType()
  90. {
  91. if ($this->getType() == GPBType::ENUM) {
  92. return $this->getPublicDescriptor($this->internal_desc->getEnumType());
  93. } else {
  94. throw new \Exception("Cannot get enum type for non-enum field '" . $this->getName() . "'");
  95. }
  96. }
  97. /**
  98. * @return boolean
  99. */
  100. public function isMap()
  101. {
  102. return $this->internal_desc->isMap();
  103. }
  104. /**
  105. * @return boolean
  106. */
  107. public function hasOptionalKeyword()
  108. {
  109. return $this->internal_desc->hasOptionalKeyword();
  110. }
  111. }