• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<?php
2
3// Protocol Buffers - Google's data interchange format
4// Copyright 2008 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\Internal;
11
12class Descriptor
13{
14    use HasPublicDescriptorTrait;
15
16    private $full_name;
17    private $field = [];
18    private $json_to_field = [];
19    private $name_to_field = [];
20    private $index_to_field = [];
21    private $nested_type = [];
22    private $enum_type = [];
23    private $klass;
24    private $legacy_klass;
25    private $previous_klass;
26    private $options;
27    private $oneof_decl = [];
28
29    public function __construct()
30    {
31        $this->public_desc = new \Google\Protobuf\Descriptor($this);
32    }
33
34    public function addOneofDecl($oneof)
35    {
36        $this->oneof_decl[] = $oneof;
37    }
38
39    public function getOneofDecl()
40    {
41        return $this->oneof_decl;
42    }
43
44    public function setFullName($full_name)
45    {
46        $this->full_name = $full_name;
47    }
48
49    public function getFullName()
50    {
51        return $this->full_name;
52    }
53
54    public function addField($field)
55    {
56        $this->field[$field->getNumber()] = $field;
57        $this->json_to_field[$field->getJsonName()] = $field;
58        $this->name_to_field[$field->getName()] = $field;
59        $this->index_to_field[] = $field;
60    }
61
62    public function getField()
63    {
64        return $this->field;
65    }
66
67    public function addNestedType($desc)
68    {
69        $this->nested_type[] = $desc;
70    }
71
72    public function getNestedType()
73    {
74        return $this->nested_type;
75    }
76
77    public function addEnumType($desc)
78    {
79        $this->enum_type[] = $desc;
80    }
81
82    public function getEnumType()
83    {
84        return $this->enum_type;
85    }
86
87    public function getFieldByNumber($number)
88    {
89        if (!isset($this->field[$number])) {
90          return NULL;
91        } else {
92          return $this->field[$number];
93        }
94    }
95
96    public function getFieldByJsonName($json_name)
97    {
98        if (!isset($this->json_to_field[$json_name])) {
99          return NULL;
100        } else {
101          return $this->json_to_field[$json_name];
102        }
103    }
104
105    public function getFieldByName($name)
106    {
107        if (!isset($this->name_to_field[$name])) {
108          return NULL;
109        } else {
110          return $this->name_to_field[$name];
111        }
112    }
113
114    public function getFieldByIndex($index)
115    {
116        if (count($this->index_to_field) <= $index) {
117            return NULL;
118        } else {
119            return $this->index_to_field[$index];
120        }
121    }
122
123    public function setClass($klass)
124    {
125        $this->klass = $klass;
126    }
127
128    public function getClass()
129    {
130        return $this->klass;
131    }
132
133    public function setLegacyClass($klass)
134    {
135        $this->legacy_klass = $klass;
136    }
137
138    public function getLegacyClass()
139    {
140        return $this->legacy_klass;
141    }
142
143    public function setPreviouslyUnreservedClass($klass)
144    {
145        $this->previous_klass = $klass;
146    }
147
148    public function getPreviouslyUnreservedClass()
149    {
150        return $this->previous_klass;
151    }
152
153    public function setOptions($options)
154    {
155        $this->options = $options;
156    }
157
158    public function getOptions()
159    {
160        return $this->options;
161    }
162
163    public static function buildFromProto($proto, $file_proto, $containing)
164    {
165        $desc = new Descriptor();
166
167        $message_name_without_package  = "";
168        $classname = "";
169        $legacy_classname = "";
170        $previous_classname = "";
171        $fullname = "";
172        GPBUtil::getFullClassName(
173            $proto,
174            $containing,
175            $file_proto,
176            $message_name_without_package,
177            $classname,
178            $legacy_classname,
179            $fullname,
180            $previous_classname);
181        $desc->setFullName($fullname);
182        $desc->setClass($classname);
183        $desc->setLegacyClass($legacy_classname);
184        $desc->setPreviouslyUnreservedClass($previous_classname);
185        $desc->setOptions($proto->getOptions());
186
187        foreach ($proto->getField() as $field_proto) {
188            $desc->addField(FieldDescriptor::buildFromProto($field_proto));
189        }
190
191        // Handle nested types.
192        foreach ($proto->getNestedType() as $nested_proto) {
193            $desc->addNestedType(Descriptor::buildFromProto(
194              $nested_proto, $file_proto, $message_name_without_package));
195        }
196
197        // Handle nested enum.
198        foreach ($proto->getEnumType() as $enum_proto) {
199            $desc->addEnumType(EnumDescriptor::buildFromProto(
200              $enum_proto, $file_proto, $message_name_without_package));
201        }
202
203        // Handle oneof fields.
204        $index = 0;
205        foreach ($proto->getOneofDecl() as $oneof_proto) {
206            $desc->addOneofDecl(
207                OneofDescriptor::buildFromProto($oneof_proto, $desc, $index));
208            $index++;
209        }
210
211        return $desc;
212    }
213}
214