1 | <?php |
||
17 | abstract class AbstractEnum |
||
18 | { |
||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $cache = []; |
||
23 | |||
24 | /** |
||
25 | * Return all constants as Key => Value array. |
||
26 | * |
||
27 | * @return array |
||
28 | */ |
||
29 | 279 | public static function toArray() |
|
30 | { |
||
31 | 279 | $class = get_called_class(); |
|
32 | 279 | if (!array_key_exists($class, static::$cache)) { |
|
33 | $reflection = new \ReflectionClass($class); |
||
34 | static::$cache[$class] = $reflection->getConstants(); |
||
35 | } |
||
36 | |||
37 | 279 | return static::$cache[$class]; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * Return all constants Keys as array. |
||
42 | * |
||
43 | * @return array|string[] |
||
44 | */ |
||
45 | 30 | public static function getKeys() |
|
49 | |||
50 | /** |
||
51 | * Return Enum Key for requested Value. |
||
52 | * |
||
53 | * @param mixed $value |
||
54 | * |
||
55 | * @throws \InvalidArgumentException |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | 15 | public static function getKey($value) |
|
65 | |||
66 | /** |
||
67 | * Validate if Key is valid. |
||
68 | * |
||
69 | * @param string $key |
||
70 | * |
||
71 | * @return bool |
||
72 | */ |
||
73 | 75 | public static function isValidKey($key) |
|
77 | |||
78 | /** |
||
79 | * Throw exception if Key is not valid. |
||
80 | * |
||
81 | * @param string $key |
||
82 | * |
||
83 | * @throws \InvalidArgumentException |
||
84 | */ |
||
85 | 75 | public static function assertIsValidKey($key) |
|
91 | |||
92 | /** |
||
93 | * Return Enum Value for requested Key. |
||
94 | * |
||
95 | * @param string $key |
||
96 | * |
||
97 | * @throws \InvalidArgumentException |
||
98 | * |
||
99 | * @return mixed |
||
100 | */ |
||
101 | 15 | public static function getValue($key) |
|
107 | |||
108 | /** |
||
109 | * Return all constants Values as array. |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 30 | public static function getValues() |
|
117 | |||
118 | /** |
||
119 | * Validate if Value is valid. |
||
120 | * |
||
121 | * @param mixed $value |
||
122 | * |
||
123 | * @return bool |
||
124 | */ |
||
125 | 159 | public static function isValidValue($value) |
|
129 | |||
130 | /** |
||
131 | * Throw exception if Value is not valid. |
||
132 | * |
||
133 | * @param mixed $value |
||
134 | * |
||
135 | * @throws \InvalidArgumentException |
||
136 | */ |
||
137 | 159 | public static function assertIsValidValue($value) |
|
143 | } |
||
144 |