1 | <?php |
||
15 | abstract class Enum |
||
16 | { |
||
17 | /** |
||
18 | * @var mixed |
||
19 | */ |
||
20 | protected $value; |
||
21 | |||
22 | /** |
||
23 | * @var array |
||
24 | */ |
||
25 | protected static $cache = array(); |
||
26 | |||
27 | /** |
||
28 | * @param mixed $value |
||
29 | * @throws \Exception |
||
30 | */ |
||
31 | 13 | public function __construct($value) |
|
39 | |||
40 | /** |
||
41 | * @return mixed |
||
42 | */ |
||
43 | 4 | public function value() |
|
47 | |||
48 | /** |
||
49 | * @return mixed |
||
50 | */ |
||
51 | 1 | public function key() |
|
55 | |||
56 | /** |
||
57 | * @return string |
||
58 | */ |
||
59 | 3 | public function __toString(): string |
|
63 | |||
64 | /** |
||
65 | * @param Enum $enum |
||
66 | * @return bool True if Enums are equal, false if not equal |
||
67 | */ |
||
68 | 2 | final public function equals(Enum $enum): bool |
|
72 | |||
73 | /** |
||
74 | * @return array |
||
75 | */ |
||
76 | 1 | public static function keys(): array |
|
80 | |||
81 | /** |
||
82 | * @return static[] Constant name in key, Enum instance in value |
||
83 | */ |
||
84 | 1 | public static function values(): array |
|
94 | |||
95 | /** |
||
96 | * @return array Constant name in key, constant value in value |
||
97 | */ |
||
98 | 32 | public static function toArray(): array |
|
108 | |||
109 | /** |
||
110 | * @param $value |
||
111 | * @return bool |
||
112 | */ |
||
113 | 20 | public static function isValid($value): bool |
|
117 | |||
118 | /** |
||
119 | * @param $key |
||
120 | * @return bool |
||
121 | */ |
||
122 | 1 | public static function isValidKey($key): bool |
|
128 | |||
129 | /** |
||
130 | * @param $value |
||
131 | * @return mixed |
||
132 | */ |
||
133 | 8 | public static function search($value) |
|
137 | |||
138 | /** |
||
139 | * Returns a value when called statically like so: MyEnum::someValue() given SOME_VALUE is a class constant |
||
140 | * @example MyEnum::someValue() |
||
141 | * |
||
142 | * @param string $name |
||
143 | * @param array $arguments |
||
144 | * @return static |
||
145 | * @throws \Exception |
||
146 | */ |
||
147 | 3 | public static function __callStatic($name, $arguments) |
|
157 | |||
158 | |||
159 | /** |
||
160 | * Check if a given key is selected using the "isKey" syntax. |
||
161 | * @example $myEnum->isSomeValue() |
||
162 | * |
||
163 | * @param string $method |
||
164 | * @param $arguments |
||
165 | * @return bool |
||
166 | * @throws \Exception |
||
167 | */ |
||
168 | 3 | public function __call($method, $arguments): bool |
|
180 | |||
181 | /** |
||
182 | * @param string $string |
||
183 | * @return string |
||
184 | */ |
||
185 | 6 | private static function toSnakeCase(string $string): string |
|
193 | |||
194 | /** |
||
195 | * @param string $value |
||
196 | * @return \Exception |
||
197 | */ |
||
198 | 2 | public static function customInvalidValueException(string $value): \Exception |
|
202 | |||
203 | /** |
||
204 | * @param string $method |
||
205 | * @return \Exception |
||
206 | */ |
||
207 | 1 | public static function customUnknownStaticMethodException(string $method): \Exception |
|
211 | |||
212 | /** |
||
213 | * @param string $method |
||
214 | * @return \Exception |
||
215 | */ |
||
216 | 1 | public static function customUnknownMethodException(string $method): \Exception |
|
220 | } |