1<?php 2 3// Protocol Buffers - Google's data interchange format 4// Copyright 2017 Google Inc. All rights reserved. 5// 6// Use of this source code is governed by a BSD-style 7// license that can be found in the LICENSE file or at 8// https://developers.google.com/open-source/licenses/bsd 9 10namespace Google\Protobuf; 11 12use Google\Protobuf\Internal\GetPublicDescriptorTrait; 13use Google\Protobuf\Internal\GPBType; 14 15class FieldDescriptor 16{ 17 use GetPublicDescriptorTrait; 18 19 /** @var \Google\Protobuf\Internal\FieldDescriptor $internal_desc */ 20 private $internal_desc; 21 22 /** 23 * @internal 24 */ 25 public function __construct($internal_desc) 26 { 27 $this->internal_desc = $internal_desc; 28 } 29 30 /** 31 * @return string Field name 32 */ 33 public function getName() 34 { 35 return $this->internal_desc->getName(); 36 } 37 38 /** 39 * @return int Protobuf field number 40 */ 41 public function getNumber() 42 { 43 return $this->internal_desc->getNumber(); 44 } 45 46 /** 47 * @return int 48 */ 49 public function getLabel() 50 { 51 return $this->internal_desc->getLabel(); 52 } 53 54 /** 55 * @return int 56 */ 57 public function getType() 58 { 59 return $this->internal_desc->getType(); 60 } 61 62 /** 63 * @return OneofDescriptor 64 */ 65 public function getContainingOneof() 66 { 67 return $this->getPublicDescriptor($this->internal_desc->getContainingOneof()); 68 } 69 70 /** 71 * Gets the field's containing oneof, only if non-synthetic. 72 * 73 * @return null|OneofDescriptor 74 */ 75 public function getRealContainingOneof() 76 { 77 return $this->getPublicDescriptor($this->internal_desc->getRealContainingOneof()); 78 } 79 80 /** 81 * @return boolean 82 */ 83 public function hasOptionalKeyword() 84 { 85 return $this->internal_desc->hasOptionalKeyword(); 86 } 87 88 /** 89 * @return Descriptor Returns a descriptor for the field type if the field type is a message, otherwise throws \Exception 90 * @throws \Exception 91 */ 92 public function getMessageType() 93 { 94 if ($this->getType() == GPBType::MESSAGE) { 95 return $this->getPublicDescriptor($this->internal_desc->getMessageType()); 96 } else { 97 throw new \Exception("Cannot get message type for non-message field '" . $this->getName() . "'"); 98 } 99 } 100 101 /** 102 * @return EnumDescriptor Returns an enum descriptor if the field type is an enum, otherwise throws \Exception 103 * @throws \Exception 104 */ 105 public function getEnumType() 106 { 107 if ($this->getType() == GPBType::ENUM) { 108 return $this->getPublicDescriptor($this->internal_desc->getEnumType()); 109 } else { 110 throw new \Exception("Cannot get enum type for non-enum field '" . $this->getName() . "'"); 111 } 112 } 113 114 /** 115 * @return boolean 116 */ 117 public function isMap() 118 { 119 return $this->internal_desc->isMap(); 120 } 121} 122