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