1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MabeEnum; |
6
|
|
|
|
7
|
|
|
use ReflectionClass; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
use LogicException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Abstract base enumeration class. |
13
|
|
|
* |
14
|
|
|
* @copyright 2019 Marc Bennewitz |
15
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
16
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
17
|
|
|
*/ |
|
|
|
|
18
|
|
|
abstract class Enum |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The selected enumerator value |
22
|
|
|
* |
23
|
|
|
* @var null|bool|int|float|string|array |
24
|
|
|
*/ |
25
|
|
|
private $value; |
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The ordinal number of the enumerator |
29
|
|
|
* |
30
|
|
|
* @var null|int |
31
|
|
|
*/ |
32
|
|
|
private $ordinal; |
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A map of enumerator names and values by enumeration class |
36
|
|
|
* |
37
|
|
|
* @var array ["$class" => ["$name" => $value, ...], ...] |
38
|
|
|
*/ |
39
|
|
|
private static $constants = []; |
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* A List of available enumerator names by enumeration class |
43
|
|
|
* |
44
|
|
|
* @var array ["$class" => ["$name0", ...], ...] |
45
|
|
|
*/ |
46
|
|
|
private static $names = []; |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Already instantiated enumerators |
50
|
|
|
* |
51
|
|
|
* @var array ["$class" => ["$name" => $instance, ...], ...] |
52
|
|
|
*/ |
53
|
|
|
private static $instances = []; |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Constructor |
57
|
|
|
* |
58
|
|
|
* @param null|bool|int|float|string|array $value The value of the enumerator |
|
|
|
|
59
|
|
|
* @param int|null $ordinal The ordinal number of the enumerator |
|
|
|
|
60
|
|
|
*/ |
61
|
43 |
|
final private function __construct($value, $ordinal = null) |
|
|
|
|
62
|
|
|
{ |
63
|
43 |
|
$this->value = $value; |
64
|
43 |
|
$this->ordinal = $ordinal; |
65
|
43 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the name of the enumerator |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* @see getName() |
72
|
|
|
*/ |
73
|
1 |
|
public function __toString(): string |
74
|
|
|
{ |
75
|
1 |
|
return $this->getName(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws LogicException Enums are not cloneable |
|
|
|
|
80
|
|
|
* because instances are implemented as singletons |
81
|
|
|
*/ |
|
|
|
|
82
|
1 |
|
final private function __clone() |
83
|
|
|
{ |
84
|
1 |
|
throw new LogicException('Enums are not cloneable'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws LogicException Enums are not serializable |
|
|
|
|
89
|
|
|
* because instances are implemented as singletons |
90
|
|
|
*/ |
|
|
|
|
91
|
1 |
|
final public function __sleep() |
92
|
|
|
{ |
93
|
1 |
|
throw new LogicException('Enums are not serializable'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @throws LogicException Enums are not serializable |
|
|
|
|
98
|
|
|
* because instances are implemented as singletons |
99
|
|
|
*/ |
|
|
|
|
100
|
1 |
|
final public function __wakeup() |
101
|
|
|
{ |
102
|
1 |
|
throw new LogicException('Enums are not serializable'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the value of the enumerator |
107
|
|
|
* |
108
|
|
|
* @return null|bool|int|float|string|array |
|
|
|
|
109
|
|
|
*/ |
110
|
29 |
|
final public function getValue() |
111
|
|
|
{ |
112
|
29 |
|
return $this->value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the name of the enumerator |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
8 |
|
final public function getName() |
121
|
|
|
{ |
122
|
8 |
|
return self::$names[static::class][$this->ordinal ?: $this->getOrdinal()]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get the ordinal number of the enumerator |
127
|
|
|
* |
128
|
|
|
* @return int |
|
|
|
|
129
|
|
|
*/ |
130
|
102 |
|
final public function getOrdinal() |
131
|
|
|
{ |
132
|
102 |
|
if ($this->ordinal === null) { |
133
|
21 |
|
$ordinal = 0; |
134
|
21 |
|
$value = $this->value; |
135
|
21 |
|
foreach (self::detectConstants(static::class) as $constValue) { |
136
|
21 |
|
if ($value === $constValue) { |
137
|
21 |
|
break; |
138
|
|
|
} |
139
|
16 |
|
++$ordinal; |
140
|
|
|
} |
141
|
|
|
|
142
|
21 |
|
$this->ordinal = $ordinal; |
143
|
|
|
} |
144
|
|
|
|
145
|
102 |
|
return $this->ordinal; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Compare this enumerator against another and check if it's the same. |
150
|
|
|
* |
151
|
|
|
* @param static|null|bool|int|float|string|array $enumerator An enumerator object or value |
|
|
|
|
152
|
|
|
* @return bool |
|
|
|
|
153
|
|
|
*/ |
154
|
2 |
|
final public function is($enumerator) |
155
|
|
|
{ |
156
|
2 |
|
return $this === $enumerator || $this->value === $enumerator |
157
|
|
|
|
158
|
|
|
// The following additional conditions are required only because of the issue of serializable singletons |
|
|
|
|
159
|
2 |
|
|| ($enumerator instanceof static |
160
|
2 |
|
&& \get_class($enumerator) === static::class |
161
|
2 |
|
&& $enumerator->value === $this->value |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Get an enumerator instance of the given enumerator value or instance |
167
|
|
|
* |
168
|
|
|
* @param static|null|bool|int|float|string|array $enumerator An enumerator object or value |
|
|
|
|
169
|
|
|
* @return static |
170
|
|
|
* @throws InvalidArgumentException On an unknwon or invalid value |
|
|
|
|
171
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
172
|
|
|
*/ |
173
|
113 |
|
final public static function get($enumerator) |
174
|
|
|
{ |
175
|
113 |
|
if ($enumerator instanceof static && \get_class($enumerator) === static::class) { |
176
|
34 |
|
return $enumerator; |
177
|
|
|
} |
178
|
|
|
|
179
|
88 |
|
return static::byValue($enumerator); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get an enumerator instance by the given value |
184
|
|
|
* |
185
|
|
|
* @param null|bool|int|float|string|array $value Enumerator value |
|
|
|
|
186
|
|
|
* @return static |
187
|
|
|
* @throws InvalidArgumentException On an unknwon or invalid value |
|
|
|
|
188
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
189
|
|
|
*/ |
190
|
88 |
|
final public static function byValue($value) |
191
|
|
|
{ |
192
|
88 |
|
if (!isset(self::$constants[static::class])) { |
193
|
16 |
|
self::detectConstants(static::class); |
194
|
|
|
} |
195
|
|
|
|
196
|
86 |
|
$name = \array_search($value, self::$constants[static::class], true); |
197
|
86 |
|
if ($name === false) { |
198
|
12 |
|
throw new InvalidArgumentException(sprintf( |
199
|
12 |
|
'Unknown value %s for enumeration %s', |
200
|
12 |
|
\is_scalar($value) |
201
|
7 |
|
? \var_export($value, true) |
|
|
|
|
202
|
12 |
|
: 'of type ' . (\is_object($value) ? \get_class($value) : \gettype($value)), |
|
|
|
|
203
|
12 |
|
static::class |
204
|
|
|
)); |
205
|
|
|
} |
206
|
|
|
|
207
|
77 |
|
if (!isset(self::$instances[static::class][$name])) { |
208
|
21 |
|
self::$instances[static::class][$name] = new static(self::$constants[static::class][$name]); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
77 |
|
return self::$instances[static::class][$name]; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Get an enumerator instance by the given name |
216
|
|
|
* |
217
|
|
|
* @param string $name The name of the enumerator |
|
|
|
|
218
|
|
|
* @return static |
219
|
|
|
* @throws InvalidArgumentException On an invalid or unknown name |
|
|
|
|
220
|
|
|
* @throws LogicException On ambiguous values |
|
|
|
|
221
|
|
|
*/ |
222
|
54 |
|
final public static function byName(string $name) |
223
|
|
|
{ |
224
|
54 |
|
if (isset(self::$instances[static::class][$name])) { |
225
|
41 |
|
return self::$instances[static::class][$name]; |
226
|
|
|
} |
227
|
|
|
|
228
|
17 |
|
$const = static::class . "::{$name}"; |
|
|
|
|
229
|
17 |
|
if (!\defined($const)) { |
230
|
1 |
|
throw new InvalidArgumentException("{$const} not defined"); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
16 |
|
return self::$instances[static::class][$name] = new static(\constant($const)); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* Get an enumeration instance by the given ordinal number |
238
|
|
|
* |
239
|
|
|
* @param int $ordinal The ordinal number of the enumerator |
|
|
|
|
240
|
|
|
* @return static |
241
|
|
|
* @throws InvalidArgumentException On an invalid ordinal number |
|
|
|
|
242
|
|
|
* @throws LogicException On ambiguous values |
|
|
|
|
243
|
|
|
*/ |
244
|
39 |
|
final public static function byOrdinal(int $ordinal) |
245
|
|
|
{ |
246
|
39 |
|
if (!isset(self::$names[static::class])) { |
247
|
2 |
|
self::detectConstants(static::class); |
248
|
|
|
} |
249
|
|
|
|
250
|
39 |
|
if (!isset(self::$names[static::class][$ordinal])) { |
251
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
252
|
1 |
|
'Invalid ordinal number %s, must between 0 and %s', |
253
|
1 |
|
$ordinal, |
254
|
1 |
|
\count(self::$names[static::class]) - 1 |
255
|
|
|
)); |
256
|
|
|
} |
257
|
|
|
|
258
|
38 |
|
$name = self::$names[static::class][$ordinal]; |
259
|
38 |
|
if (isset(self::$instances[static::class][$name])) { |
260
|
37 |
|
return self::$instances[static::class][$name]; |
261
|
|
|
} |
262
|
|
|
|
263
|
6 |
|
return self::$instances[static::class][$name] = new static(self::$constants[static::class][$name], $ordinal); |
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Get a list of enumerator instances ordered by ordinal number |
268
|
|
|
* |
269
|
|
|
* @return static[] |
270
|
|
|
*/ |
271
|
16 |
|
final public static function getEnumerators() |
272
|
|
|
{ |
273
|
16 |
|
if (!isset(self::$names[static::class])) { |
274
|
1 |
|
self::detectConstants(static::class); |
275
|
|
|
} |
276
|
16 |
|
return \array_map([static::class, 'byName'], self::$names[static::class]); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* Get a list of enumerator values ordered by ordinal number |
281
|
|
|
* |
282
|
|
|
* @return mixed[] |
283
|
|
|
*/ |
284
|
1 |
|
final public static function getValues() |
285
|
|
|
{ |
286
|
1 |
|
return \array_values(self::detectConstants(static::class)); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Get a list of enumerator names ordered by ordinal number |
291
|
|
|
* |
292
|
|
|
* @return string[] |
293
|
|
|
*/ |
294
|
2 |
|
final public static function getNames() |
295
|
|
|
{ |
296
|
2 |
|
if (!isset(self::$names[static::class])) { |
297
|
1 |
|
self::detectConstants(static::class); |
298
|
|
|
} |
299
|
2 |
|
return self::$names[static::class]; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Get a list of enumerator ordinal numbers |
304
|
|
|
* |
305
|
|
|
* @return int[] |
306
|
|
|
*/ |
307
|
1 |
|
final public static function getOrdinals() |
308
|
|
|
{ |
309
|
1 |
|
$count = \count(self::detectConstants(static::class)); |
310
|
1 |
|
return $count ? \range(0, $count - 1) : []; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Get all available constants of the called class |
315
|
|
|
* |
316
|
|
|
* @return array |
317
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
318
|
|
|
*/ |
319
|
114 |
|
final public static function getConstants() |
320
|
|
|
{ |
321
|
114 |
|
return self::detectConstants(static::class); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Test if the given enumerator is part of this enumeration |
326
|
|
|
* |
327
|
|
|
* @param static|null|bool|int|float|string|array $enumerator |
|
|
|
|
328
|
|
|
* @return bool |
|
|
|
|
329
|
|
|
*/ |
330
|
1 |
|
final public static function has($enumerator) |
331
|
|
|
{ |
332
|
1 |
|
return ($enumerator instanceof static && \get_class($enumerator) === static::class) |
333
|
1 |
|
|| static::hasValue($enumerator); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Test if the given enumerator value is part of this enumeration |
338
|
|
|
* |
339
|
|
|
* @param null|bool|int|float|string|array $value |
|
|
|
|
340
|
|
|
* @return bool |
|
|
|
|
341
|
|
|
*/ |
342
|
2 |
|
final public static function hasValue($value) |
343
|
|
|
{ |
344
|
2 |
|
$constants = self::detectConstants(static::class); |
345
|
2 |
|
return \in_array($value, $constants, true); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Test if the given enumerator name is part of this enumeration |
350
|
|
|
* |
351
|
|
|
* @param string $name |
|
|
|
|
352
|
|
|
* @return bool |
|
|
|
|
353
|
|
|
*/ |
354
|
1 |
|
final public static function hasName(string $name) |
355
|
|
|
{ |
356
|
1 |
|
return \defined("static::{$name}"); |
|
|
|
|
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Detect all public available constants of given enumeration class |
361
|
|
|
* |
362
|
|
|
* @param string $class |
|
|
|
|
363
|
|
|
* @return array |
364
|
|
|
*/ |
365
|
136 |
|
private static function detectConstants($class) |
|
|
|
|
366
|
|
|
{ |
367
|
136 |
|
if (!isset(self::$constants[$class])) { |
368
|
42 |
|
$reflection = new ReflectionClass($class); |
369
|
42 |
|
$constants = []; |
370
|
|
|
|
371
|
|
|
do { |
372
|
42 |
|
$scopeConstants = []; |
373
|
|
|
// Enumerators must be defined as public class constants |
374
|
42 |
|
foreach ($reflection->getReflectionConstants() as $reflConstant) { |
375
|
41 |
|
if ($reflConstant->isPublic()) { |
376
|
41 |
|
$scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
42 |
|
$constants = $scopeConstants + $constants; |
381
|
42 |
|
} while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
|
|
|
382
|
|
|
|
383
|
42 |
|
assert( |
384
|
42 |
|
self::noAmbiguousValues($constants), |
385
|
42 |
|
"Ambiguous enumerator values detected for {$class}" |
|
|
|
|
386
|
|
|
); |
387
|
|
|
|
388
|
40 |
|
self::$constants[$class] = $constants; |
389
|
40 |
|
self::$names[$class] = \array_keys($constants); |
390
|
|
|
} |
391
|
|
|
|
392
|
134 |
|
return self::$constants[$class]; |
393
|
|
|
} |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Test that the given constants does not contain ambiguous values |
397
|
|
|
* @param array $constants |
|
|
|
|
398
|
|
|
* @return bool |
|
|
|
|
399
|
|
|
*/ |
400
|
42 |
|
private static function noAmbiguousValues($constants) |
|
|
|
|
401
|
|
|
{ |
402
|
42 |
|
foreach ($constants as $value) { |
403
|
41 |
|
$names = \array_keys($constants, $value, true); |
404
|
41 |
|
if (\count($names) > 1) { |
405
|
4 |
|
return false; |
406
|
|
|
} |
407
|
|
|
} |
408
|
|
|
|
409
|
38 |
|
return true; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Get an enumerator instance by the given name. |
414
|
|
|
* |
415
|
|
|
* This will be called automatically on calling a method |
416
|
|
|
* with the same name of a defined enumerator. |
417
|
|
|
* |
418
|
|
|
* @param string $method The name of the enumerator (called as method) |
|
|
|
|
419
|
|
|
* @param array $args There should be no arguments |
|
|
|
|
420
|
|
|
* @return static |
421
|
|
|
* @throws InvalidArgumentException On an invalid or unknown name |
|
|
|
|
422
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
423
|
|
|
*/ |
424
|
35 |
|
final public static function __callStatic(string $method, array $args) |
425
|
|
|
{ |
426
|
35 |
|
return self::byName($method); |
427
|
|
|
} |
428
|
|
|
} |
429
|
|
|
|