|
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
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
|
13
|
|
|
* @copyright Copyright (c) 2015 Marc Bennewitz |
|
14
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class Enum |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* The selected enumerator value |
|
20
|
|
|
* |
|
21
|
|
|
* @var null|bool|int|float|string |
|
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
|
|
|
* An array of available constants by class |
|
34
|
|
|
* |
|
35
|
|
|
* @var array ["$class" => ["$name" => $value, ...], ...] |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $constants = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Already instantiated enumerators |
|
41
|
|
|
* |
|
42
|
|
|
* @var array ["$class" => ["$name" => $instance, ...], ...] |
|
43
|
|
|
*/ |
|
44
|
|
|
private static $instances = array(); |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor |
|
48
|
|
|
* |
|
49
|
|
|
* @param null|bool|int|float|string $value The value of the enumerator |
|
50
|
|
|
* @param int|null $ordinal The ordinal number of the enumerator |
|
51
|
|
|
*/ |
|
52
|
29 |
|
final private function __construct($value, $ordinal = null) |
|
53
|
|
|
{ |
|
54
|
29 |
|
$this->value = $value; |
|
55
|
29 |
|
$this->ordinal = $ordinal; |
|
56
|
29 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the name of the enumerator |
|
60
|
|
|
* |
|
61
|
|
|
* @return string |
|
62
|
|
|
* @see getName() |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function __toString() |
|
65
|
|
|
{ |
|
66
|
1 |
|
return $this->getName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @throws LogicException Enums are not cloneable |
|
71
|
|
|
* because instances are implemented as singletons |
|
72
|
|
|
*/ |
|
73
|
1 |
|
final private function __clone() |
|
74
|
|
|
{ |
|
75
|
1 |
|
throw new LogicException('Enums are not cloneable'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @throws LogicException Enums are not serializable |
|
80
|
|
|
* because instances are implemented as singletons |
|
81
|
|
|
*/ |
|
82
|
1 |
|
final public function __sleep() |
|
83
|
|
|
{ |
|
84
|
1 |
|
throw new LogicException('Enums are not serializable'); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws LogicException Enums are not serializable |
|
89
|
|
|
* because instances are implemented as singletons |
|
90
|
|
|
*/ |
|
91
|
1 |
|
final public function __wakeup() |
|
92
|
|
|
{ |
|
93
|
1 |
|
throw new LogicException('Enums are not serializable'); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Get the value of the enumerator |
|
98
|
|
|
* |
|
99
|
|
|
* @return null|bool|int|float|string |
|
100
|
|
|
*/ |
|
101
|
9 |
|
final public function getValue() |
|
102
|
|
|
{ |
|
103
|
9 |
|
return $this->value; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Get the name of the enumarator |
|
108
|
|
|
* |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
6 |
|
final public function getName() |
|
112
|
|
|
{ |
|
113
|
6 |
|
return array_search($this->value, self::detectConstants(get_called_class()), true); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Get the ordinal number of the enumerator |
|
118
|
|
|
* |
|
119
|
|
|
* @return int |
|
120
|
|
|
*/ |
|
121
|
17 |
|
final public function getOrdinal() |
|
122
|
|
|
{ |
|
123
|
17 |
|
if ($this->ordinal !== null) { |
|
124
|
15 |
|
return $this->ordinal; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
// detect ordinal |
|
128
|
8 |
|
$ordinal = 0; |
|
129
|
8 |
|
$value = $this->value; |
|
130
|
8 |
|
foreach (self::detectConstants(get_called_class()) as $constValue) { |
|
131
|
8 |
|
if ($value === $constValue) { |
|
132
|
8 |
|
break; |
|
133
|
|
|
} |
|
134
|
7 |
|
++$ordinal; |
|
135
|
8 |
|
} |
|
136
|
|
|
|
|
137
|
8 |
|
$this->ordinal = $ordinal; |
|
138
|
8 |
|
return $ordinal; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Compare this enumerator against another and check if it's the same. |
|
143
|
|
|
* |
|
144
|
|
|
* @param mixed $enumerator |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
1 |
|
final public function is($enumerator) |
|
148
|
|
|
{ |
|
149
|
1 |
|
return $this === $enumerator || $this->value === $enumerator; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Get an enumerator instance of the given value or instance |
|
154
|
|
|
* |
|
155
|
|
|
* @param static|null|bool|int|float|string $value |
|
156
|
|
|
* @return static |
|
157
|
|
|
* @throws InvalidArgumentException On an unknwon or invalid value |
|
158
|
|
|
* @throws LogicException On ambiguous constant values |
|
159
|
|
|
*/ |
|
160
|
40 |
|
final public static function get($value) |
|
161
|
|
|
{ |
|
162
|
40 |
|
if ($value instanceof static && get_class($value) === get_called_class()) { |
|
163
|
15 |
|
return $value; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
29 |
|
$class = get_called_class(); |
|
167
|
29 |
|
$constants = self::detectConstants($class); |
|
168
|
27 |
|
$name = array_search($value, $constants, true); |
|
169
|
27 |
View Code Duplication |
if ($name === false) { |
|
170
|
7 |
|
$message = is_scalar($value) |
|
171
|
7 |
|
? 'Unknown value ' . var_export($value, true) |
|
172
|
7 |
|
: 'Invalid value of type ' . (is_object($value) ? get_class($value) : gettype($value)); |
|
173
|
7 |
|
throw new InvalidArgumentException($message); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
21 |
|
if (!isset(self::$instances[$class][$name])) { |
|
177
|
15 |
|
self::$instances[$class][$name] = new $class($constants[$name]); |
|
178
|
15 |
|
} |
|
179
|
|
|
|
|
180
|
21 |
|
return self::$instances[$class][$name]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Get an enumarator instance by the given name |
|
185
|
|
|
* |
|
186
|
|
|
* @param string $name The name of the enumerator |
|
187
|
|
|
* @return static |
|
188
|
|
|
* @throws InvalidArgumentException On an invalid or unknown name |
|
189
|
|
|
* @throws LogicException On ambiguous values |
|
190
|
|
|
*/ |
|
191
|
24 |
|
final public static function getByName($name) |
|
192
|
|
|
{ |
|
193
|
24 |
|
$name = (string) $name; |
|
194
|
24 |
|
$class = get_called_class(); |
|
195
|
24 |
|
if (isset(self::$instances[$class][$name])) { |
|
196
|
16 |
|
return self::$instances[$class][$name]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
12 |
|
$const = $class . '::' . $name; |
|
200
|
12 |
|
if (!defined($const)) { |
|
201
|
1 |
|
throw new InvalidArgumentException($const . ' not defined'); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
11 |
|
return self::$instances[$class][$name] = new $class(constant($const)); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Get an enumeration instance by the given ordinal number |
|
209
|
|
|
* |
|
210
|
|
|
* @param int $ordinal The ordinal number or the enumerator |
|
211
|
|
|
* @return static |
|
212
|
|
|
* @throws InvalidArgumentException On an invalid ordinal number |
|
213
|
|
|
* @throws LogicException On ambiguous values |
|
214
|
|
|
*/ |
|
215
|
8 |
|
final public static function getByOrdinal($ordinal) |
|
216
|
|
|
{ |
|
217
|
8 |
|
$ordinal = (int) $ordinal; |
|
218
|
8 |
|
$class = get_called_class(); |
|
219
|
8 |
|
$constants = self::detectConstants($class); |
|
220
|
8 |
|
$item = array_slice($constants, $ordinal, 1, true); |
|
221
|
8 |
|
if (empty($item)) { |
|
222
|
1 |
|
throw new InvalidArgumentException(sprintf( |
|
223
|
1 |
|
'Invalid ordinal number, must between 0 and %s', |
|
224
|
1 |
|
count($constants) - 1 |
|
225
|
1 |
|
)); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
7 |
|
$name = key($item); |
|
229
|
7 |
|
if (isset(self::$instances[$class][$name])) { |
|
230
|
5 |
|
return self::$instances[$class][$name]; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
3 |
|
return self::$instances[$class][$name] = new $class(current($item), $ordinal); |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Clear all instantiated enumerators of the called class |
|
238
|
|
|
* |
|
239
|
|
|
* NOTE: This can break singleton behavior ... use it with caution! |
|
240
|
|
|
* |
|
241
|
|
|
* @return void |
|
242
|
|
|
*/ |
|
243
|
31 |
|
final public static function clear() |
|
244
|
|
|
{ |
|
245
|
31 |
|
$class = get_called_class(); |
|
246
|
31 |
|
unset(self::$instances[$class], self::$constants[$class]); |
|
247
|
31 |
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Get a list of enumerator instances ordered by ordinal number |
|
251
|
|
|
* |
|
252
|
|
|
* @return static[] |
|
253
|
|
|
*/ |
|
254
|
1 |
|
final public static function getEnumerators() |
|
255
|
|
|
{ |
|
256
|
1 |
|
return array_map('self::getByName', array_keys(self::detectConstants(get_called_class()))); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Get all available constants of the called class |
|
261
|
|
|
* |
|
262
|
|
|
* @return array |
|
263
|
|
|
* @throws LogicException On ambiguous constant values |
|
264
|
|
|
*/ |
|
265
|
25 |
|
final public static function getConstants() |
|
266
|
|
|
{ |
|
267
|
25 |
|
return self::detectConstants(get_called_class()); |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Is the given enumerator part of this enumeration |
|
272
|
|
|
* |
|
273
|
|
|
* @param static|null|bool|int|float|string $value |
|
274
|
|
|
* @return bool |
|
275
|
|
|
*/ |
|
276
|
1 |
|
final public static function has($value) |
|
277
|
|
|
{ |
|
278
|
1 |
|
if ($value instanceof static && get_class($value) === get_called_class()) { |
|
279
|
1 |
|
return true; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
1 |
|
$class = get_called_class(); |
|
283
|
1 |
|
$constants = self::detectConstants($class); |
|
284
|
|
|
|
|
285
|
1 |
|
return in_array($value, $constants, true); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Detect all available constants by the given class |
|
290
|
|
|
* |
|
291
|
|
|
* @param string $class |
|
292
|
|
|
* @return array |
|
293
|
|
|
* @throws LogicException On ambiguous constant values |
|
294
|
|
|
*/ |
|
295
|
45 |
|
private static function detectConstants($class) |
|
296
|
|
|
{ |
|
297
|
45 |
|
if (!isset(self::$constants[$class])) { |
|
298
|
28 |
|
$reflection = new ReflectionClass($class); |
|
299
|
28 |
|
$ambiguous = array(); |
|
|
|
|
|
|
300
|
|
|
|
|
301
|
28 |
|
if (PHP_VERSION_ID >= 70100) { |
|
302
|
|
|
// Since PHP-7.1 visibility modifiers are allowed for class constants |
|
303
|
|
|
// for enumerations we are only interected in public once. |
|
304
|
|
|
$constants = array(); |
|
305
|
|
|
foreach ($reflection->getReflectionConstants() as $reflConstant) { |
|
306
|
|
|
if ($reflConstant->isPublic()) { |
|
307
|
|
|
$constants[ $reflConstant->getName() ] = $reflConstant->getValue(); |
|
308
|
|
|
} |
|
309
|
|
|
} |
|
310
|
|
|
} else { |
|
311
|
|
|
// In PHP < 7.1 all class constants were public by definition |
|
312
|
28 |
|
$constants = $reflection->getConstants(); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
// Detect ambiguous values and report names |
|
316
|
28 |
|
$ambiguous = array(); |
|
317
|
28 |
|
foreach ($constants as $value) { |
|
318
|
28 |
|
$names = array_keys($constants, $value, true); |
|
319
|
28 |
|
if (count($names) > 1) { |
|
320
|
2 |
|
$ambiguous[var_export($value, true)] = $names; |
|
321
|
2 |
|
} |
|
322
|
28 |
|
} |
|
323
|
28 |
|
if (!empty($ambiguous)) { |
|
324
|
2 |
|
throw new LogicException( |
|
325
|
|
|
'All possible values needs to be unique. The following are ambiguous: ' |
|
326
|
2 |
|
. implode(', ', array_map(function ($names) use ($constants) { |
|
327
|
2 |
|
return implode('/', $names) . '=' . var_export($constants[$names[0]], true); |
|
328
|
2 |
|
}, $ambiguous)) |
|
329
|
2 |
|
); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
// This is required to make sure that constants of base classes will be the first |
|
333
|
26 |
|
while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__) { |
|
334
|
8 |
|
$constants = $reflection->getConstants() + $constants; |
|
335
|
8 |
|
} |
|
336
|
|
|
|
|
337
|
26 |
|
self::$constants[$class] = $constants; |
|
338
|
26 |
|
} |
|
339
|
|
|
|
|
340
|
43 |
|
return self::$constants[$class]; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* Get an enumarator instance by the given name. |
|
345
|
|
|
* |
|
346
|
|
|
* This will be called automatically on calling a method |
|
347
|
|
|
* with the same name of a defined enumerator. |
|
348
|
|
|
* |
|
349
|
|
|
* @param string $method The name of the enumeraotr (called as method) |
|
350
|
|
|
* @param array $args There should be no arguments |
|
351
|
|
|
* @return static |
|
352
|
|
|
* @throws InvalidArgumentException On an invalid or unknown name |
|
353
|
|
|
* @throws LogicException On ambiguous constant values |
|
354
|
|
|
*/ |
|
355
|
21 |
|
final public static function __callStatic($method, array $args) |
|
356
|
|
|
{ |
|
357
|
21 |
|
return self::getByName($method); |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.