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