1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cerbero\Enum; |
6
|
|
|
|
7
|
|
|
use BackedEnum; |
8
|
|
|
use Countable; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
use JsonSerializable; |
11
|
|
|
use Stringable; |
12
|
|
|
use Traversable; |
13
|
|
|
use UnitEnum; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The collection of enum cases. |
17
|
|
|
* |
18
|
|
|
* @template-covariant TEnum of UnitEnum |
19
|
|
|
* |
20
|
|
|
* @implements IteratorAggregate<array-key, TEnum> |
21
|
|
|
*/ |
22
|
|
|
class CasesCollection implements Countable, IteratorAggregate, JsonSerializable, Stringable |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Whether the cases belong to a backed enum. |
26
|
|
|
*/ |
27
|
|
|
protected readonly bool $enumIsBacked; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Instantiate the class. |
31
|
|
|
* |
32
|
|
|
* @param array<array-key, TEnum> $cases |
|
|
|
|
33
|
|
|
*/ |
34
|
68 |
|
final public function __construct(protected readonly array $cases) |
35
|
|
|
{ |
36
|
68 |
|
$this->enumIsBacked = reset($cases) instanceof BackedEnum; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Turn the collection into a string. |
41
|
|
|
*/ |
42
|
2 |
|
public function __toString(): string |
43
|
|
|
{ |
44
|
2 |
|
return (string) json_encode($this->jsonSerialize()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Turn the collection into a JSON serializable array. |
49
|
|
|
* |
50
|
|
|
* @return list<string|int> |
51
|
|
|
*/ |
52
|
2 |
|
public function jsonSerialize(): array |
53
|
|
|
{ |
54
|
2 |
|
return $this->enumIsBacked ? $this->values() : $this->names(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retrieve the count of cases. |
59
|
|
|
*/ |
60
|
4 |
|
public function count(): int |
61
|
|
|
{ |
62
|
4 |
|
return count($this->cases); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the iterable cases. |
67
|
|
|
* |
68
|
|
|
* @return Traversable<array-key, TEnum> |
69
|
|
|
*/ |
70
|
4 |
|
public function getIterator(): Traversable |
71
|
|
|
{ |
72
|
4 |
|
yield from $this->cases; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Retrieve all the cases as a plain array. |
77
|
|
|
* |
78
|
|
|
* @return array<array-key, TEnum> |
|
|
|
|
79
|
|
|
*/ |
80
|
73 |
|
public function all(): array |
81
|
|
|
{ |
82
|
73 |
|
return $this->cases; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Determine whether the collection contains the given case. |
87
|
|
|
*/ |
88
|
2 |
|
public function has(mixed $case): bool |
89
|
|
|
{ |
90
|
2 |
|
foreach ($this->cases as $instance) { |
91
|
2 |
|
if ($instance->is($case)) { |
92
|
2 |
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
2 |
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Retrieve all the cases as a plain array recursively. |
101
|
|
|
* |
102
|
|
|
* @return array<array-key, mixed> |
|
|
|
|
103
|
|
|
*/ |
104
|
1 |
|
public function toArray(): array |
105
|
|
|
{ |
106
|
1 |
|
$array = []; |
107
|
|
|
|
108
|
1 |
|
foreach ($this->cases as $key => $value) { |
109
|
1 |
|
$array[$key] = $value instanceof static ? $value->toArray() : $value; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $array; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieve the first case. |
117
|
|
|
* |
118
|
|
|
* @param ?callable(TEnum, array-key): bool $callback |
119
|
|
|
* @return ?TEnum |
120
|
|
|
*/ |
121
|
5 |
|
public function first(?callable $callback = null): mixed |
122
|
|
|
{ |
123
|
5 |
|
$callback ??= fn() => true; |
124
|
|
|
|
125
|
5 |
|
foreach ($this->cases as $key => $case) { |
126
|
4 |
|
if ($callback($case, $key)) { |
127
|
4 |
|
return $case; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
1 |
|
return null; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Retrieve all the names of the cases. |
136
|
|
|
* |
137
|
|
|
* @return list<string> |
138
|
|
|
*/ |
139
|
3 |
|
public function names(): array |
140
|
|
|
{ |
141
|
|
|
/** @var list<string> */ |
142
|
3 |
|
return array_column($this->cases, 'name'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Retrieve all the values of the backed cases. |
147
|
|
|
* |
148
|
|
|
* @return list<string|int> |
149
|
|
|
*/ |
150
|
4 |
|
public function values(): array |
151
|
|
|
{ |
152
|
|
|
/** @var list<string|int> */ |
153
|
4 |
|
return array_column($this->cases, 'value'); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Retrieve an array of values optionally keyed by the given key. |
158
|
|
|
* |
159
|
|
|
* @template TPluckValue |
160
|
|
|
* |
161
|
|
|
* @param (callable(TEnum): TPluckValue)|string $value |
|
|
|
|
162
|
|
|
* @param (callable(TEnum): array-key)|string|null $key |
163
|
|
|
* @return array<array-key, TPluckValue> |
|
|
|
|
164
|
|
|
*/ |
165
|
10 |
|
public function pluck(callable|string $value, callable|string|null $key = null): array |
166
|
|
|
{ |
167
|
10 |
|
$result = []; |
168
|
|
|
|
169
|
10 |
|
foreach ($this->cases as $case) { |
170
|
10 |
|
if ($key === null) { |
171
|
6 |
|
$result[] = $case->resolveItem($value); |
172
|
|
|
} else { |
173
|
4 |
|
$result[$case->resolveItem($key)] = $case->resolveItem($value); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
10 |
|
return $result; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Retrieve the result of mapping over the cases. |
182
|
|
|
* |
183
|
|
|
* @template TMapValue |
184
|
|
|
* |
185
|
|
|
* @param callable(TEnum, array-key): TMapValue $callback |
186
|
|
|
* @return array<array-key, TMapValue> |
|
|
|
|
187
|
|
|
*/ |
188
|
3 |
|
public function map(callable $callback): array |
189
|
|
|
{ |
190
|
3 |
|
$keys = array_keys($this->cases); |
191
|
3 |
|
$values = array_map($callback, $this->cases, $keys); |
192
|
|
|
|
193
|
3 |
|
return array_combine($keys, $values); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Retrieve the cases keyed by their own name. |
198
|
|
|
*/ |
199
|
1 |
|
public function keyByName(): static |
200
|
|
|
{ |
201
|
1 |
|
return $this->keyBy('name'); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Retrieve the cases keyed by the given key. |
206
|
|
|
* |
207
|
|
|
* @param (callable(TEnum): array-key)|string $key |
|
|
|
|
208
|
|
|
*/ |
209
|
4 |
|
public function keyBy(callable|string $key): static |
210
|
|
|
{ |
211
|
4 |
|
$keyed = []; |
212
|
|
|
|
213
|
4 |
|
foreach ($this->cases as $case) { |
214
|
4 |
|
$keyed[$case->resolveItem($key)] = $case; |
215
|
|
|
} |
216
|
|
|
|
217
|
4 |
|
return new static($keyed); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Retrieve the cases keyed by their own value. |
222
|
|
|
*/ |
223
|
2 |
|
public function keyByValue(): static |
224
|
|
|
{ |
225
|
2 |
|
return $this->enumIsBacked ? $this->keyBy('value') : new static([]); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Retrieve the cases grouped by the given key. |
230
|
|
|
* |
231
|
|
|
* @param (callable(TEnum): array-key)|string $key |
|
|
|
|
232
|
|
|
* @return array<array-key, static<TEnum>> |
|
|
|
|
233
|
|
|
*/ |
234
|
6 |
|
public function groupBy(callable|string $key): array |
235
|
|
|
{ |
236
|
6 |
|
$grouped = []; |
237
|
|
|
|
238
|
6 |
|
foreach ($this->cases as $case) { |
239
|
6 |
|
$grouped[$case->resolveItem($key)][] = $case; |
240
|
|
|
} |
241
|
|
|
|
242
|
6 |
|
foreach ($grouped as $key => $cases) { |
243
|
6 |
|
$grouped[$key] = new static($cases); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** @var array<array-key, static<TEnum>> */ |
247
|
6 |
|
return $grouped; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Retrieve a new collection with the filtered cases. |
252
|
|
|
* |
253
|
|
|
* @param (callable(TEnum): bool)|string $filter |
|
|
|
|
254
|
|
|
*/ |
255
|
15 |
|
public function filter(callable|string $filter): static |
256
|
|
|
{ |
257
|
15 |
|
$callback = is_callable($filter) ? $filter : fn(UnitEnum $case) => $case->resolveItem($filter) === true; |
258
|
|
|
|
259
|
15 |
|
return new static(array_filter($this->cases, $callback)); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Retrieve a new collection of cases having only the given names. |
264
|
|
|
*/ |
265
|
2 |
|
public function only(string ...$name): static |
266
|
|
|
{ |
267
|
2 |
|
return $this->filter(fn(UnitEnum $case) => in_array($case->name, $name)); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Retrieve a collection of cases not having the given names. |
272
|
|
|
*/ |
273
|
2 |
|
public function except(string ...$name): static |
274
|
|
|
{ |
275
|
2 |
|
return $this->filter(fn(UnitEnum $case) => !in_array($case->name, $name)); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Retrieve a new collection of backed cases having only the given values. |
280
|
|
|
*/ |
281
|
3 |
|
public function onlyValues(string|int ...$value): static |
282
|
|
|
{ |
283
|
3 |
|
return $this->filter(fn(UnitEnum $case) => $this->enumIsBacked && in_array($case->value, $value, true)); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Retrieve a new collection of backed cases not having the given values. |
288
|
|
|
*/ |
289
|
3 |
|
public function exceptValues(string|int ...$value): static |
290
|
|
|
{ |
291
|
3 |
|
return $this->filter(fn(UnitEnum $case) => $this->enumIsBacked && !in_array($case->value, $value, true)); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Retrieve a new collection of cases sorted by their own name ascending. |
296
|
|
|
*/ |
297
|
2 |
|
public function sort(): static |
298
|
|
|
{ |
299
|
2 |
|
return $this->sortBy('name'); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Retrieve a new collection of cases sorted by the given key ascending. |
304
|
|
|
* |
305
|
|
|
* @param (callable(TEnum): mixed)|string $key |
|
|
|
|
306
|
|
|
*/ |
307
|
6 |
|
public function sortBy(callable|string $key): static |
308
|
|
|
{ |
309
|
6 |
|
$cases = $this->cases; |
310
|
|
|
|
311
|
6 |
|
uasort($cases, fn(UnitEnum $a, UnitEnum $b) => $a->resolveItem($key) <=> $b->resolveItem($key)); |
312
|
|
|
|
313
|
6 |
|
return new static($cases); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Retrieve a new collection of cases sorted by their own value ascending. |
318
|
|
|
*/ |
319
|
2 |
|
public function sortByValue(): static |
320
|
|
|
{ |
321
|
2 |
|
return $this->enumIsBacked ? $this->sortBy('value') : new static([]); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Retrieve a new collection of cases sorted by their own name descending. |
326
|
|
|
*/ |
327
|
2 |
|
public function sortDesc(): static |
328
|
|
|
{ |
329
|
2 |
|
return $this->sortByDesc('name'); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* Retrieve a new collection of cases sorted by the given key descending. |
334
|
|
|
* |
335
|
|
|
* @param (callable(TEnum): mixed)|string $key |
|
|
|
|
336
|
|
|
*/ |
337
|
6 |
|
public function sortByDesc(callable|string $key): static |
338
|
|
|
{ |
339
|
6 |
|
$cases = $this->cases; |
340
|
|
|
|
341
|
6 |
|
uasort($cases, fn(UnitEnum $a, UnitEnum $b) => $b->resolveItem($key) <=> $a->resolveItem($key)); |
342
|
|
|
|
343
|
6 |
|
return new static($cases); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Retrieve a new collection of cases sorted by their own value descending. |
348
|
|
|
*/ |
349
|
2 |
|
public function sortByDescValue(): static |
350
|
|
|
{ |
351
|
2 |
|
return $this->enumIsBacked ? $this->sortByDesc('value') : new static([]); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
|