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