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