-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflectionEnumUnitCase.php
73 lines (62 loc) · 2.07 KB
/
ReflectionEnumUnitCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/**
* Part of SplTypes package.
*
* (c) Adrien Loyant <donald_duck@team-df.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Ducks\Polyfill\Enum;
class ReflectionEnumUnitCase extends \ReflectionClassConstant
{
private static array $instances = [];
/**
* Instantiates a ReflectionEnumUnitCase object
*
* @param object|string $class An enum instance or a name.
* @param string $constant An enum constant name.
*
* @throws ReflectionException if $class is not a \ReflectionEnumUnitCase
*
* @link https://www.php.net/manual/en/reflectionenumunitcase.construct.php
*/
public function __construct($class, string $constant)
{
parent::__construct($class, $constant);
if (!$this->getEnum()->hasCase($constant)) {
throw new \ReflectionException(
'Enum case ' . $this->class . '::' . $this->name . ' is not a case'
);
}
}
/**
* Gets the reflection of the enum of this case
*
* @return ReflectionEnum instance describing the Enum this case belongs to.
*
* @link https://www.php.net/manual/en/reflectionenumunitcase.getenum.php
*/
public function getEnum(): ReflectionEnum
{
return new ReflectionEnum($this->class);
}
/**
* Gets the enum case object described by this reflection object
*
* @return UnitEnum The enum case object described by this reflection object.
*/
public function getValue(): UnitEnum
{
if (!isset(self::$instances[$this->class][$this->name])) {
$class = $this->getDeclaringClass();
$instance = $class->newInstanceWithoutConstructor();
$object = $class->getConstructor();
$object->setAccessible(true);
$object->invoke($instance, $this->name);
self::$instances[$this->class][$this->name] = $instance;
}
return self::$instances[$this->class][$this->name];
}
}