-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractEnum.php
273 lines (230 loc) · 5.93 KB
/
AbstractEnum.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/*
* enum (https://github.com/phpgears/enum).
* Enum object for PHP.
*
* @license MIT
* @link https://github.com/phpgears/enum
* @author Julián Gutiérrez <juliangut@gmail.com>
*/
declare(strict_types=1);
namespace Gears\Enum;
use Gears\Enum\Exception\EnumException;
use Gears\Enum\Exception\InvalidEnumNameException;
use Gears\Enum\Exception\InvalidEnumValueException;
use Gears\Immutability\ImmutabilityBehaviour;
/**
* Base immutable enum class.
*/
abstract class AbstractEnum implements Enum
{
use ImmutabilityBehaviour;
/**
* Class is final checked map.
*
* @var bool[]
*/
protected static $finalCheckMap = [];
/**
* Enum class constants map.
*
* @var array<string, mixed[]>
*/
protected static $enumCacheMap = [];
/**
* Enum value.
*
* @var mixed
*/
private $value;
/**
* AbstractEnum constructor.
*
* @param static|mixed $value
*
* @throws EnumException
* @throws InvalidEnumValueException
*/
final public function __construct($value)
{
$this->assertImmutable();
$this->assertFinal();
$this->checkValue($value);
$this->value = $value instanceof Enum ? $value->getValue() : $value;
}
/**
* Assert enum is final.
*
* @throws EnumException
*/
private function assertFinal(): void
{
$class = static::class;
if (isset(static::$finalCheckMap[$class])) {
return;
}
if (!(new \ReflectionClass(static::class))->isFinal()) {
throw new EnumException(\sprintf('Enum class "%s" should be final', static::class));
}
static::$finalCheckMap[$class] = true;
}
/**
* Value based static constructor.
*
* @param string $name
* @param mixed[] $params
*
* @return self
*/
final public static function __callStatic(string $name, array $params)
{
if (\count($params) !== 0) {
throw new EnumException('Enum static constructor must be called with no parameters');
}
$enumerator = static::normalizeName($name);
$enumerators = static::getEnumerators();
if (!\array_key_exists($enumerator, $enumerators)) {
throw new InvalidEnumNameException(\sprintf(
'"%s" is not a valid enumerator for enum "%s"',
$name,
static::class
));
}
return new static($enumerators[$enumerator]);
}
/**
* {@inheritdoc}
*/
final public function isEqualTo(Enum $enum): bool
{
return \get_class($enum) === static::class && $enum->getValue() === $this->value;
}
/**
* {@inheritdoc}
*/
final public function isAnyOf(array $enums): bool
{
foreach ($enums as $enum) {
if ($this->isEqualTo($enum)) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
final public function getValue()
{
return $this->value;
}
/**
* @return array<string, mixed>
*/
final public function __serialize(): array
{
return ['value' => $this->value];
}
/**
* @param array<string, mixed> $data
*
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
final public function __unserialize(array $data): void
{
$this->assertImmutable();
$this->assertFinal();
$value = $data['value'];
$this->checkValue($value);
$this->value = $value;
}
/**
* {@inheritdoc}
*/
final public function serialize(): string
{
return \serialize($this->value);
}
/**
* {@inheritdoc}
*
* @param mixed $serialized
*/
public function unserialize($serialized): void
{
$this->assertImmutable();
$this->assertFinal();
$value = \unserialize($serialized, ['allowed_classes' => false]);
$this->checkValue($value);
$this->value = $value;
}
final public function __clone()
{
}
/**
* Check enum value validity.
*
* @param static|mixed $value
*
* @throws InvalidEnumValueException
*/
private function checkValue($value): void
{
if ($value instanceof Enum) {
if (\get_class($value) !== static::class) {
throw new InvalidEnumValueException(\sprintf(
'Enum "%s" cannot be created from enum "%s"',
static::class,
\get_class($value)
));
}
return;
}
if (!\in_array($value, static::getEnumerators(), true)) {
throw new InvalidEnumValueException(\sprintf(
'"%s" is not a valid value for enum "%s"',
$value,
static::class
));
}
}
/**
* Get list of enumerators.
*
* @return array<string, mixed>
*/
private static function getEnumerators(): array
{
$class = static::class;
if (!isset(static::$enumCacheMap[$class])) {
$constants = [];
foreach ((new \ReflectionClass($class))->getReflectionConstants() as $reflectionConstant) {
if ($reflectionConstant->isPublic()) {
$constants[static::normalizeName($reflectionConstant->getName())] = $reflectionConstant->getValue();
}
}
static::$enumCacheMap[$class] = $constants;
}
return static::$enumCacheMap[$class];
}
/**
* Normalize name.
*
* @param string $name
*
* @return string
*/
private static function normalizeName(string $name): string
{
return \strtoupper($name);
}
/**
* {@inheritdoc}
*
* @return string[]
*/
final protected function getAllowedInterfaces(): array
{
return [Enum::class];
}
}