forked from consistence/consistence
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnum.php
185 lines (158 loc) · 4.16 KB
/
Enum.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
<?php
declare(strict_types = 1);
namespace Consistence\Enum;
use Consistence\Reflection\ClassReflection;
use Consistence\Type\ArrayType\ArrayType;
use Consistence\Type\ArrayType\KeyValuePair;
use Consistence\Type\Type;
use ReflectionClass;
use ReflectionClassConstant;
abstract class Enum extends \Consistence\ObjectPrototype
{
/** @var mixed */
private $value;
/** @var self[] indexed by enum and value */
private static $instances = [];
/** @var mixed[] format: enum name (string) => cached values (const name (string) => value (mixed)) */
private static $availableValues;
/**
* @param mixed $value
*/
final private function __construct($value)
{
static::checkValue($value);
$this->value = $value;
}
/**
* @param mixed $value
* @return static
*/
public static function get($value): self
{
$index = sprintf('%s::%s', get_called_class(), self::getValueIndex($value));
if (!isset(self::$instances[$index])) {
self::$instances[$index] = new static($value);
}
return self::$instances[$index];
}
/**
* @param mixed $value
* @return string
*/
private static function getValueIndex($value): string
{
try {
Type::checkType($value, 'string|int|float|bool|null');
} catch (\Consistence\InvalidArgumentTypeException $e) {
throw new \Consistence\Enum\InvalidEnumValueException($value, static::class, $e);
}
$type = Type::getType($value);
return $value . sprintf('[%s]', $type);
}
/**
* @return mixed[]
*/
public static function getAvailableValues(): iterable
{
$index = get_called_class();
if (!isset(self::$availableValues[$index])) {
$availableValues = self::getEnumConstants();
static::checkAvailableValues($availableValues);
self::$availableValues[$index] = $availableValues;
}
return self::$availableValues[$index];
}
/**
* @return static[]
*/
public static function getAvailableEnums(): iterable
{
$values = static::getAvailableValues();
return ArrayType::mapByCallback($values, function (KeyValuePair $pair) {
return new KeyValuePair($pair->getKey(), static::get($pair->getValue()));
});
}
/**
* @return mixed[] format: const name (string) => value (mixed)
*/
private static function getEnumConstants(): array
{
$classReflection = new ReflectionClass(get_called_class());
$declaredConstants = ClassReflection::getDeclaredConstants($classReflection);
$declaredPublicConstants = ArrayType::filterValuesByCallback(
$declaredConstants,
function (ReflectionClassConstant $constant): bool {
return $constant->isPublic();
}
);
return ArrayType::mapByCallback(
$declaredPublicConstants,
function (KeyValuePair $keyValuePair): KeyValuePair {
$constant = $keyValuePair->getValue();
assert($constant instanceof ReflectionClassConstant);
return new KeyValuePair(
$constant->getName(),
$constant->getValue()
);
}
);
}
/**
* @param mixed[] $availableValues
*/
protected static function checkAvailableValues(iterable $availableValues): void
{
$index = [];
foreach ($availableValues as $value) {
Type::checkType($value, 'int|string|float|bool|null');
$key = self::getValueIndex($value);
if (isset($index[$key])) {
throw new \Consistence\Enum\DuplicateValueSpecifiedException($value, static::class);
}
$index[$key] = true;
}
}
/**
* @param mixed $value
* @return bool
*/
public static function isValidValue($value): bool
{
return ArrayType::containsValue(static::getAvailableValues(), $value);
}
/**
* @param mixed $value
*/
public static function checkValue($value): void
{
if (!static::isValidValue($value)) {
throw new \Consistence\Enum\InvalidEnumValueException($value, static::class);
}
}
protected function checkSameEnum(self $that): void
{
if (get_class($this) !== get_class($that)) {
throw new \Consistence\Enum\OperationSupportedOnlyForSameEnumException($that, $this);
}
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
public function equals(self $that): bool
{
$this->checkSameEnum($that);
return $this === $that;
}
/**
* @param mixed $value
* @return bool
*/
public function equalsValue($value): bool
{
return $this->getValue() === $value;
}
}