1 | <?php |
||
15 | abstract class ReflectionEnum implements Enum, \Serializable |
||
16 | { |
||
17 | /** |
||
18 | * @var mixed |
||
19 | */ |
||
20 | private $value = ''; |
||
21 | |||
22 | /** |
||
23 | * @var Enum[] |
||
24 | */ |
||
25 | private static $instances = []; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $constants = []; |
||
31 | |||
32 | /** |
||
33 | * @param mixed $value |
||
34 | */ |
||
35 | final private function __construct($value) |
||
36 | { |
||
37 | $this->value = $value; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @param mixed $value |
||
42 | * |
||
43 | * @return Enum |
||
44 | */ |
||
45 | final public static function byValue($value) |
||
46 | { |
||
47 | $class = get_called_class(); |
||
48 | self::constants(); |
||
49 | |||
50 | $constant = array_search($value, self::$constants[$class], true); |
||
51 | |||
52 | if ($constant === false) { |
||
53 | throw OutOfEnumException::invalidValue($value, $class); |
||
54 | } |
||
55 | |||
56 | 4 | // limitation of count object instances |
|
57 | if (!isset(self::$instances[$class][$constant])) { |
||
58 | 4 | self::$instances[$class][$constant] = new static($value); |
|
59 | 4 | } |
|
60 | |||
61 | return self::$instances[$class][$constant]; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @param string $name |
||
66 | 30 | * |
|
67 | * @return Enum |
||
68 | 30 | */ |
|
69 | 30 | final public static function byName($name) |
|
70 | { |
||
71 | 30 | $class = get_called_class(); |
|
72 | if (isset(self::$instances[$class][$name])) { |
||
73 | 30 | return self::$instances[$class][$name]; |
|
74 | 2 | } |
|
75 | |||
76 | self::constants(); |
||
77 | |||
78 | 28 | if (!isset(self::$constants[$class][$name])) { |
|
79 | 2 | throw OutOfEnumException::undefinedConstant($class . '::' . $name); |
|
80 | 2 | } |
|
81 | |||
82 | 28 | return self::$instances[$class][$name] = new static(self::$constants[$class][$name]); |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return mixed |
||
87 | */ |
||
88 | 20 | final public function value() |
|
92 | |||
93 | /** |
||
94 | * Available values. |
||
95 | * |
||
96 | * @return Enum[] |
||
97 | */ |
||
98 | 2 | final public static function values() |
|
107 | 2 | ||
108 | /** |
||
109 | 2 | * @return string |
|
110 | */ |
||
111 | final public function name() |
||
115 | |||
116 | /** |
||
117 | 8 | * @param Enum $enum |
|
118 | * |
||
119 | 8 | * @return bool |
|
120 | */ |
||
121 | final public function equals(Enum $enum) |
||
125 | |||
126 | /** |
||
127 | * Get choices for radio group. |
||
128 | * |
||
129 | 8 | * <code> |
|
130 | * { |
||
131 | 8 | * value1: 'Readable value 1', |
|
132 | * value2: 'Readable value 2', |
||
133 | * } |
||
134 | * </code> |
||
135 | * |
||
136 | * @return array |
||
137 | */ |
||
138 | final public static function choices() |
||
147 | |||
148 | 2 | /** |
|
149 | * Return readable value. |
||
150 | 2 | * |
|
151 | 2 | * @return string |
|
152 | 2 | */ |
|
153 | 2 | public function __toString() |
|
157 | 2 | ||
158 | final public function __clone() |
||
162 | |||
163 | /** |
||
164 | * @return mixed |
||
165 | 10 | */ |
|
166 | public function serialize() |
||
170 | 2 | ||
171 | /** |
||
172 | 2 | * @param mixed $data |
|
173 | */ |
||
174 | public function unserialize($data) |
||
178 | 8 | ||
179 | /** |
||
180 | 8 | * @return array |
|
181 | */ |
||
182 | private static function constants() |
||
191 | |||
192 | /** |
||
193 | * @param string $method |
||
194 | 44 | * @param array $arguments |
|
195 | * |
||
196 | 44 | * @return Enum |
|
197 | 2 | */ |
|
198 | 2 | public static function __callStatic($method, array $arguments = []) |
|
202 | } |
||
203 |