|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace Thunder\Platenum\Enum; |
|
4
|
|
|
|
|
5
|
|
|
use Thunder\Platenum\Exception\PlatenumException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @author Tomasz Kowalczyk <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
trait EnumTrait |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var string */ |
|
13
|
|
|
private $member; |
|
14
|
|
|
/** @var int|string */ |
|
15
|
|
|
private $value; |
|
16
|
|
|
|
|
17
|
|
|
/** @var non-empty-array<string,non-empty-array<string,int|string>> */ |
|
|
|
|
|
|
18
|
|
|
protected static $members = []; |
|
19
|
|
|
/** @var array<string,array<string,static>> */ |
|
20
|
|
|
protected static $instances = []; |
|
21
|
|
|
|
|
22
|
|
|
/** @param int|string $value */ |
|
23
|
26 |
|
/* final */ private function __construct(string $member, $value) |
|
24
|
|
|
{ |
|
25
|
26 |
|
$this->member = $member; |
|
26
|
26 |
|
$this->value = $value; |
|
27
|
26 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/* --- CREATE --- */ |
|
30
|
|
|
|
|
31
|
38 |
|
final public static function __callStatic(string $member, array $arguments) |
|
32
|
|
|
{ |
|
33
|
38 |
|
$class = static::class; |
|
34
|
38 |
|
if($arguments) { |
|
|
|
|
|
|
35
|
1 |
|
throw PlatenumException::fromConstantArguments($class); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
37 |
|
return static::fromMember($member); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
45 |
|
final public static function fromMember(string $member): self |
|
42
|
|
|
{ |
|
43
|
45 |
|
$class = static::class; |
|
44
|
45 |
|
if(isset(static::$instances[$class][$member])) { |
|
45
|
9 |
|
return static::$instances[$class][$member]; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
44 |
|
static::resolveMembers(); |
|
49
|
36 |
|
if(false === array_key_exists($member, static::$members[$class])) { |
|
50
|
10 |
|
static::throwInvalidMemberException($member); |
|
51
|
6 |
|
static::throwDefaultInvalidMemberException($member); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** @psalm-suppress UnsafeInstantiation */ |
|
55
|
26 |
|
return static::$instances[$class][$member] = new static($member, static::$members[$class][$member]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param mixed $value |
|
60
|
|
|
* @return static |
|
61
|
|
|
*/ |
|
62
|
14 |
|
final public static function fromValue($value): self |
|
63
|
|
|
{ |
|
64
|
14 |
|
$class = static::class; |
|
65
|
14 |
|
if(false === is_scalar($value)) { |
|
66
|
1 |
|
throw PlatenumException::fromIllegalValue($class, $value); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
13 |
|
static::resolveMembers(); |
|
70
|
11 |
|
$member = array_search($value, static::$members[$class], true); |
|
71
|
11 |
|
if(false === $member) { |
|
72
|
5 |
|
static::throwInvalidValueException($value); |
|
73
|
3 |
|
static::throwDefaultInvalidValueException($value); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** @var string $member */ |
|
77
|
6 |
|
return static::fromMember($member); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param static $enum |
|
82
|
|
|
* @return static |
|
83
|
|
|
*/ |
|
84
|
4 |
|
final public static function fromEnum($enum): self |
|
85
|
|
|
{ |
|
86
|
4 |
|
if(false === $enum instanceof static) { |
|
87
|
2 |
|
throw PlatenumException::fromMismatchedClass(static::class, \get_class($enum)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return static::fromValue($enum->value); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param static $enum |
|
95
|
|
|
* @param-out AbstractConstantsEnum|AbstractDocblockEnum|AbstractStaticEnum|AbstractCallbackEnum|AbstractAttributeEnum $enum |
|
96
|
|
|
*/ |
|
97
|
2 |
|
final public function fromInstance(&$enum): void |
|
98
|
|
|
{ |
|
99
|
2 |
|
$enum = static::fromEnum($enum); |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @psalm-suppress UnusedForeachValue |
|
104
|
3 |
|
* @return list<static> |
|
|
|
|
|
|
105
|
|
|
*/ |
|
106
|
3 |
|
final public static function getInstances(): array |
|
107
|
|
|
{ |
|
108
|
9 |
|
static::resolveMembers(); |
|
109
|
|
|
foreach(static::$members[static::class] as $member => $value) { |
|
110
|
9 |
|
static::fromMember($member); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return array_values(static::$instances[static::class]); |
|
|
|
|
|
|
114
|
2 |
|
} |
|
115
|
|
|
|
|
116
|
2 |
|
/* --- EXCEPTIONS --- */ |
|
117
|
|
|
|
|
118
|
|
|
/** @psalm-suppress UnusedParam */ |
|
119
|
6 |
|
private static function throwInvalidMemberException(string $member): void |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
6 |
|
} |
|
122
|
|
|
|
|
123
|
|
|
private static function throwDefaultInvalidMemberException(string $member): void |
|
124
|
|
|
{ |
|
125
|
|
|
throw PlatenumException::fromInvalidMember(static::class, $member, static::$members[static::class]); |
|
126
|
|
|
} |
|
127
|
3 |
|
|
|
128
|
|
|
/** |
|
129
|
3 |
|
* @param mixed $value |
|
130
|
|
|
* @psalm-suppress UnusedParam |
|
131
|
|
|
*/ |
|
132
|
|
|
private static function throwInvalidValueException($value): void |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
6 |
|
} |
|
135
|
|
|
|
|
136
|
6 |
|
/** @param mixed $value */ |
|
137
|
|
|
private static function throwDefaultInvalidValueException($value): void |
|
138
|
|
|
{ |
|
139
|
|
|
throw PlatenumException::fromInvalidValue(static::class, $value); |
|
140
|
9 |
|
} |
|
141
|
|
|
|
|
142
|
9 |
|
/* --- COMPARE --- */ |
|
143
|
|
|
|
|
144
|
|
|
/** @param static $other */ |
|
145
|
1 |
|
final public function equals($other): bool |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $other instanceof $this && $this->value === $other->value; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
1 |
|
/* --- TRANSFORM --- */ |
|
151
|
|
|
|
|
152
|
1 |
|
final public function getMember(): string |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->member; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
8 |
|
/** @return int|string */ |
|
158
|
|
|
final public function getValue() |
|
159
|
8 |
|
{ |
|
160
|
|
|
return $this->value; |
|
161
|
7 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
#[\ReturnTypeWillChange] |
|
164
|
|
|
final public function jsonSerialize() |
|
165
|
8 |
|
{ |
|
166
|
|
|
return $this->getValue(); |
|
167
|
8 |
|
} |
|
168
|
|
|
|
|
169
|
8 |
|
final public function __toString(): string |
|
170
|
|
|
{ |
|
171
|
|
|
return (string)$this->getValue(); |
|
172
|
1 |
|
} |
|
173
|
|
|
|
|
174
|
1 |
|
/* --- CHECK --- */ |
|
175
|
|
|
|
|
176
|
|
|
final public static function memberExists(string $member): bool |
|
177
|
|
|
{ |
|
178
|
1 |
|
static::resolveMembers(); |
|
179
|
|
|
|
|
180
|
1 |
|
return array_key_exists($member, static::$members[static::class]); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** @param int|string $value */ |
|
184
|
|
|
final public static function valueExists($value): bool |
|
185
|
|
|
{ |
|
186
|
6 |
|
static::resolveMembers(); |
|
187
|
|
|
|
|
188
|
6 |
|
return \in_array($value, static::$members[static::class], true); |
|
189
|
5 |
|
} |
|
190
|
3 |
|
|
|
191
|
|
|
/** @param list<string> $members */ |
|
192
|
|
|
final public static function membersExist(array $members): bool |
|
193
|
1 |
|
{ |
|
194
|
|
|
static::resolveMembers(); |
|
195
|
|
|
|
|
196
|
|
|
return [] !== array_intersect(array_keys(static::$members[static::class]), $members); |
|
197
|
7 |
|
} |
|
198
|
|
|
|
|
199
|
7 |
|
/** @param list<int|string> $values */ |
|
200
|
5 |
|
final public static function valuesExist(array $values): bool |
|
201
|
3 |
|
{ |
|
202
|
|
|
static::resolveMembers(); |
|
203
|
|
|
|
|
204
|
2 |
|
return [] !== array_intersect(static::$members[static::class], $values); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
final public function hasMember(string $members): bool |
|
208
|
|
|
{ |
|
209
|
1 |
|
return $members === $this->member; |
|
210
|
|
|
} |
|
211
|
1 |
|
|
|
212
|
|
|
/** @param int|string $value */ |
|
213
|
|
|
final public function hasValue($value): bool |
|
214
|
1 |
|
{ |
|
215
|
|
|
return $value === $this->value; |
|
216
|
1 |
|
} |
|
217
|
|
|
|
|
218
|
1 |
|
/** @param list<string> $members */ |
|
219
|
|
|
final public function hasMemberIn(array $members): bool |
|
220
|
|
|
{ |
|
221
|
5 |
|
return in_array($this->member, $members, true); |
|
222
|
|
|
} |
|
223
|
5 |
|
|
|
224
|
|
|
/** @param list<int|string> $values */ |
|
225
|
5 |
|
final public function hasValueIn(array $values): bool |
|
226
|
|
|
{ |
|
227
|
|
|
return in_array($this->value, $values, true); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
74 |
|
/** @param list<static> $enums */ |
|
231
|
|
|
final public function isIn(array $enums): bool |
|
232
|
74 |
|
{ |
|
233
|
74 |
|
return in_array($this, $enums, true); |
|
234
|
15 |
|
} |
|
235
|
|
|
|
|
236
|
|
|
final public static function isMemberWarm(string $member): bool |
|
237
|
73 |
|
{ |
|
238
|
1 |
|
return self::memberExists($member) |
|
239
|
73 |
|
&& array_key_exists(static::class, static::$instances) |
|
240
|
|
|
&& array_key_exists($member, static::$instances[static::class]); |
|
241
|
|
|
} |
|
242
|
73 |
|
|
|
243
|
|
|
/* --- INFO --- */ |
|
244
|
73 |
|
|
|
245
|
66 |
|
/** @return int|string */ |
|
246
|
1 |
|
final public static function memberToValue(string $member) |
|
247
|
|
|
{ |
|
248
|
65 |
|
if(false === static::memberExists($member)) { |
|
249
|
1 |
|
static::throwInvalidMemberException($member); |
|
250
|
|
|
static::throwDefaultInvalidMemberException($member); |
|
251
|
|
|
} |
|
252
|
64 |
|
|
|
253
|
1 |
|
return static::$members[static::class][$member]; |
|
254
|
|
|
} |
|
255
|
63 |
|
|
|
256
|
1 |
|
/** @param int|string $value */ |
|
257
|
|
|
final public static function valueToMember($value): string |
|
258
|
|
|
{ |
|
259
|
62 |
|
if(false === static::valueExists($value)) { |
|
260
|
62 |
|
static::throwInvalidValueException($value); |
|
261
|
|
|
static::throwDefaultInvalidValueException($value); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
1 |
|
return (string)array_search($value, static::$members[static::class], true); |
|
265
|
|
|
} |
|
266
|
1 |
|
|
|
267
|
|
|
final public static function getMembers(): array |
|
268
|
|
|
{ |
|
269
|
1 |
|
static::resolveMembers(); |
|
270
|
|
|
|
|
271
|
1 |
|
return array_keys(static::$members[static::class]); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
1 |
|
final public static function getValues(): array |
|
275
|
|
|
{ |
|
276
|
1 |
|
static::resolveMembers(); |
|
277
|
|
|
|
|
278
|
|
|
return array_values(static::$members[static::class]); |
|
279
|
|
|
} |
|
280
|
1 |
|
|
|
281
|
|
|
final public static function getMembersAndValues(): array |
|
282
|
1 |
|
{ |
|
283
|
|
|
static::resolveMembers(); |
|
284
|
|
|
|
|
285
|
|
|
return static::$members[static::class]; |
|
286
|
1 |
|
} |
|
287
|
|
|
|
|
288
|
1 |
|
/* --- SOURCE --- */ |
|
289
|
|
|
|
|
290
|
|
|
private static function resolveMembers(): void |
|
291
|
|
|
{ |
|
292
|
1 |
|
$class = static::class; |
|
293
|
|
|
if(isset(static::$members[$class])) { |
|
294
|
1 |
|
return; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
1 |
|
$throwMissingResolve = function(string $class): void { |
|
298
|
|
|
throw PlatenumException::fromMissingResolve($class); |
|
299
|
1 |
|
}; |
|
300
|
|
|
// reflection instead of method_exists because of PHP 7.4 bug #78632 |
|
301
|
|
|
// @see https://bugs.php.net/bug.php?id=78632 |
|
302
|
|
|
$hasResolve = (new \ReflectionClass($class))->hasMethod('resolve'); |
|
303
|
|
|
/** @var array<string,int|string> $members */ |
|
304
|
|
|
$members = $hasResolve ? static::resolve() : $throwMissingResolve($class); |
|
|
|
|
|
|
305
|
|
|
if(empty($members)) { |
|
306
|
|
|
throw PlatenumException::fromEmptyMembers($class); |
|
307
|
|
|
} |
|
308
|
|
|
if(\count($members) !== \count(\array_unique($members))) { |
|
309
|
|
|
throw PlatenumException::fromNonUniqueMembers($class); |
|
310
|
|
|
} |
|
311
|
|
|
if(['string'] !== \array_unique(\array_map('gettype', array_keys($members)))) { |
|
312
|
|
|
throw PlatenumException::fromNonStringMembers($class); |
|
313
|
|
|
} |
|
314
|
|
|
if(1 !== \count(\array_unique(\array_map('gettype', $members)))) { |
|
315
|
|
|
throw PlatenumException::fromNonUniformMemberValues($class, $members); |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
static::$members[$class] = $members; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/* --- MAGIC --- */ |
|
322
|
|
|
|
|
323
|
|
|
final public function __clone() |
|
324
|
|
|
{ |
|
325
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
final public function __call(string $name, array $arguments) |
|
329
|
|
|
{ |
|
330
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
final public function __invoke() |
|
334
|
|
|
{ |
|
335
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** @param mixed $name */ |
|
339
|
|
|
final public function __isset($name) |
|
340
|
|
|
{ |
|
341
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
342
|
|
|
} |
|
343
|
|
|
|
|
344
|
|
|
/** @param mixed $name */ |
|
345
|
|
|
final public function __unset($name) |
|
346
|
|
|
{ |
|
347
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
348
|
|
|
} |
|
349
|
|
|
|
|
350
|
|
|
/** @param mixed $value */ |
|
351
|
|
|
final public function __set(string $name, $value) |
|
352
|
|
|
{ |
|
353
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
final public function __get(string $name) |
|
357
|
|
|
{ |
|
358
|
|
|
throw PlatenumException::fromMagicMethod(static::class, __FUNCTION__); |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|