aboutsummaryrefslogtreecommitdiffhomepage
path: root/php/src/Google/Protobuf/Internal/EnumDescriptor.php
blob: 01649fec4fa395635c5ac3f1898768f98e310249 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Google\Protobuf\Internal;

use Google\Protobuf\EnumValueDescriptor;

class EnumDescriptor
{
    use HasPublicDescriptorTrait;

    private $klass;
    private $full_name;
    private $value;
    private $name_to_value;
    private $value_descriptor = [];

    public function __construct()
    {
        $this->public_desc = new \Google\Protobuf\EnumDescriptor($this);
    }

    public function setFullName($full_name)
    {
        $this->full_name = $full_name;
    }

    public function getFullName()
    {
        return $this->full_name;
    }

    public function addValue($number, $value)
    {
        $this->value[$number] = $value;
        $this->name_to_value[$value->getName()] = $value;
        $this->value_descriptor[] = new EnumValueDescriptor($value->getName(), $number);
    }

    public function getValueByNumber($number)
    {
        return $this->value[$number];
    }

    public function getValueByName($name)
    {
        return $this->name_to_value[$name];
    }

    public function getValueDescriptorByIndex($index)
    {
        return $this->value_descriptor[$index];
    }

    public function getValueCount()
    {
        return count($this->value);
    }

    public function setClass($klass)
    {
        $this->klass = $klass;
    }

    public function getClass()
    {
        return $this->klass;
    }

    public static function buildFromProto($proto, $file_proto, $containing)
    {
        $desc = new EnumDescriptor();

        $enum_name_without_package  = "";
        $classname = "";
        $fullname = "";
        GPBUtil::getFullClassName(
            $proto,
            $containing,
            $file_proto,
            $enum_name_without_package,
            $classname,
            $fullname);
        $desc->setFullName($fullname);
        $desc->setClass($classname);
        $values = $proto->getValue();
        foreach ($values as $value) {
            $desc->addValue($value->getNumber(), $value);
        }

        return $desc;
    }
}