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