1 | <?php |
||
19 | abstract class AbstractEnum |
||
20 | { |
||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected static $cache = []; |
||
25 | |||
26 | /** |
||
27 | * Return all constants as Key => Value array. |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | 292 | public static function toArray() |
|
32 | { |
||
33 | 292 | $class = get_called_class(); |
|
34 | 292 | if (!array_key_exists($class, static::$cache)) { |
|
35 | $reflection = new \ReflectionClass($class); |
||
36 | static::$cache[$class] = $reflection->getConstants(); |
||
37 | } |
||
38 | |||
39 | 292 | return static::$cache[$class]; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Return all constants Keys as array. |
||
44 | * |
||
45 | * @return array|string[] |
||
46 | */ |
||
47 | 32 | public static function getKeys() |
|
51 | |||
52 | /** |
||
53 | * Return Enum Key for requested Value. |
||
54 | * |
||
55 | * @param mixed $value |
||
56 | * |
||
57 | * @throws \InvalidArgumentException |
||
58 | * |
||
59 | * @return string |
||
60 | */ |
||
61 | 16 | public static function getKey($value) |
|
67 | |||
68 | /** |
||
69 | * Validate if Key is valid. |
||
70 | * |
||
71 | * @param string $key |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 80 | public static function isValidKey($key) |
|
79 | |||
80 | /** |
||
81 | * Throw exception if Key is not valid. |
||
82 | * |
||
83 | * @param string $key |
||
84 | * |
||
85 | * @throws \InvalidArgumentException |
||
86 | */ |
||
87 | 80 | public static function assertIsValidKey($key) |
|
93 | |||
94 | /** |
||
95 | * Return Enum Value for requested Key. |
||
96 | * |
||
97 | * @param string $key |
||
98 | * |
||
99 | * @throws \InvalidArgumentException |
||
100 | * |
||
101 | * @return mixed |
||
102 | */ |
||
103 | 16 | public static function getValue($key) |
|
109 | |||
110 | /** |
||
111 | * Return all constants Values as array. |
||
112 | * |
||
113 | * @return array |
||
114 | */ |
||
115 | 32 | public static function getValues() |
|
119 | |||
120 | /** |
||
121 | * Validate if Value is valid. |
||
122 | * |
||
123 | * @param mixed $value |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | 164 | public static function isValidValue($value) |
|
131 | |||
132 | /** |
||
133 | * Throw exception if Value is not valid. |
||
134 | * |
||
135 | * @param mixed $value |
||
136 | * |
||
137 | * @throws \InvalidArgumentException |
||
138 | */ |
||
139 | 164 | public static function assertIsValidValue($value) |
|
145 | } |
||
146 |