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
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
|
|
|
|
15
|
|
|
* @copyright Copyright (c) 2017 Marc Bennewitz |
|
|
|
|
16
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
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
|
|
|
final private function __construct($value, int $ordinal = null) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->value = $value; |
64
|
|
|
$this->ordinal = $ordinal; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get the name of the enumerator |
69
|
|
|
* |
70
|
|
|
* @return string |
71
|
|
|
* @see getName() |
72
|
|
|
*/ |
73
|
|
|
public function __toString(): string |
74
|
|
|
{ |
75
|
|
|
return $this->getName(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @throws LogicException Enums are not cloneable |
|
|
|
|
80
|
|
|
* because instances are implemented as singletons |
81
|
|
|
*/ |
|
|
|
|
82
|
|
|
final private function __clone() |
83
|
|
|
{ |
84
|
|
|
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
|
|
|
final public function __sleep() |
92
|
|
|
{ |
93
|
|
|
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
|
|
|
final public function __wakeup() |
101
|
|
|
{ |
102
|
|
|
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
|
|
|
final public function getValue() |
111
|
|
|
{ |
112
|
|
|
return $this->value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Get the name of the enumerator |
117
|
|
|
* |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
final public function getName(): string |
121
|
|
|
{ |
122
|
|
|
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
|
|
|
final public function getOrdinal(): int |
131
|
|
|
{ |
132
|
|
|
if ($this->ordinal === null) { |
133
|
|
|
$ordinal = 0; |
134
|
|
|
$value = $this->value; |
135
|
|
|
foreach (self::detectConstants(static::class) as $constValue) { |
136
|
|
|
if ($value === $constValue) { |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
++$ordinal; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->ordinal = $ordinal; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
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
|
|
|
final public function is($enumerator): bool |
155
|
|
|
{ |
156
|
|
|
return $this === $enumerator || $this->value === $enumerator |
157
|
|
|
|
158
|
|
|
// The following additional conditions are required only because of the issue of serializable singletons |
|
|
|
|
159
|
|
|
|| ($enumerator instanceof static |
160
|
|
|
&& \get_class($enumerator) === static::class |
161
|
|
|
&& $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
|
|
|
final public static function get($enumerator): self |
174
|
|
|
{ |
175
|
|
|
if ($enumerator instanceof static && \get_class($enumerator) === static::class) { |
176
|
|
|
return $enumerator; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return static::byValue($enumerator); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get an enumerator instance by the given value |
184
|
|
|
* |
185
|
|
|
* @param mixed $value The value of the enumerator |
|
|
|
|
186
|
|
|
* @return static |
187
|
|
|
* @throws InvalidArgumentException On an unknwon or invalid value |
|
|
|
|
188
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
189
|
|
|
*/ |
190
|
|
|
final public static function byValue($value): self |
191
|
|
|
{ |
192
|
|
|
if (!isset(self::$constants[static::class])) { |
193
|
|
|
self::detectConstants(static::class); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
$name = \array_search($value, self::$constants[static::class], true); |
197
|
|
|
if ($name === false) { |
198
|
|
|
throw new InvalidArgumentException(sprintf( |
199
|
|
|
'Unknown value %s for enumeration %s', |
200
|
|
|
\is_scalar($value) |
201
|
|
|
? \var_export($value, true) |
|
|
|
|
202
|
|
|
: 'of type ' . (\is_object($value) ? \get_class($value) : \gettype($value)), |
|
|
|
|
203
|
|
|
static::class |
204
|
|
|
)); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
if (!isset(self::$instances[static::class][$name])) { |
208
|
|
|
self::$instances[static::class][$name] = new static(self::$constants[static::class][$name]); |
|
|
|
|
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
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
|
|
|
final public static function byName(string $name): self |
223
|
|
|
{ |
224
|
|
|
if (isset(self::$instances[static::class][$name])) { |
225
|
|
|
return self::$instances[static::class][$name]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
$const = static::class . "::{$name}"; |
|
|
|
|
229
|
|
|
if (!\defined($const)) { |
230
|
|
|
throw new InvalidArgumentException("{$const} not defined"); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
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
|
|
|
final public static function byOrdinal(int $ordinal): self |
245
|
|
|
{ |
246
|
|
|
if (!isset(self::$names[static::class])) { |
247
|
|
|
self::detectConstants(static::class); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (!isset(self::$names[static::class][$ordinal])) { |
251
|
|
|
throw new InvalidArgumentException(\sprintf( |
252
|
|
|
'Invalid ordinal number, must between 0 and %s', |
253
|
|
|
\count(self::$names[static::class]) - 1 |
254
|
|
|
)); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$name = self::$names[static::class][$ordinal]; |
258
|
|
|
if (isset(self::$instances[static::class][$name])) { |
259
|
|
|
return self::$instances[static::class][$name]; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
return self::$instances[static::class][$name] = new static(self::$constants[static::class][$name], $ordinal); |
|
|
|
|
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Get a list of enumerator instances ordered by ordinal number |
267
|
|
|
* |
268
|
|
|
* @return static[] |
269
|
|
|
*/ |
270
|
|
|
final public static function getEnumerators(): array |
271
|
|
|
{ |
272
|
|
|
if (!isset(self::$names[static::class])) { |
273
|
|
|
self::detectConstants(static::class); |
274
|
|
|
} |
275
|
|
|
return \array_map([static::class, 'byName'], self::$names[static::class]); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get a list of enumerator values ordered by ordinal number |
280
|
|
|
* |
281
|
|
|
* @return mixed[] |
282
|
|
|
*/ |
283
|
|
|
final public static function getValues(): array |
284
|
|
|
{ |
285
|
|
|
return \array_values(self::detectConstants(static::class)); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get a list of enumerator names ordered by ordinal number |
290
|
|
|
* |
291
|
|
|
* @return string[] |
292
|
|
|
*/ |
293
|
|
|
final public static function getNames(): array |
294
|
|
|
{ |
295
|
|
|
if (!isset(self::$names[static::class])) { |
296
|
|
|
self::detectConstants(static::class); |
297
|
|
|
} |
298
|
|
|
return self::$names[static::class]; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Get a list of enumerator ordinal numbers |
303
|
|
|
* |
304
|
|
|
* @return int[] |
305
|
|
|
*/ |
306
|
|
|
final public static function getOrdinals(): array |
307
|
|
|
{ |
308
|
|
|
$count = \count(self::detectConstants(static::class)); |
309
|
|
|
return $count ? \range(0, $count - 1) : []; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get all available constants of the called class |
314
|
|
|
* |
315
|
|
|
* @return array |
316
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
317
|
|
|
*/ |
318
|
|
|
final public static function getConstants(): array |
319
|
|
|
{ |
320
|
|
|
return self::detectConstants(static::class); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Test if the given enumerator is part of this enumeration |
325
|
|
|
* |
326
|
|
|
* @param static|null|bool|int|float|string|array $enumerator |
|
|
|
|
327
|
|
|
* @return bool |
|
|
|
|
328
|
|
|
*/ |
329
|
|
|
final public static function has($enumerator): bool |
330
|
|
|
{ |
331
|
|
|
return ($enumerator instanceof static && \get_class($enumerator) === static::class) |
332
|
|
|
|| static::hasValue($enumerator); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Test if the given enumerator value is part of this enumeration |
337
|
|
|
* |
338
|
|
|
* @param null|bool|int|float|string|array $value |
|
|
|
|
339
|
|
|
* @return bool |
|
|
|
|
340
|
|
|
*/ |
341
|
|
|
final public static function hasValue($value): bool |
342
|
|
|
{ |
343
|
|
|
$constants = self::detectConstants(static::class); |
344
|
|
|
return \in_array($value, $constants, true); |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Test if the given enumerator name is part of this enumeration |
349
|
|
|
* |
350
|
|
|
* @param string $name |
|
|
|
|
351
|
|
|
* @return bool |
|
|
|
|
352
|
|
|
*/ |
353
|
|
|
final public static function hasName(string $name): bool |
354
|
|
|
{ |
355
|
|
|
return \defined("static::{$name}"); |
|
|
|
|
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Detect all public available constants of given enumeration class |
360
|
|
|
* |
361
|
|
|
* @param string $class |
|
|
|
|
362
|
|
|
* @return array |
363
|
|
|
*/ |
364
|
|
|
private static function detectConstants(string $class): array |
365
|
|
|
{ |
366
|
|
|
if (!isset(self::$constants[$class])) { |
367
|
|
|
$reflection = new ReflectionClass($class); |
368
|
|
|
$allConstants = []; |
369
|
|
|
|
370
|
|
|
do { |
371
|
|
|
$scopeConstants = []; |
372
|
|
|
// Enumerators must be defined as public class constants |
373
|
|
|
foreach ($reflection->getReflectionConstants() as $reflConstant) { |
374
|
|
|
if ($reflConstant->isPublic()) { |
375
|
|
|
$scopeConstants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
376
|
|
|
} |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
$allConstants = $scopeConstants + $allConstants; |
380
|
|
|
} while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__); |
|
|
|
|
381
|
|
|
|
382
|
|
|
assert( |
383
|
|
|
self::noAmbiguousValues($allConstants), |
384
|
|
|
"Ambiguous enumerator values detected for {$class}" |
|
|
|
|
385
|
|
|
); |
386
|
|
|
|
387
|
|
|
self::$constants[$class] = $allConstants; |
388
|
|
|
self::$names[$class] = \array_keys($allConstants); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
return self::$constants[$class]; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Test that the given constants does not contain ambiguous values |
396
|
|
|
* @param array $constants |
|
|
|
|
397
|
|
|
* @return bool |
|
|
|
|
398
|
|
|
*/ |
399
|
|
|
private static function noAmbiguousValues(array $constants): bool |
400
|
|
|
{ |
401
|
|
|
foreach ($constants as $value) { |
402
|
|
|
$names = \array_keys($constants, $value, true); |
403
|
|
|
if (\count($names) > 1) { |
404
|
|
|
return false; |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
return true; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Get an enumerator instance by the given name. |
413
|
|
|
* |
414
|
|
|
* This will be called automatically on calling a method |
415
|
|
|
* with the same name of a defined enumerator. |
416
|
|
|
* |
417
|
|
|
* @param string $method The name of the enumerator (called as method) |
|
|
|
|
418
|
|
|
* @param array $args There should be no arguments |
|
|
|
|
419
|
|
|
* @return static |
420
|
|
|
* @throws InvalidArgumentException On an invalid or unknown name |
|
|
|
|
421
|
|
|
* @throws LogicException On ambiguous constant values |
|
|
|
|
422
|
|
|
*/ |
423
|
|
|
final public static function __callStatic(string $method, array $args): self |
424
|
|
|
{ |
425
|
|
|
return self::byName($method); |
426
|
|
|
} |
427
|
|
|
} |
428
|
|
|
|