1<?php
2
3namespace Google\Protobuf\Internal;
4
5use Google\Protobuf\EnumValueDescriptor;
6
7class EnumDescriptor
8{
9    use HasPublicDescriptorTrait;
10
11    private $klass;
12    private $legacy_klass;
13    private $full_name;
14    private $value;
15    private $name_to_value;
16    private $value_descriptor = [];
17
18    public function __construct()
19    {
20        $this->public_desc = new \Google\Protobuf\EnumDescriptor($this);
21    }
22
23    public function setFullName($full_name)
24    {
25        $this->full_name = $full_name;
26    }
27
28    public function getFullName()
29    {
30        return $this->full_name;
31    }
32
33    public function addValue($number, $value)
34    {
35        $this->value[$number] = $value;
36        $this->name_to_value[$value->getName()] = $value;
37        $this->value_descriptor[] = new EnumValueDescriptor($value->getName(), $number);
38    }
39
40    public function getValueByNumber($number)
41    {
42        if (isset($this->value[$number])) {
43            return $this->value[$number];
44        }
45        return null;
46    }
47
48    public function getValueByName($name)
49    {
50        if (isset($this->name_to_value[$name])) {
51            return $this->name_to_value[$name];
52        }
53        return null;
54    }
55
56    public function getValueDescriptorByIndex($index)
57    {
58        if (isset($this->value_descriptor[$index])) {
59            return $this->value_descriptor[$index];
60        }
61        return null;
62    }
63
64    public function getValueCount()
65    {
66        return count($this->value);
67    }
68
69    public function setClass($klass)
70    {
71        $this->klass = $klass;
72    }
73
74    public function getClass()
75    {
76        return $this->klass;
77    }
78
79    public function setLegacyClass($klass)
80    {
81        $this->legacy_klass = $klass;
82    }
83
84    public function getLegacyClass()
85    {
86        return $this->legacy_klass;
87    }
88
89    public static function buildFromProto($proto, $file_proto, $containing)
90    {
91        $desc = new EnumDescriptor();
92
93        $enum_name_without_package  = "";
94        $classname = "";
95        $legacy_classname = "";
96        $fullname = "";
97        GPBUtil::getFullClassName(
98            $proto,
99            $containing,
100            $file_proto,
101            $enum_name_without_package,
102            $classname,
103            $legacy_classname,
104            $fullname);
105        $desc->setFullName($fullname);
106        $desc->setClass($classname);
107        $desc->setLegacyClass($legacy_classname);
108        $values = $proto->getValue();
109        foreach ($values as $value) {
110            $desc->addValue($value->getNumber(), $value);
111        }
112
113        return $desc;
114    }
115}
116