1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace DaveRandom\Enum; |
4
|
|
|
|
5
|
|
|
abstract class Enum |
6
|
|
|
{ |
7
|
|
|
private static $constantCache = []; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Get a map of member names to values for the specified class name |
11
|
|
|
* |
12
|
|
|
* @param string $className |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
19 |
|
private static function getClassConstants(string $className): array |
16
|
|
|
{ |
17
|
19 |
|
return self::$constantCache[$className] |
18
|
19 |
|
?? self::$constantCache[$className] = (new \ReflectionClass($className))->getConstants(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param array $array |
23
|
|
|
* @param string $string |
24
|
|
|
* @return string|null |
25
|
|
|
*/ |
26
|
4 |
|
private static function searchArrayCaseInsensitive(array $array, string $string) |
27
|
|
|
{ |
28
|
4 |
|
foreach ($array as $key => $value) { |
29
|
4 |
|
if (\strcasecmp($string, $key) === 0) { |
30
|
4 |
|
return $key; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return null; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
final public static function valueExists($searchValue, bool $looseComparison = false): bool |
38
|
|
|
{ |
39
|
4 |
|
return false !== \array_search($searchValue, self::getClassConstants(static::class), !$looseComparison); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the name of the first member with the specified value |
44
|
|
|
* |
45
|
|
|
* @param mixed $searchValue |
46
|
|
|
* @param bool $looseComparison |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
4 |
|
final public static function parseValue($searchValue, bool $looseComparison = false): string |
50
|
|
|
{ |
51
|
4 |
|
if (false !== $name = \array_search($searchValue, self::getClassConstants(static::class), !$looseComparison)) { |
52
|
2 |
|
return $name; |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
throw new \InvalidArgumentException('Unknown enumeration value: ' . $searchValue); |
56
|
|
|
} |
57
|
|
|
|
58
|
5 |
|
final public static function nameExists(string $searchName, bool $caseInsensitive = false): bool |
59
|
|
|
{ |
60
|
5 |
|
$constants = self::getClassConstants(static::class); |
61
|
|
|
|
62
|
5 |
|
return isset($constants[$searchName]) |
63
|
5 |
|
|| ($caseInsensitive && null !== self::searchArrayCaseInsensitive($constants, $searchName)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the value of the member with the specified name |
68
|
|
|
* |
69
|
|
|
* @param string $searchName |
70
|
|
|
* @param bool $caseInsensitive |
71
|
|
|
* @return mixed |
72
|
|
|
*/ |
73
|
5 |
|
final public static function parseName(string $searchName, bool $caseInsensitive = false) |
74
|
|
|
{ |
75
|
5 |
|
$constants = self::getClassConstants(static::class); |
76
|
|
|
|
77
|
5 |
|
if (isset($constants[$searchName])) { |
78
|
1 |
|
return $constants[$searchName]; |
79
|
|
|
} |
80
|
|
|
|
81
|
4 |
|
if ($caseInsensitive && null !== $key = self::searchArrayCaseInsensitive($constants, $searchName)) { |
82
|
1 |
|
return $constants[$key]; |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
throw new \InvalidArgumentException('Unknown enumeration member: ' . $searchName); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
final public static function toArray(): array |
89
|
|
|
{ |
90
|
1 |
|
return self::getClassConstants(static::class); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
final protected function __construct() { } |
94
|
|
|
} |
95
|
|
|
|