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