1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MabeEnum; |
6
|
|
|
|
7
|
|
|
use ArrayAccess; |
8
|
|
|
use Countable; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use Iterator; |
11
|
|
|
use IteratorAggregate; |
12
|
|
|
use UnexpectedValueException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* A map of enumerators and data values (EnumMap<K extends Enum, V>). |
16
|
|
|
* |
17
|
|
|
* @copyright 2019 Marc Bennewitz |
18
|
|
|
* @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License |
19
|
|
|
* @link http://github.com/marc-mabe/php-enum for the canonical source repository |
20
|
|
|
*/ |
|
|
|
|
21
|
|
|
class EnumMap implements ArrayAccess, Countable, IteratorAggregate |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The classname of the enumeration type |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $enumeration; |
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Internal map of ordinal number and data value |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $map = []; |
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Constructor |
37
|
|
|
* @param string $enumeration The classname of the enumeration type |
|
|
|
|
38
|
|
|
* @param null|iterable $map Initialize map |
|
|
|
|
39
|
|
|
* @throws InvalidArgumentException |
|
|
|
|
40
|
|
|
*/ |
41
|
18 |
|
public function __construct(string $enumeration, iterable $map = null) |
|
|
|
|
42
|
|
|
{ |
43
|
18 |
|
if (!\is_subclass_of($enumeration, Enum::class)) { |
44
|
1 |
|
throw new InvalidArgumentException(\sprintf( |
45
|
1 |
|
'%s can handle subclasses of %s only', |
46
|
1 |
|
__CLASS__, |
47
|
1 |
|
Enum::class |
48
|
|
|
)); |
49
|
|
|
} |
50
|
17 |
|
$this->enumeration = $enumeration; |
51
|
|
|
|
52
|
17 |
|
if ($map) { |
|
|
|
|
53
|
1 |
|
$this->addIterable($map); |
54
|
|
|
} |
55
|
17 |
|
} |
56
|
|
|
|
57
|
|
|
/* write access (mutable) */ |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Adds the given enumerator (object or value) mapping to the specified data value. |
61
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
62
|
|
|
* @param mixed $value |
|
|
|
|
63
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
64
|
|
|
* @see offsetSet() |
65
|
|
|
*/ |
|
|
|
|
66
|
11 |
|
public function add($enumerator, $value): void |
67
|
|
|
{ |
68
|
11 |
|
$ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
69
|
10 |
|
$this->map[$ord] = $value; |
70
|
10 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Adds the given iterable, mapping enumerators (objects or values) to data values. |
74
|
|
|
* @param iterable $map |
|
|
|
|
75
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
76
|
|
|
*/ |
|
|
|
|
77
|
3 |
|
public function addIterable(iterable $map): void |
78
|
|
|
{ |
79
|
3 |
|
$innerMap = $this->map; |
80
|
3 |
|
foreach ($map as $enumerator => $value) { |
81
|
3 |
|
$ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
82
|
3 |
|
$innerMap[$ord] = $value; |
83
|
|
|
} |
84
|
3 |
|
$this->map = $innerMap; |
85
|
3 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Removes the given enumerator (object or value) mapping. |
89
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
90
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
91
|
|
|
* @see offsetUnset() |
92
|
|
|
*/ |
|
|
|
|
93
|
5 |
|
public function remove($enumerator): void |
94
|
|
|
{ |
95
|
5 |
|
$ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
96
|
5 |
|
unset($this->map[$ord]); |
97
|
5 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Removes the given iterable enumerator (object or value) mappings. |
101
|
|
|
* @param iterable $enumerators |
|
|
|
|
102
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
103
|
|
|
*/ |
|
|
|
|
104
|
2 |
|
public function removeIterable(iterable $enumerators): void |
105
|
|
|
{ |
106
|
2 |
|
$map = $this->map; |
107
|
2 |
|
foreach ($enumerators as $enumerator) { |
108
|
2 |
|
$ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
109
|
2 |
|
unset($map[$ord]); |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
$this->map = $map; |
113
|
2 |
|
} |
114
|
|
|
|
115
|
|
|
/* write access (immutable) */ |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Creates a new map with the given enumerator (object or value) mapping to the specified data value added. |
|
|
|
|
119
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
120
|
|
|
* @param mixed $value |
|
|
|
|
121
|
|
|
* @return static |
122
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
123
|
|
|
*/ |
124
|
1 |
|
public function with($enumerator, $value): self |
125
|
|
|
{ |
126
|
1 |
|
$clone = clone $this; |
127
|
1 |
|
$clone->add($enumerator, $value); |
128
|
1 |
|
return $clone; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Creates a new map with the given iterable mapping enumerators (objects or values) to data values added. |
|
|
|
|
133
|
|
|
* @param iterable $map |
|
|
|
|
134
|
|
|
* @return static |
135
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
136
|
|
|
*/ |
137
|
1 |
|
public function withIterable(iterable $map): self |
138
|
|
|
{ |
139
|
1 |
|
$clone = clone $this; |
140
|
1 |
|
$clone->addIterable($map); |
141
|
1 |
|
return $clone; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Create a new map with the given enumerator mapping removed. |
146
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
147
|
|
|
* @return static |
148
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
149
|
|
|
*/ |
150
|
1 |
|
public function without($enumerator): self |
151
|
|
|
{ |
152
|
1 |
|
$clone = clone $this; |
153
|
1 |
|
$clone->remove($enumerator); |
154
|
1 |
|
return $clone; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Creates a new map with the given iterable enumerator (object or value) mappings removed. |
159
|
|
|
* @param iterable $enumerators |
|
|
|
|
160
|
|
|
* @return static |
161
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
162
|
|
|
*/ |
163
|
1 |
|
public function withoutIterable(iterable $enumerators): self |
164
|
|
|
{ |
165
|
1 |
|
$clone = clone $this; |
166
|
1 |
|
$clone->removeIterable($enumerators); |
167
|
1 |
|
return $clone; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/* read access */ |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get the classname of the enumeration type. |
174
|
|
|
* @return string |
175
|
|
|
*/ |
176
|
1 |
|
public function getEnumeration(): string |
177
|
|
|
{ |
178
|
1 |
|
return $this->enumeration; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the mapped data value of the given enumerator (object or value). |
183
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
184
|
|
|
* @return mixed |
185
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
186
|
|
|
* @throws UnexpectedValueException If the given enumerator does not exist in this map |
|
|
|
|
187
|
|
|
* @see offsetGet() |
188
|
|
|
*/ |
189
|
10 |
|
public function get($enumerator) |
190
|
|
|
{ |
191
|
10 |
|
$enumerator = ($this->enumeration)::get($enumerator); |
192
|
10 |
|
$ord = $enumerator->getOrdinal(); |
193
|
10 |
|
if (!\array_key_exists($ord, $this->map)) { |
194
|
2 |
|
throw new UnexpectedValueException(sprintf( |
195
|
2 |
|
'Enumerator %s could not be found', |
196
|
2 |
|
\var_export($enumerator->getValue(), true) |
197
|
|
|
)); |
198
|
|
|
} |
199
|
|
|
|
200
|
8 |
|
return $this->map[$ord]; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Get a list of enumerator keys. |
205
|
|
|
* @return Enum[] |
206
|
|
|
*/ |
207
|
7 |
|
public function getKeys(): array |
208
|
|
|
{ |
209
|
7 |
|
return \array_map([$this->enumeration, 'byOrdinal'], \array_keys($this->map)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get a list of mapped data values. |
214
|
|
|
* @return mixed[] |
215
|
|
|
*/ |
216
|
7 |
|
public function getValues(): array |
217
|
|
|
{ |
218
|
7 |
|
return \array_values($this->map); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Search for the given data value. |
223
|
|
|
* @param mixed $value |
|
|
|
|
224
|
|
|
* @param bool $strict Use strict type comparison |
|
|
|
|
225
|
|
|
* @return Enum|null The enumerator object of the first matching data value or NULL |
226
|
|
|
*/ |
227
|
2 |
|
public function search($value, bool $strict = false) |
|
|
|
|
228
|
|
|
{ |
229
|
2 |
|
$ord = \array_search($value, $this->map, $strict); |
230
|
2 |
|
if ($ord !== false) { |
231
|
2 |
|
return ($this->enumeration)::byOrdinal($ord); |
232
|
|
|
} |
233
|
|
|
|
234
|
2 |
|
return null; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Test if the given enumerator key (object or value) exists. |
239
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
240
|
|
|
* @return bool |
|
|
|
|
241
|
|
|
* @see offsetExists |
242
|
|
|
*/ |
243
|
7 |
|
public function contains($enumerator): bool |
244
|
|
|
{ |
245
|
|
|
try { |
246
|
7 |
|
$ord = ($this->enumeration)::get($enumerator)->getOrdinal(); |
247
|
6 |
|
return \array_key_exists($ord, $this->map); |
248
|
1 |
|
} catch (InvalidArgumentException $e) { |
249
|
|
|
// An invalid enumerator can't be contained in this map |
250
|
1 |
|
return false; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/* ArrayAccess */ |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Test if the given enumerator key (object or value) exists and is not NULL |
258
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
259
|
|
|
* @return bool |
|
|
|
|
260
|
|
|
* @see contains |
261
|
|
|
*/ |
262
|
5 |
|
public function offsetExists($enumerator): bool |
263
|
|
|
{ |
264
|
|
|
try { |
265
|
5 |
|
return isset($this->map[($this->enumeration)::get($enumerator)->getOrdinal()]); |
266
|
1 |
|
} catch (InvalidArgumentException $e) { |
267
|
|
|
// An invalid enumerator can't be an offset of this map |
268
|
1 |
|
return false; |
269
|
|
|
} |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get the mapped data value of the given enumerator (object or value). |
274
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
275
|
|
|
* @return mixed The mapped date value of the given enumerator or NULL |
276
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
277
|
|
|
* @see get() |
278
|
|
|
*/ |
279
|
4 |
|
public function offsetGet($enumerator) |
280
|
|
|
{ |
281
|
|
|
try { |
282
|
4 |
|
return $this->get($enumerator); |
283
|
1 |
|
} catch (UnexpectedValueException $e) { |
284
|
1 |
|
return null; |
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Adds the given enumerator (object or value) mapping to the specified data value. |
290
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
291
|
|
|
* @param mixed $value |
|
|
|
|
292
|
|
|
* @return void |
293
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
294
|
|
|
* @see add() |
295
|
|
|
*/ |
296
|
7 |
|
public function offsetSet($enumerator, $value = null): void |
|
|
|
|
297
|
|
|
{ |
298
|
7 |
|
$this->add($enumerator, $value); |
299
|
6 |
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Removes the given enumerator (object or value) mapping. |
303
|
|
|
* @param Enum|null|bool|int|float|string|array $enumerator |
|
|
|
|
304
|
|
|
* @return void |
305
|
|
|
* @throws InvalidArgumentException On an invalid given enumerator |
|
|
|
|
306
|
|
|
* @see remove() |
307
|
|
|
*/ |
308
|
2 |
|
public function offsetUnset($enumerator): void |
309
|
|
|
{ |
310
|
2 |
|
$this->remove($enumerator); |
311
|
2 |
|
} |
312
|
|
|
|
313
|
|
|
/* IteratorAggregate */ |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Get a new Iterator. |
317
|
|
|
* |
318
|
|
|
* @return Iterator Iterator<K extends Enum, V> |
319
|
|
|
*/ |
320
|
2 |
|
public function getIterator(): Iterator |
321
|
|
|
{ |
322
|
2 |
|
$map = $this->map; |
323
|
2 |
|
foreach ($map as $ordinal => $value) { |
324
|
2 |
|
yield ($this->enumeration)::byOrdinal($ordinal) => $value; |
325
|
|
|
} |
326
|
2 |
|
} |
327
|
|
|
|
328
|
|
|
/* Countable */ |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Count the number of elements |
332
|
|
|
* |
333
|
|
|
* @return int |
|
|
|
|
334
|
|
|
*/ |
335
|
3 |
|
public function count(): int |
336
|
|
|
{ |
337
|
3 |
|
return \count($this->map); |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|