|
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\Reflection; |
|
15
|
|
|
|
|
16
|
|
|
use Ducks\Component\SplTypes\SplUnitEnum; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The SplReflectionEnumBackedCase class reports info about an SplEnum backed case, which has no scalar equivalent. |
|
20
|
|
|
* |
|
21
|
|
|
* @psalm-api |
|
22
|
|
|
* @psalm-immutable |
|
23
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
|
24
|
|
|
*/ |
|
25
|
|
|
class SplReflectionEnumBackedCase extends SplReflectionEnumUnitCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* Instantiates a ReflectionEnumBackedCase object |
|
29
|
|
|
* |
|
30
|
|
|
* @param object|string $class An enum instance or a name. |
|
31
|
|
|
* @param string $constant An enum constant name. |
|
32
|
|
|
* |
|
33
|
|
|
* @throws \ReflectionException if $class is not a \ReflectionEnumBackedCase |
|
34
|
|
|
* @throws \ReflectionException if $class is not a \Ducks\Component\SplTypes\SplEnumerable interface. |
|
35
|
|
|
* @throws \ReflectionException in case the given class constant case does not exist. |
|
36
|
|
|
* @throws \ReflectionException if $constant is not a case. |
|
37
|
|
|
* |
|
38
|
|
|
* @phpcs:ignore Generic.Files.LineLength.TooLong |
|
39
|
|
|
* @phpstan-param \Ducks\Component\SplTypes\SplEnumerable|class-string<\Ducks\Component\SplTypes\SplEnumerable> $class An enum instance or a name. |
|
40
|
|
|
* @phpstan-param string $constant An enum constant name. |
|
41
|
|
|
* |
|
42
|
|
|
* @psalm-suppress UninitializedProperty |
|
43
|
|
|
* @psalm-suppress ImpureMethodCall |
|
44
|
|
|
* |
|
45
|
|
|
* @link https://www.php.net/manual/en/reflectionenumbackedcase.construct.php |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct($class, string $constant) |
|
48
|
|
|
{ |
|
49
|
|
|
parent::__construct($class, $constant); |
|
50
|
|
|
|
|
51
|
|
|
if (!$this->getEnum()->isBacked()) { |
|
52
|
|
|
/** @psalm-suppress PossiblyNullOperand */ |
|
53
|
|
|
throw new \ReflectionException( |
|
54
|
|
|
'Enum case ' . $this->class . '::' . $this->name . ' is not a backed case' |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Gets the scalar value backing this Enum case |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed The scalar equivalent of this enum case. |
|
63
|
|
|
* |
|
64
|
|
|
* @link https://www.php.net/manual/en/reflectionenumbackedcase.getbackingvalue.php |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getBackingValue() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getDeclaringClass() |
|
69
|
|
|
->getConstant($this->name); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
* |
|
75
|
|
|
* @return SplUnitEnum The enum case object described by this reflection object. |
|
76
|
|
|
* |
|
77
|
|
|
* @psalm-suppress ImpureMethodCall |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getValue(): SplUnitEnum |
|
80
|
|
|
{ |
|
81
|
|
|
$instance = parent::getValue(); |
|
82
|
|
|
|
|
83
|
|
|
$class = new \ReflectionObject($instance); |
|
84
|
|
|
if ($class->hasProperty('value')) { |
|
85
|
|
|
$property = $class->getProperty('value'); |
|
86
|
|
|
$property->setAccessible(true); |
|
87
|
|
|
$property->setValue($instance, $class->getConstant($this->name)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $instance; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|