1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Giritli\Enum; |
4
|
|
|
|
5
|
|
|
use Giritli\Enum\Exception\NoDefaultValueException; |
6
|
|
|
use Giritli\Enum\Exception\ValueNotFoundException; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
use JsonSerializable; |
9
|
|
|
|
10
|
|
|
abstract class Enum implements JsonSerializable |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The value of the enum. Must be compatible with the enum constant values. |
16
|
|
|
* |
17
|
|
|
* @var mixed |
18
|
|
|
*/ |
19
|
|
|
protected $value = null; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Name of the enum constant. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $key = null; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The ordinal of the enum value. |
32
|
|
|
* |
33
|
|
|
* @var integer |
34
|
|
|
*/ |
35
|
|
|
protected $ordinal = -1; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The default value of the enum. |
40
|
|
|
* |
41
|
|
|
* @var mixed |
42
|
|
|
*/ |
43
|
|
|
protected $default = null; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Can the enum be null? |
48
|
|
|
*/ |
49
|
|
|
protected $nullable = false; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Static cache of all loaded enum values. |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
protected static $cache = []; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Static cache of all the ordinals of enum values. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected static $ordinals = []; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Instantiate a new enum value. |
70
|
|
|
* |
71
|
|
|
* @param mixed $value |
72
|
|
|
* @throws NoDefaultValueException |
73
|
|
|
*/ |
74
|
11 |
|
public function __construct($value = null) |
75
|
|
|
{ |
76
|
11 |
|
if (is_null($value)) |
77
|
11 |
|
{ |
78
|
6 |
|
if ($this->nullable) |
79
|
6 |
|
{ |
80
|
|
|
static::generateCache(); |
81
|
|
|
|
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
6 |
|
if ($this->default) |
86
|
6 |
|
{ |
87
|
5 |
|
return $this->__construct($this->default); |
|
|
|
|
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
1 |
|
throw new NoDefaultValueException('No enum default exists.'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
10 |
|
static::generateCache(); |
96
|
|
|
|
97
|
10 |
|
if ($value instanceof Enum) |
98
|
10 |
|
{ |
99
|
1 |
|
$this->loadValue($value->getValue()); |
100
|
1 |
|
} |
101
|
|
|
else |
102
|
|
|
{ |
103
|
10 |
|
$this->loadValue($value); |
104
|
|
|
} |
105
|
8 |
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Generate cache for current class and also cache the |
110
|
|
|
* ordinal value. |
111
|
|
|
*/ |
112
|
13 |
|
protected static function generateCache() |
113
|
|
|
{ |
114
|
13 |
|
$class = static::class; |
115
|
|
|
|
116
|
13 |
|
if (!isset(static::$cache[$class])) |
117
|
13 |
|
{ |
118
|
2 |
|
$reflection = new ReflectionClass($class); |
119
|
2 |
|
static::$cache[$class] = $reflection->getConstants(); |
120
|
2 |
|
$i = 0; |
121
|
2 |
|
foreach (static::$cache[$class] as $constant => $value) |
122
|
|
|
{ |
123
|
2 |
|
static::$ordinals[$class][$constant] = $i++; |
124
|
2 |
|
} |
125
|
2 |
|
} |
126
|
|
|
|
127
|
13 |
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Load the value of the enum. |
132
|
|
|
* |
133
|
|
|
* @param mixed $value |
134
|
|
|
* @throws ValueNotFoundException |
135
|
|
|
*/ |
136
|
10 |
|
protected function loadValue($value) |
137
|
|
|
{ |
138
|
10 |
|
$class = static::class; |
139
|
10 |
|
$key = array_search($value, static::$cache[$class], true); |
140
|
|
|
|
141
|
10 |
|
if ($key === false) |
142
|
10 |
|
{ |
143
|
2 |
|
throw new ValueNotFoundException('Enum value not found.'); |
144
|
|
|
} |
145
|
|
|
|
146
|
8 |
|
$this->key = $key; |
147
|
8 |
|
$this->value = $value; |
148
|
8 |
|
$this->ordinal = static::$ordinals[$class][$key]; |
149
|
8 |
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Allow instantiation of enum by calling Enum::key() and |
154
|
|
|
* returning a new instance of an enum. |
155
|
|
|
* |
156
|
|
|
* @param string $name |
157
|
|
|
* @param array $arguments |
158
|
|
|
* @return Enum |
159
|
|
|
*/ |
160
|
2 |
|
public static function __callStatic($name, $arguments) |
161
|
|
|
{ |
162
|
2 |
|
static::generateCache(); |
163
|
2 |
|
$class = static::class; |
164
|
|
|
|
165
|
2 |
|
if (isset(static::$cache[$class][$name])) |
166
|
2 |
|
{ |
167
|
1 |
|
return new static(static::$cache[$class][$name]); |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return new static($name); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get all enum values. |
176
|
|
|
* |
177
|
|
|
* @return mixed[] |
178
|
|
|
*/ |
179
|
1 |
|
public static function getValues() |
180
|
|
|
{ |
181
|
1 |
|
static::generateCache(); |
182
|
|
|
|
183
|
1 |
|
return static::$cache[static::class]; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Get all enum value ordinals. |
189
|
|
|
* |
190
|
|
|
* @return int[] |
191
|
|
|
*/ |
192
|
1 |
|
public static function getOrdinals() |
193
|
|
|
{ |
194
|
1 |
|
static::generateCache(); |
195
|
|
|
|
196
|
1 |
|
return static::$ordinals[static::class]; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Get all enum keys. |
202
|
|
|
* |
203
|
|
|
* @return array |
204
|
|
|
*/ |
205
|
1 |
|
public static function getKeys() |
206
|
|
|
{ |
207
|
1 |
|
static::generateCache(); |
208
|
|
|
|
209
|
1 |
|
return array_keys(static::$ordinals[static::class]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Get the enum value. |
215
|
|
|
* |
216
|
|
|
* @return mixed |
217
|
|
|
*/ |
218
|
6 |
|
public function getValue() |
219
|
|
|
{ |
220
|
6 |
|
return $this->value; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Get the enum value's key. |
226
|
|
|
* |
227
|
|
|
* @return string; |
|
|
|
|
228
|
|
|
*/ |
229
|
1 |
|
public function getKey() |
230
|
|
|
{ |
231
|
1 |
|
return $this->key; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Get the current ordinal value of the enum. |
237
|
|
|
* |
238
|
|
|
* @return integer |
239
|
|
|
*/ |
240
|
1 |
|
public function getOrdinal() |
241
|
|
|
{ |
242
|
1 |
|
return $this->ordinal; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Compare current value to given value. |
248
|
|
|
* |
249
|
|
|
* @returns boolean; |
250
|
|
|
*/ |
251
|
1 |
|
public function is($value) |
252
|
|
|
{ |
253
|
1 |
|
return (string) $this->getValue() === (string) $value; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Return string representation of enum value. |
259
|
|
|
* |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
3 |
|
public function __toString() |
263
|
|
|
{ |
264
|
3 |
|
return (string) $this->getValue(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Return string value for json representation. |
270
|
|
|
* |
271
|
|
|
* @return string |
272
|
|
|
*/ |
273
|
|
|
public function jsonSerialize() |
274
|
|
|
{ |
275
|
|
|
return $this->__toString(); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
|