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 | 3 | final private function __construct($value) |
|
50 | |||
51 | /** |
||
52 | * @param mixed $value |
||
53 | * |
||
54 | * @return Enum |
||
55 | */ |
||
56 | 39 | final public static function byValue($value) |
|
57 | { |
||
58 | 39 | $class = get_called_class(); |
|
59 | 39 | self::detectConstants($class); |
|
60 | |||
61 | 39 | $method = array_search($value, self::$create_methods[$class], true); |
|
62 | |||
63 | 39 | if ($method === false) { |
|
64 | 3 | throw OutOfEnumException::create($value, $class); |
|
65 | } |
||
66 | |||
67 | // limitation of count object instances |
||
68 | 36 | if (!isset(self::$instances[$class][$method])) { |
|
69 | 3 | self::$instances[$class][$method] = new static($value); |
|
70 | } |
||
71 | |||
72 | 36 | return self::$instances[$class][$method]; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return mixed |
||
77 | */ |
||
78 | 25 | final public function value() |
|
82 | |||
83 | /** |
||
84 | * Available values. |
||
85 | * |
||
86 | * @return Enum[] |
||
87 | */ |
||
88 | 3 | final public static function values() |
|
89 | { |
||
90 | 3 | $values = []; |
|
91 | 3 | foreach (self::constants() as $constant => $value) { |
|
92 | 3 | $values[$constant] = self::byValue($value); |
|
93 | } |
||
94 | |||
95 | 3 | return $values; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param Enum $enum |
||
100 | * |
||
101 | * @return bool |
||
102 | */ |
||
103 | 10 | final public function equals(Enum $enum) |
|
107 | |||
108 | /** |
||
109 | * Is value supported. |
||
110 | * |
||
111 | * @param mixed $value |
||
112 | * |
||
113 | * @return bool |
||
114 | */ |
||
115 | 10 | 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 | 3 | final public static function choices() |
|
133 | { |
||
134 | 3 | $choices = []; |
|
135 | 3 | foreach (self::constants() as $value) { |
|
136 | 3 | $choices[$value] = (string) self::byValue($value); |
|
137 | } |
||
138 | |||
139 | 3 | return $choices; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Return readable value. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 13 | public function __toString() |
|
151 | |||
152 | 3 | final public function __clone() |
|
156 | |||
157 | /** |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 10 | public function serialize() |
|
164 | |||
165 | /** |
||
166 | * @param mixed $data |
||
167 | */ |
||
168 | 10 | public function unserialize($data) |
|
172 | |||
173 | /** |
||
174 | * @param string $class |
||
175 | */ |
||
176 | 54 | private static function detectConstants($class) |
|
194 | |||
195 | /** |
||
196 | * @return array |
||
197 | */ |
||
198 | 16 | private static function constants() |
|
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | 13 | private function constant() |
|
213 | |||
214 | /** |
||
215 | * @param string $method |
||
216 | * @param array $arguments |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 12 | public function __call($method, array $arguments = []) |
|
231 | |||
232 | /** |
||
233 | * @param string $method |
||
234 | * @param array $arguments |
||
235 | * |
||
236 | * @return Enum |
||
237 | */ |
||
238 | 18 | public static function __callStatic($method, array $arguments = []) |
|
253 | } |
||
254 |