1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CorpSoft\Enum; |
4
|
|
|
|
5
|
|
|
use BadMethodCallException; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
use UnexpectedValueException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class BaseEnum |
11
|
|
|
* |
12
|
|
|
* @package CorpSoft\Enum |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseEnum |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The cached list of constants by name. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected static $byName = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The cached list of constants by value. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected static $byValue = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array list of properties |
32
|
|
|
*/ |
33
|
|
|
protected static $list = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The value managed by this type instance. |
37
|
|
|
* |
38
|
|
|
* @var mixed |
39
|
|
|
*/ |
40
|
|
|
protected $value; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Sets the value that will be managed by this type instance. |
44
|
|
|
* |
45
|
|
|
* @param mixed $value The value to be managed |
46
|
|
|
* |
47
|
|
|
* @throws UnexpectedValueException If the value is not valid |
48
|
|
|
*/ |
49
|
|
|
public function __construct($value) |
50
|
|
|
{ |
51
|
|
|
if (!static::isValidValue($value)) { |
52
|
|
|
throw new UnexpectedValueException("Value '{$value}' is not part of the enum " . get_called_class()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->value = $value; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* @param array $arguments |
63
|
|
|
* |
64
|
|
|
* @throws BadMethodCallException |
65
|
|
|
* |
66
|
|
|
* @return static |
67
|
|
|
*/ |
68
|
|
|
public static function __callStatic($name, $arguments) |
69
|
|
|
{ |
70
|
|
|
$constants = static::getConstantsByName(); |
71
|
|
|
|
72
|
|
|
if (isset($constants[$name])) { |
73
|
|
|
return new static($constants[$name]); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
throw new BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function __toString(): string |
83
|
|
|
{ |
84
|
|
|
return (string) $this->value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Creates a new type instance using the name of a value. |
89
|
|
|
* |
90
|
|
|
* @param string $name The name of a value |
91
|
|
|
* |
92
|
|
|
* @throws UnexpectedValueException |
93
|
|
|
* |
94
|
|
|
* @return $this The new type instance |
95
|
|
|
*/ |
96
|
|
|
public static function createByName($name) |
97
|
|
|
{ |
98
|
|
|
$constants = static::getConstantsByName(); |
99
|
|
|
if (!array_key_exists($name, $constants)) { |
100
|
|
|
throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return new static($constants[$name]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* get constant key by value(label) |
108
|
|
|
* |
109
|
|
|
* @param $value |
110
|
|
|
* |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public static function getValueByName($value) |
114
|
|
|
{ |
115
|
|
|
return array_search($value, static::listData()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Creates a new type instance using the value. |
120
|
|
|
* |
121
|
|
|
* @param mixed $value The value |
122
|
|
|
* |
123
|
|
|
* @throws UnexpectedValueException |
124
|
|
|
* |
125
|
|
|
* @return $this The new type instance |
126
|
|
|
*/ |
127
|
|
|
public static function createByValue($value) |
128
|
|
|
{ |
129
|
|
|
if (!array_key_exists($value, static::getConstantsByValue())) { |
130
|
|
|
throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class()); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return new static($value); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get list data |
138
|
|
|
* |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
|
|
public static function listData() |
142
|
|
|
{ |
143
|
|
|
return static::$list; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get label by value |
148
|
|
|
* |
149
|
|
|
* @var string value |
150
|
|
|
* |
151
|
|
|
* @param mixed $value |
152
|
|
|
* |
153
|
|
|
* @return string label |
154
|
|
|
*/ |
155
|
|
|
public static function getLabel($value): ?string |
156
|
|
|
{ |
157
|
|
|
return static::$list[$value] ?? null; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Returns the list of constants (by name) for this type. |
162
|
|
|
* |
163
|
|
|
* @return array The list of constants by name |
164
|
|
|
*/ |
165
|
|
|
public static function getConstantsByName() |
166
|
|
|
{ |
167
|
|
|
$class = get_called_class(); |
168
|
|
|
|
169
|
|
|
if (!array_key_exists($class, static::$byName)) { |
170
|
|
|
$reflection = new ReflectionClass($class); |
171
|
|
|
static::$byName[$class] = $reflection->getConstants(); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return static::$byName[$class]; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the list of constants (by value) for this type. |
179
|
|
|
* |
180
|
|
|
* @return array The list of constants by value |
181
|
|
|
*/ |
182
|
|
|
public static function getConstantsByValue() |
183
|
|
|
{ |
184
|
|
|
$class = get_called_class(); |
185
|
|
|
|
186
|
|
|
if (!isset(static::$byValue[$class])) { |
187
|
|
|
static::$byValue[$class] = array_flip(static::getConstantsByName()); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return static::$byValue[$class]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Returns the name of the value. |
195
|
|
|
* |
196
|
|
|
* @return array|string The name, or names, of the value |
197
|
|
|
*/ |
198
|
|
|
public function getName() |
199
|
|
|
{ |
200
|
|
|
$constants = static::getConstantsByValue(); |
201
|
|
|
|
202
|
|
|
return $constants[$this->value]; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Unwraps the type and returns the raw value. |
207
|
|
|
* |
208
|
|
|
* @return mixed The raw value managed by the type instance |
209
|
|
|
*/ |
210
|
|
|
public function getValue() |
211
|
|
|
{ |
212
|
|
|
return $this->value; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Checks if a name is valid for this type. |
217
|
|
|
* |
218
|
|
|
* @param string $name The name of the value |
219
|
|
|
* |
220
|
|
|
* @return bool If the name is valid for this type, `true` is returned. |
221
|
|
|
* Otherwise, the name is not valid and `false` is returned |
222
|
|
|
*/ |
223
|
|
|
public static function isValidName($name): bool |
224
|
|
|
{ |
225
|
|
|
return array_key_exists($name, static::getConstantsByName()); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Checks if a value is valid for this type. |
230
|
|
|
* |
231
|
|
|
* @param string $value The value |
232
|
|
|
* |
233
|
|
|
* @return bool If the value is valid for this type, `true` is returned. |
234
|
|
|
* Otherwise, the value is not valid and `false` is returned |
235
|
|
|
*/ |
236
|
|
|
public static function isValidValue($value): bool |
237
|
|
|
{ |
238
|
|
|
return array_key_exists($value, static::getConstantsByValue()); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|