1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace vijinho\Enums; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract class for implementing ENUMs in PHP7 |
7
|
|
|
* |
8
|
|
|
* @author Vijay Mahrra <[email protected]> |
9
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
10
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
11
|
|
|
* @link http://php.net/manual/en/class.serializable.php |
12
|
|
|
* @link http://php.net/manual/en/class.arrayaccess.php |
13
|
|
|
*/ |
14
|
|
|
class Enum implements \Serializable, \ArrayAccess |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* base enum values |
18
|
|
|
* |
19
|
|
|
* @var array $values |
20
|
|
|
*/ |
21
|
|
|
protected static $values = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* allow keys to be unset/deleted? |
25
|
|
|
* cannot be over-ridden once defined in a class |
26
|
|
|
* |
27
|
|
|
* @var boolean |
28
|
|
|
*/ |
29
|
|
|
protected static $delete = false; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* allow keys to be over-written when adding? |
33
|
|
|
* cannot be over-ridden once defined in a class |
34
|
|
|
* |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
protected static $overwrite = false; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* always capitalize enum keys? |
41
|
|
|
* can be over-ridden once defined in a class |
42
|
|
|
* with capitalize(boolean) method |
43
|
|
|
* |
44
|
|
|
* @var boolean |
45
|
|
|
*/ |
46
|
|
|
protected static $capitalize = false; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* case-sensitive check when searching by key for a value? |
50
|
|
|
* can be over-ridden once defined in a class |
51
|
|
|
* with caseSensitive(boolean) method |
52
|
|
|
* |
53
|
|
|
* @var boolean |
54
|
|
|
*/ |
55
|
|
|
protected static $caseSensitive = false; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* initialize with array of additional values not existing already |
60
|
|
|
* |
61
|
|
|
* @param array static::$values |
62
|
|
|
*/ |
63
|
|
|
public function __construct(array $newValues = []) |
64
|
|
|
{ |
65
|
|
|
static::fixKeys(); |
66
|
|
|
static::add($newValues); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Set case-sensitivity when searching |
72
|
|
|
* |
73
|
|
|
* @param boolean $bool |
74
|
|
|
*/ |
75
|
2 |
|
public static function caseSensitive($bool = false) |
76
|
|
|
{ |
77
|
2 |
|
static::$caseSensitive = !empty($bool); |
78
|
2 |
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Set capitalization for enum keys |
83
|
|
|
* |
84
|
|
|
* @param boolean $bool |
85
|
|
|
*/ |
86
|
2 |
|
public static function capitalize($bool = false) |
87
|
|
|
{ |
88
|
2 |
|
static::$capitalize = !empty($bool); |
89
|
2 |
|
if (static::$capitalize) { |
90
|
1 |
|
static::$values = array_change_key_case(static::$values, CASE_UPPER); |
91
|
|
|
} |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Clear all data - use with care! |
97
|
|
|
* |
98
|
|
|
* @param boolean $caseSensitive |
99
|
|
|
* @param boolean $capitalize |
100
|
|
|
*/ |
101
|
1 |
|
public static function reset($caseSensitive = false, $capitalize = false) |
102
|
|
|
{ |
103
|
1 |
|
static::$values = []; |
104
|
1 |
|
static::$caseSensitive = $caseSensitive; |
105
|
1 |
|
static::$capitalize = $capitalize; |
106
|
1 |
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Remove an enum value - use with care! |
111
|
|
|
* |
112
|
|
|
*/ |
113
|
1 |
|
public static function delete($key) |
114
|
|
|
{ |
115
|
1 |
|
$allowed = !empty(static::$delete); |
116
|
1 |
|
if (empty($allowed)) { |
117
|
1 |
|
throw new \LogicException('Method not allowed.'); |
118
|
|
|
} |
119
|
|
|
$values = & static::$values; |
120
|
|
|
if (array_key_exists($key, $values)) { |
121
|
|
|
unset($values[$key]); |
122
|
|
|
} else { |
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
return true; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Make sure that the enum keys have no SPACEs |
131
|
|
|
* Make sure the keys are strings |
132
|
|
|
* Set the case according to the settings |
133
|
|
|
* |
134
|
|
|
* @param return array static::$values |
135
|
|
|
*/ |
136
|
2 |
|
public static function fixKeys(): array |
137
|
|
|
{ |
138
|
2 |
|
$values = static::$values; |
139
|
2 |
|
foreach ($values as $k => $v) { |
140
|
2 |
|
unset($values[$k]); |
141
|
2 |
|
if (!is_string($k)) { |
142
|
1 |
|
if (!is_string($v)) { |
143
|
|
|
throw new \UnexpectedValueException(sprintf("Key '%s' for value '%s' is not a string!", print_r($k, 1), print_r($v, 1))); |
144
|
|
|
} else { |
145
|
|
|
// if the key is not a string, use the value if it is a string |
146
|
1 |
|
$k = $v; |
147
|
|
|
} |
148
|
|
|
} |
149
|
2 |
|
$k = str_replace(' ', '_', $k); // space converted for magic calls |
150
|
2 |
|
$values[$k] = $v; |
151
|
|
|
} |
152
|
|
|
|
153
|
2 |
|
static::$values = empty(static::$capitalize) ? |
154
|
2 |
|
$values : array_change_key_case($values, CASE_UPPER); |
155
|
|
|
|
156
|
2 |
|
return static::$values; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* add array of extra values not existing already |
162
|
|
|
* |
163
|
|
|
* @param array|string $newValues |
164
|
|
|
* @param null|boolean $overwrite allow over-write of values? |
165
|
|
|
* @return array static::$values |
166
|
|
|
*/ |
167
|
1 |
|
public static function add($newValues, $overwrite = null): array |
168
|
|
|
{ |
169
|
1 |
|
if (empty(static::$overwrite) && !empty($overwrite)) { |
170
|
|
|
throw new \LogicException('Overwrite not allowed.'); |
171
|
|
|
} |
172
|
|
|
|
173
|
1 |
|
$values = & static::$values; |
174
|
|
|
|
175
|
|
|
// if it's a string, convert to array |
176
|
1 |
|
if (is_string($newValues)) { |
177
|
|
|
$newValues = [$newValues => $newValues]; |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
foreach ($newValues as $k => $v) { |
181
|
1 |
|
$overwrite = (null == $overwrite) ? static::$overwrite : $overwrite; |
182
|
1 |
|
if (!empty($overwrite)) { |
183
|
|
|
$values[$k] = $v; |
184
|
1 |
|
} elseif (!array_key_exists($k, $values)) { |
185
|
1 |
|
$values[$k] = $v; |
186
|
|
|
} |
187
|
|
|
} |
188
|
1 |
|
return static::fixKeys(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* get array of keys |
194
|
|
|
* |
195
|
|
|
* @return array keys of static::$values |
196
|
|
|
*/ |
197
|
1 |
|
public static function keys(): array |
198
|
|
|
{ |
199
|
1 |
|
return array_keys(static::$values); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* get key for the given value |
205
|
|
|
* |
206
|
|
|
* @param mixed $value |
207
|
|
|
* @param null|bool $caseSensitive search is case sensitive? |
208
|
|
|
* @return string|array key(s) |
|
|
|
|
209
|
|
|
*/ |
210
|
1 |
|
public static function key($value, $caseSensitive = null) |
211
|
|
|
{ |
212
|
1 |
|
$values = static::$values; |
213
|
1 |
|
if (!is_array($value) && !is_object($value)) { |
214
|
1 |
|
if (is_string($value)) { |
215
|
1 |
|
$value = strtoupper($value); |
216
|
|
|
// if case-sensitivity is not specified use the class boolean value |
217
|
1 |
|
if (null === $caseSensitive) { |
218
|
1 |
|
$caseSensitive = static::$caseSensitive; |
219
|
|
|
} |
220
|
1 |
|
if (empty($caseSensitive)) { |
221
|
1 |
|
$values = array_map(function($value) { |
222
|
1 |
|
return strtoupper($value); |
223
|
1 |
|
}, $values); |
224
|
|
|
} |
225
|
|
|
} |
226
|
1 |
|
$keys = array_keys($values, $value); |
227
|
1 |
|
$count = count($keys); |
228
|
1 |
|
if (0 === $count) { |
229
|
|
|
throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1))); |
230
|
|
|
} |
231
|
1 |
|
return count($keys) > 1 ? $keys : $keys[0]; |
232
|
|
|
} elseif (is_array($value)) { |
233
|
|
|
$search = array_search($value, $values); |
234
|
|
|
if (false === $search) { |
235
|
|
|
throw new \InvalidArgumentException(sprintf("Key for value '%s' does not exist.", print_r($value, 1))); |
236
|
|
|
} |
237
|
|
|
return $search; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* count values |
244
|
|
|
* |
245
|
|
|
* @return int number of values |
246
|
|
|
*/ |
247
|
1 |
|
public static function count(): int |
248
|
|
|
{ |
249
|
1 |
|
return count(static::$values); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* count values |
255
|
|
|
* |
256
|
|
|
* @return int number of values |
257
|
|
|
*/ |
258
|
1 |
|
public static function sizeof(): int |
259
|
|
|
{ |
260
|
1 |
|
return static::count(); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* get existing values |
266
|
|
|
* |
267
|
|
|
* @return array static::$values |
268
|
|
|
*/ |
269
|
1 |
|
public static function values(): array |
270
|
|
|
{ |
271
|
1 |
|
return static::$values; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* get value for the given key |
277
|
|
|
* |
278
|
|
|
* @param string $key |
279
|
|
|
* @param null|bool $caseSensitive search is case sensitive? |
280
|
|
|
* @return mixed static::$values[$key] |
281
|
|
|
*/ |
282
|
3 |
|
public static function value($key, $caseSensitive = null) |
283
|
|
|
{ |
284
|
3 |
|
$values = static::$values; |
285
|
|
|
// if case-sensitivity is not specified use the class boolean value |
286
|
3 |
|
if (null === $caseSensitive) { |
287
|
3 |
|
$caseSensitive = static::$caseSensitive; |
288
|
|
|
} |
289
|
3 |
|
if (empty($caseSensitive)) { |
290
|
2 |
|
$key = strtoupper($key); |
291
|
2 |
|
$values = array_change_key_case($values, CASE_UPPER); |
292
|
|
|
} |
293
|
3 |
|
if (false === array_key_exists($key, $values)) { |
294
|
1 |
|
throw new \InvalidArgumentException(sprintf("Value for key '%s' does not exist.", print_r($key, 1))); |
295
|
|
|
} |
296
|
3 |
|
return $values[$key]; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* get value for the given key |
302
|
|
|
* method allows getting value from an object with $object->key |
303
|
|
|
* |
304
|
|
|
* @return mixed static::$values[$key] |
305
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#object.get |
306
|
|
|
*/ |
307
|
|
|
public function __get(string $key) { |
308
|
|
|
return static::value($key); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* get value named the same as the method called |
314
|
|
|
* method allows getting value from an object with $object->key() |
315
|
|
|
* |
316
|
|
|
* @param string $key the method called is used as the key |
317
|
|
|
* @param array $args |
318
|
|
|
* @return mixed static::$values[$key] |
319
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#object.call |
320
|
|
|
*/ |
321
|
1 |
|
public function __call(string $key, array $args = []) { |
322
|
1 |
|
return static::value($key); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* get value named the same as the method called statically |
328
|
|
|
* method allows getting value from an object with $object::key() |
329
|
|
|
* |
330
|
|
|
* @param string $key the method called is used as the key |
331
|
|
|
* @param array $args |
332
|
|
|
* @return mixed static::$values[$key] |
333
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#object.callstatic |
334
|
|
|
*/ |
335
|
|
|
public static function __callStatic(string $key, array $args = []) { |
336
|
|
|
return static::value($key); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* check if the given key exists via call to $object of isset($object->key) |
342
|
|
|
* |
343
|
|
|
* @param string $key |
344
|
|
|
* @return boolean |
345
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#object.isset |
346
|
|
|
*/ |
347
|
1 |
|
public function __isset($key) |
348
|
|
|
{ |
349
|
1 |
|
return empty(static::$caseSensitive) ? |
350
|
1 |
|
array_key_exists(strtoupper($key), array_change_key_case(static::$values, CASE_UPPER)) : array_key_exists($key, static::$values); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* when called as a function this class will add new values and return the result |
356
|
|
|
* |
357
|
|
|
* @param array $newValues |
358
|
|
|
* @return boolean |
|
|
|
|
359
|
|
|
* @link http://php.net/manual/en/language.oop5.overloading.php#object.invoke |
360
|
|
|
*/ |
361
|
1 |
|
public function __invoke($newValues) |
362
|
|
|
{ |
363
|
1 |
|
return static::add($newValues); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
|
367
|
|
|
/** |
368
|
|
|
* return the values as a string |
369
|
|
|
* method allows outputting values as a string |
370
|
|
|
* |
371
|
|
|
* @return string json_encode(static::$values) |
372
|
|
|
* @link http://php.net/manual/en/language.oop5.magic.php#object.tostring |
373
|
|
|
*/ |
374
|
1 |
|
public function __toString(): string |
375
|
|
|
{ |
376
|
1 |
|
return json_encode(static::$values, JSON_PRETTY_PRINT); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* return the values as a string |
382
|
|
|
* method allows outputting values as a string when called statically |
383
|
|
|
* |
384
|
|
|
* @return string json_encode(static::$values) |
385
|
|
|
*/ |
386
|
|
|
public static function toString(): string |
387
|
|
|
{ |
388
|
|
|
return json_encode(static::$values, JSON_PRETTY_PRINT); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
/** |
393
|
|
|
* serialize enum values |
394
|
|
|
* |
395
|
|
|
* @return string enum values serialized |
396
|
|
|
* @link http://php.net/manual/en/class.serializable.php |
397
|
|
|
*/ |
398
|
1 |
|
public function serialize(): string |
399
|
|
|
{ |
400
|
1 |
|
return serialize(static::$values); |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* unserialize serialized values to object |
405
|
|
|
* |
406
|
|
|
* @return string enum values serialized |
407
|
|
|
* @link http://php.net/manual/en/class.serializable.php |
408
|
|
|
* @return void |
409
|
|
|
*/ |
410
|
1 |
|
public function unserialize($data) { |
411
|
1 |
|
$data = unserialize($data); |
412
|
1 |
|
static::$values = $data; |
413
|
1 |
|
} |
414
|
|
|
|
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* returned dump values as array |
418
|
|
|
* |
419
|
|
|
* @return array |
420
|
|
|
*/ |
421
|
|
|
public static function var_dump(): array |
422
|
|
|
{ |
423
|
|
|
$delete = static::$delete; |
424
|
|
|
$overwrite = static::$overwrite; |
425
|
|
|
$capitalize = static::$capitalize; |
426
|
|
|
$caseSensitive = static::$caseSensitive; |
427
|
|
|
$values = static::$values; |
428
|
|
|
return [ |
429
|
|
|
'overwrite' => $overwrite, |
430
|
|
|
'delete' => $delete, |
431
|
|
|
'capitalise' => $capitalize, |
432
|
|
|
'caseSensitive' => $caseSensitive, |
433
|
|
|
'values' => $values |
434
|
|
|
]; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* returned values when called with var_dump() |
440
|
|
|
* |
441
|
|
|
* @return array debug info |
442
|
|
|
* @link http://php.net/manual/en/language.oop5.magic.php#object.debuginfo |
443
|
|
|
*/ |
444
|
|
|
public function __debugInfo(): array |
445
|
|
|
{ |
446
|
|
|
return static::var_dump(); |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* Implement Array offsetSet |
451
|
|
|
* |
452
|
|
|
* @param string $key the key to set |
453
|
|
|
* @param mixed $value the value to set |
454
|
|
|
* @return array debug info |
|
|
|
|
455
|
|
|
* @link http://php.net/manual/en/class.arrayaccess.php |
456
|
|
|
*/ |
457
|
|
|
public function offsetSet($key, $value) { |
458
|
|
|
if (is_null($key)) { |
459
|
|
|
static::$values[] = $value; |
460
|
|
|
} else { |
461
|
|
|
static::$values[$key] = $value; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
|
466
|
|
|
/** |
467
|
|
|
* Implement Array offsetExists |
468
|
|
|
* |
469
|
|
|
* @param string $key the key to set |
470
|
|
|
* @param mixed $value the value to set |
|
|
|
|
471
|
|
|
* @return array debug info |
|
|
|
|
472
|
|
|
* @link http://php.net/manual/en/class.arrayaccess.php |
473
|
|
|
*/ |
474
|
1 |
|
public function offsetExists($key) { |
475
|
1 |
|
return isset(static::$values[$key]); |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
|
479
|
|
|
/** |
480
|
|
|
* Implement Array offsetUnset |
481
|
|
|
* |
482
|
|
|
* @param string $key the key to set |
483
|
|
|
* @param mixed $value the value to set |
|
|
|
|
484
|
|
|
* @return array debug info |
|
|
|
|
485
|
|
|
* @link http://php.net/manual/en/class.arrayaccess.php |
486
|
|
|
*/ |
487
|
1 |
|
public function offsetUnset($key) { |
488
|
1 |
|
if (!empty(static::$overwrite)) { |
489
|
|
|
throw new \LogicException('Overwrite not allowed.'); |
490
|
|
|
} |
491
|
1 |
|
unset(static::$values[$key]); |
492
|
1 |
|
} |
493
|
|
|
|
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Implement Array offsetGet |
497
|
|
|
* |
498
|
|
|
* @param string $key the key to set |
499
|
|
|
* @param mixed $value the value to set |
|
|
|
|
500
|
|
|
* @return array debug info |
501
|
|
|
* @link http://php.net/manual/en/class.arrayaccess.php |
502
|
|
|
*/ |
503
|
1 |
|
public function offsetGet($key) { |
504
|
1 |
|
return isset(static::$values[$key]) ? static::$values[$key] : null; |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.