xref: /aosp_15_r20/external/protobuf/php/src/Google/Protobuf/FieldDescriptor.php (revision 1b3f573f81763fcece89efc2b6a5209149e44ab8)
1<?php
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2017 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;
34
35use Google\Protobuf\Internal\GetPublicDescriptorTrait;
36use Google\Protobuf\Internal\GPBType;
37
38class FieldDescriptor
39{
40    use GetPublicDescriptorTrait;
41
42    /** @var  \Google\Protobuf\Internal\FieldDescriptor $internal_desc */
43    private $internal_desc;
44
45    /**
46     * @internal
47     */
48    public function __construct($internal_desc)
49    {
50        $this->internal_desc = $internal_desc;
51    }
52
53    /**
54     * @return string Field name
55     */
56    public function getName()
57    {
58        return $this->internal_desc->getName();
59    }
60
61    /**
62     * @return int Protobuf field number
63     */
64    public function getNumber()
65    {
66        return $this->internal_desc->getNumber();
67    }
68
69    /**
70     * @return int
71     */
72    public function getLabel()
73    {
74        return $this->internal_desc->getLabel();
75    }
76
77    /**
78     * @return int
79     */
80    public function getType()
81    {
82        return $this->internal_desc->getType();
83    }
84
85    /**
86     * @return OneofDescriptor
87     */
88    public function getContainingOneof()
89    {
90        return $this->getPublicDescriptor($this->internal_desc->getContainingOneof());
91    }
92
93    /**
94     * Gets the field's containing oneof, only if non-synthetic.
95     *
96     * @return null|OneofDescriptor
97     */
98    public function getRealContainingOneof()
99    {
100        return $this->getPublicDescriptor($this->internal_desc->getRealContainingOneof());
101    }
102
103    /**
104     * @return boolean
105     */
106    public function hasOptionalKeyword()
107    {
108        return $this->internal_desc->hasOptionalKeyword();
109    }
110
111    /**
112     * @return Descriptor Returns a descriptor for the field type if the field type is a message, otherwise throws \Exception
113     * @throws \Exception
114     */
115    public function getMessageType()
116    {
117        if ($this->getType() == GPBType::MESSAGE) {
118            return $this->getPublicDescriptor($this->internal_desc->getMessageType());
119        } else {
120            throw new \Exception("Cannot get message type for non-message field '" . $this->getName() . "'");
121        }
122    }
123
124    /**
125     * @return EnumDescriptor Returns an enum descriptor if the field type is an enum, otherwise throws \Exception
126     * @throws \Exception
127     */
128    public function getEnumType()
129    {
130        if ($this->getType() == GPBType::ENUM) {
131            return $this->getPublicDescriptor($this->internal_desc->getEnumType());
132        } else {
133            throw new \Exception("Cannot get enum type for non-enum field '" . $this->getName() . "'");
134        }
135    }
136
137    /**
138     * @return boolean
139     */
140    public function isMap()
141    {
142        return $this->internal_desc->isMap();
143    }
144}
145