1 | <?php |
||
16 | abstract class ReflectionEnum implements Enum, \Serializable |
||
17 | { |
||
18 | /** |
||
19 | * @var mixed |
||
20 | */ |
||
21 | private $value = ''; |
||
22 | |||
23 | /** |
||
24 | * @var Enum[] |
||
25 | */ |
||
26 | private static $instances = []; |
||
27 | |||
28 | /** |
||
29 | * @var mixed[][] |
||
30 | */ |
||
31 | private static $create_methods = []; |
||
32 | |||
33 | /** |
||
34 | * @var mixed[][] |
||
35 | */ |
||
36 | private static $is_methods = []; |
||
37 | |||
38 | /** |
||
39 | * @var mixed[][] |
||
40 | */ |
||
41 | private static $constants = []; |
||
42 | |||
43 | /** |
||
44 | * @param mixed $value |
||
45 | */ |
||
46 | 4 | final private function __construct($value) |
|
50 | |||
51 | /** |
||
52 | * @param mixed $value |
||
53 | * |
||
54 | * @return Enum |
||
55 | */ |
||
56 | 30 | final public static function byValue($value) |
|
74 | |||
75 | /** |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 20 | final public function value() |
|
82 | |||
83 | /** |
||
84 | * Available values. |
||
85 | * |
||
86 | * @return Enum[] |
||
87 | */ |
||
88 | 2 | final public static function values() |
|
97 | |||
98 | /** |
||
99 | * @param Enum $enum |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | 8 | final public function equals(Enum $enum) |
|
107 | |||
108 | /** |
||
109 | * Is value supported. |
||
110 | * |
||
111 | * @param mixed $value |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | 8 | final public static function isValid($value) |
|
119 | |||
120 | /** |
||
121 | * Get choices for radio group. |
||
122 | * |
||
123 | * <code> |
||
124 | * { |
||
125 | * value1: 'Readable value 1', |
||
126 | * value2: 'Readable value 2', |
||
127 | * } |
||
128 | * </code> |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | 2 | final public static function choices() |
|
141 | |||
142 | /** |
||
143 | * Return readable value. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 10 | public function __toString() |
|
151 | |||
152 | 2 | final public function __clone() |
|
156 | |||
157 | /** |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 8 | public function serialize() |
|
164 | |||
165 | /** |
||
166 | * @param mixed $data |
||
167 | */ |
||
168 | 8 | public function unserialize($data) |
|
172 | |||
173 | /** |
||
174 | * @param string $class |
||
175 | */ |
||
176 | 44 | private static function detectConstants($class) |
|
177 | { |
||
178 | 44 | if (!isset(self::$create_methods[$class])) { |
|
179 | 2 | self::$create_methods[$class] = []; |
|
180 | 2 | self::$is_methods[$class] = []; |
|
181 | 2 | self::$constants[$class] = []; |
|
182 | |||
183 | 2 | foreach (ConstantDetector::detect($class) as $constant => $constant_value) { |
|
184 | 2 | self::$constants[$class][$constant] = $constant_value; |
|
185 | |||
186 | // second parameter of ucwords() is not supported on HHVM |
||
187 | 2 | $constant = str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($constant)))); |
|
188 | |||
189 | 2 | self::$is_methods[$class]['is'.$constant] = $constant_value; |
|
190 | 2 | self::$create_methods[$class][lcfirst($constant)] = $constant_value; |
|
191 | 2 | } |
|
192 | 2 | } |
|
193 | 44 | } |
|
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | 12 | private static function constants() |
|
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | 10 | private function constant() |
|
213 | |||
214 | /** |
||
215 | * @param string $method |
||
216 | * @param array $arguments |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 10 | public function __call($method, array $arguments = []) |
|
231 | |||
232 | /** |
||
233 | * @param string $method |
||
234 | * @param array $arguments |
||
235 | * |
||
236 | * @return Enum |
||
237 | */ |
||
238 | 16 | public static function __callStatic($method, array $arguments = []) |
|
253 | } |
||
254 |