|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Paillechat\Enum; |
|
4
|
|
|
|
|
5
|
|
|
use Paillechat\Enum\Exception\EnumException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class Enum |
|
8
|
|
|
{ |
|
9
|
|
|
/** @var Enum[] */ |
|
10
|
|
|
private static $instances = []; |
|
11
|
|
|
/** @var \ReflectionClassConstant[] */ |
|
12
|
|
|
private static $constReflections = []; |
|
13
|
|
|
/** @var \ReflectionClass[] */ |
|
14
|
|
|
private static $reflections = []; |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $name; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $name |
|
20
|
|
|
*/ |
|
21
|
3 |
|
final private function __construct(string $name) |
|
22
|
|
|
{ |
|
23
|
3 |
|
$this->name = $name; |
|
24
|
3 |
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Creates enum instance with short static constructor |
|
28
|
|
|
* |
|
29
|
|
|
* @param string $name |
|
30
|
|
|
* @param array $arguments |
|
31
|
|
|
* |
|
32
|
|
|
* @return static |
|
33
|
|
|
* |
|
34
|
|
|
* @throws EnumException |
|
35
|
|
|
*/ |
|
36
|
6 |
|
final public static function __callStatic(string $name, array $arguments) |
|
37
|
|
|
{ |
|
38
|
6 |
|
$const = static::getConstList(); |
|
39
|
|
|
|
|
40
|
6 |
|
if (!\in_array($name, $const, true)) { |
|
41
|
1 |
|
throw EnumException::becauseUnknownMember(static::class, $name); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
return static::createNamedInstance($name); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
7 |
|
public static function getConstList(): array |
|
48
|
|
|
{ |
|
49
|
7 |
|
return array_keys(self::getEnumReflection(static::class)->getConstants()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
5 |
View Code Duplication |
private static function getConstantReflection(string $class, string $name): \ReflectionClassConstant |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
5 |
|
$key = self::getConstKey($class, $name); |
|
55
|
5 |
|
if (!array_key_exists($key, self::$constReflections)) { |
|
56
|
4 |
|
$refl = self::getEnumReflection(static::class); |
|
57
|
|
|
|
|
58
|
4 |
|
self::$constReflections[$key] = $refl->getReflectionConstant($name); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
5 |
|
return self::$constReflections[$key]; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
5 |
|
private static function getConstKey(string $class, string $name): string |
|
65
|
|
|
{ |
|
66
|
5 |
|
return $class . '::' . $name; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
5 |
|
private static function findParentClassForConst(string $name): string |
|
70
|
|
|
{ |
|
71
|
5 |
|
return self::getConstantReflection(static::class, $name)->getDeclaringClass()->getName(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
7 |
|
private static function getEnumReflection(string $class): \ReflectionClass |
|
75
|
|
|
{ |
|
76
|
7 |
|
if (!array_key_exists($class, self::$reflections)) { |
|
77
|
3 |
|
self::$reflections[$class] = new \ReflectionClass($class); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
7 |
|
return self::$reflections[$class]; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Create named enum instance |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $name |
|
87
|
|
|
* |
|
88
|
|
|
* @return static |
|
89
|
|
|
*/ |
|
90
|
5 |
View Code Duplication |
private static function createNamedInstance(string $name) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
5 |
|
$class = self::findParentClassForConst($name); |
|
93
|
|
|
|
|
94
|
5 |
|
$key = self::getConstKey($class, $name); |
|
95
|
|
|
|
|
96
|
5 |
|
if (!array_key_exists($key, self::$instances)) { |
|
97
|
3 |
|
self::$instances[$key] = new static($name); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
5 |
|
return self::$instances[$key]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
final public function getName(): string |
|
104
|
|
|
{ |
|
105
|
1 |
|
return $this->name; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
1 |
|
final public function __toString(): string |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->getName(); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: