|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Part of SplTypes package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Adrien Loyant <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Ducks\Component\SplTypes; |
|
15
|
|
|
|
|
16
|
|
|
use Ducks\Component\SplTypes\Reflection\SplReflectionEnum; |
|
17
|
|
|
use Ducks\Component\SplTypes\Reflection\SplReflectionEnumUnitCase; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Simplify SplUnitEnum integration |
|
21
|
|
|
* |
|
22
|
|
|
* @property-read string $name |
|
23
|
|
|
* |
|
24
|
|
|
* @phpstan-require-implements SplUnitEnum |
|
25
|
|
|
*/ |
|
26
|
|
|
trait SplUnitEnumTrait |
|
27
|
|
|
{ |
|
28
|
|
|
use SplEnumSingletonTrait; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* case-sensitive name of the case itself. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
protected string $name; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Generates a list of cases on an enum |
|
39
|
|
|
* |
|
40
|
|
|
* @return static[] An array of all defined cases of this enumeration, in order of declaration. |
|
41
|
|
|
* |
|
42
|
|
|
* @psalm-suppress MixedInferredReturnType |
|
43
|
|
|
* |
|
44
|
|
|
* @see SplUnitEnum::cases() |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function cases(): array |
|
47
|
|
|
{ |
|
48
|
|
|
static $cases = null; |
|
49
|
|
|
|
|
50
|
|
|
if (null === $cases) { |
|
51
|
|
|
$enum = new SplReflectionEnum(static::class); |
|
52
|
|
|
foreach ($enum->getCases() as $case) { |
|
53
|
|
|
$cases[] = $case->getValue(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $cases ?? []; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return a new instance of enum |
|
62
|
|
|
* |
|
63
|
|
|
* @param string $name |
|
64
|
|
|
* @param mixed[] $arguments |
|
65
|
|
|
* |
|
66
|
|
|
* @return static self keywords not an equivalent |
|
67
|
|
|
* |
|
68
|
|
|
* @throws \Error if $name is not a valid constant enum |
|
69
|
|
|
* |
|
70
|
|
|
* @phpstan-param string $name |
|
71
|
|
|
* @phpstan-param list<mixed> $arguments |
|
72
|
|
|
* @phpstan-return static |
|
73
|
|
|
* @phpstan-ignore-next-line |
|
74
|
|
|
* |
|
75
|
|
|
* @psalm-suppress UnsafeInstantiation |
|
76
|
|
|
*/ |
|
77
|
|
|
#[\ReturnTypeWillChange] |
|
78
|
|
|
public static function __callStatic(string $name, array $arguments) |
|
79
|
|
|
{ |
|
80
|
|
|
try { |
|
81
|
|
|
$unit = new SplReflectionEnumUnitCase(static::class, $name); |
|
82
|
|
|
$object = $unit->getValue(); |
|
83
|
|
|
} catch (\ReflectionException $th) { |
|
84
|
|
|
throw new \Error('Undefined constant ' . static::class . '::' . $name); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** @var static $object */ |
|
88
|
|
|
return $object; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|