|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the WrikePhpLibrary package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Zbigniew Ślązak |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Zibios\WrikePhpLibrary\Enum; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Abstract Enum |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractEnum |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected static $cache = []; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
107 |
|
public static function toArray() |
|
27
|
|
|
{ |
|
28
|
107 |
|
$class = get_called_class(); |
|
29
|
107 |
|
if (!array_key_exists($class, static::$cache)) { |
|
30
|
|
|
$reflection = new \ReflectionClass($class); |
|
31
|
|
|
static::$cache[$class] = $reflection->getConstants(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
107 |
|
return static::$cache[$class]; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return array|string[] |
|
39
|
|
|
*/ |
|
40
|
14 |
|
public static function getKeys() |
|
41
|
|
|
{ |
|
42
|
14 |
|
return array_keys(static::toArray()); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param mixed $value |
|
47
|
|
|
* |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws \InvalidArgumentException |
|
50
|
|
|
*/ |
|
51
|
7 |
|
public static function getKey($value) |
|
52
|
|
|
{ |
|
53
|
7 |
|
self::assertIsValidValue($value); |
|
54
|
|
|
|
|
55
|
7 |
|
return (string) array_search($value, static::toArray(), true); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $key |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
35 |
|
public static function isValidKey($key) |
|
64
|
|
|
{ |
|
65
|
35 |
|
return array_key_exists($key, self::toArray()); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* |
|
71
|
|
|
* @throws \InvalidArgumentException |
|
72
|
|
|
*/ |
|
73
|
35 |
|
public static function assertIsValidKey($key) |
|
74
|
|
|
{ |
|
75
|
35 |
|
if (self::isValidKey($key) === false) { |
|
76
|
21 |
|
throw new \InvalidArgumentException('Wrong key.'); |
|
77
|
|
|
} |
|
78
|
14 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $key |
|
82
|
|
|
* |
|
83
|
|
|
* @return mixed |
|
84
|
|
|
* @throws \InvalidArgumentException |
|
85
|
|
|
*/ |
|
86
|
7 |
|
public static function getValue($key) |
|
87
|
|
|
{ |
|
88
|
7 |
|
self::assertIsValidKey($key); |
|
89
|
|
|
|
|
90
|
7 |
|
return static::toArray()[$key]; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
14 |
|
public static function getValues() |
|
97
|
|
|
{ |
|
98
|
14 |
|
return array_values(self::toArray()); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $value |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
51 |
|
public static function isValidValue($value) |
|
107
|
|
|
{ |
|
108
|
51 |
|
return in_array($value, static::toArray(), true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @throws \InvalidArgumentException |
|
113
|
|
|
* |
|
114
|
|
|
* @param mixed $value |
|
115
|
|
|
*/ |
|
116
|
51 |
|
public static function assertIsValidValue($value) |
|
117
|
|
|
{ |
|
118
|
51 |
|
if (self::isValidValue($value) === false) { |
|
119
|
21 |
|
throw new \InvalidArgumentException('Wrong value.'); |
|
120
|
|
|
} |
|
121
|
30 |
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|