1 | <?php |
||
11 | abstract class Enum |
||
12 | { |
||
13 | /** |
||
14 | * @var mixed |
||
15 | */ |
||
16 | protected $value; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected static $cache = array(); |
||
22 | |||
23 | /** |
||
24 | * @param mixed $value |
||
25 | * @throws \Exception |
||
26 | */ |
||
27 | 17 | public function __construct($value) |
|
35 | |||
36 | /** |
||
37 | * @return mixed |
||
38 | */ |
||
39 | 5 | public function value() |
|
43 | |||
44 | /** |
||
45 | * @return mixed |
||
46 | */ |
||
47 | 2 | public function key() |
|
51 | |||
52 | /** |
||
53 | * @return string |
||
54 | */ |
||
55 | 3 | public function __toString(): string |
|
59 | |||
60 | /** |
||
61 | * @param Enum $enum |
||
62 | * @return bool True if Enums are equal, false if not equal |
||
63 | */ |
||
64 | 2 | public function equals(Enum $enum): bool |
|
68 | |||
69 | /** |
||
70 | * @return array |
||
71 | */ |
||
72 | 1 | public static function keys(): array |
|
76 | |||
77 | /** |
||
78 | * @return static[] Constant name in key, Enum instance in value |
||
79 | */ |
||
80 | 1 | public static function values(): array |
|
90 | |||
91 | /** |
||
92 | * @return array Constant name in key, constant value in value |
||
93 | */ |
||
94 | 37 | public static function toArray(): array |
|
104 | |||
105 | /** |
||
106 | * @param $value |
||
107 | * @return bool |
||
108 | */ |
||
109 | 24 | public static function isValid($value): bool |
|
113 | |||
114 | /** |
||
115 | * @param $key |
||
116 | * @return bool |
||
117 | */ |
||
118 | 1 | public static function isValidKey($key): bool |
|
124 | |||
125 | /** |
||
126 | * @param $value |
||
127 | * @return mixed |
||
128 | */ |
||
129 | 9 | public static function search($value) |
|
133 | |||
134 | /** |
||
135 | * Returns a value when called statically like so: MyEnum::someValue() given SOME_VALUE is a class constant |
||
136 | * @example MyEnum::someValue() |
||
137 | * |
||
138 | * @param string $name |
||
139 | * @param array $arguments |
||
140 | * @return static |
||
141 | * @throws \Exception |
||
142 | */ |
||
143 | 5 | public static function __callStatic($name, $arguments) |
|
153 | |||
154 | |||
155 | /** |
||
156 | * Check if a given key is selected using the "isKey" syntax. |
||
157 | * @example $myEnum->isSomeValue() |
||
158 | * |
||
159 | * @param string $method |
||
160 | * @param $arguments |
||
161 | * @return bool |
||
162 | * @throws \Exception |
||
163 | */ |
||
164 | 5 | public function __call($method, $arguments): bool |
|
176 | |||
177 | /** |
||
178 | * @param string $string |
||
179 | * @return string |
||
180 | */ |
||
181 | 9 | private static function toSnakeCase(string $string): string |
|
182 | { |
||
183 | 9 | return strtoupper(preg_replace( |
|
184 | 9 | '/(?<=\d)(?=[A-Za-z])|(?<=[A-Za-z])(?=\d)|(?<=[a-z])(?=[A-Z])/', |
|
185 | 9 | '_', |
|
186 | 9 | $string |
|
187 | )); |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $value |
||
192 | * @return \Exception |
||
193 | */ |
||
194 | 3 | public static function customInvalidValueException(string $value): \Exception |
|
198 | |||
199 | /** |
||
200 | * @param string $method |
||
201 | * @return \Exception |
||
202 | */ |
||
203 | 2 | public static function customUnknownStaticMethodException(string $method): \Exception |
|
207 | |||
208 | /** |
||
209 | * @param string $method |
||
210 | * @return \Exception |
||
211 | */ |
||
212 | 2 | public static function customUnknownMethodException(string $method): \Exception |
|
216 | } |