|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Cerbero\Enum\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Cerbero\Enum\CasesCollection; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* The trait to collect the cases of an enum. |
|
9
|
|
|
*/ |
|
10
|
|
|
trait CollectsCases |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Retrieve a collection with all the cases. |
|
14
|
|
|
* |
|
15
|
|
|
* @return CasesCollection<array-key, self> |
|
16
|
|
|
*/ |
|
17
|
34 |
|
public static function collect(): CasesCollection |
|
18
|
|
|
{ |
|
19
|
34 |
|
return new CasesCollection(self::cases()); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Retrieve the count of cases. |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public static function count(): int |
|
26
|
|
|
{ |
|
27
|
1 |
|
return self::collect()->count(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Retrieve the first case. |
|
32
|
|
|
* |
|
33
|
|
|
* @param (callable(self, array-key): bool)|null $callback |
|
|
|
|
|
|
34
|
|
|
*/ |
|
35
|
2 |
|
public static function first(callable $callback = null): ?self |
|
36
|
|
|
{ |
|
37
|
2 |
|
return self::collect()->first($callback); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Retrieve the name of all the cases. |
|
42
|
|
|
* |
|
43
|
|
|
* @return string[] |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public static function names(): array |
|
46
|
|
|
{ |
|
47
|
1 |
|
return self::collect()->names(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Retrieve the value of all the backed cases. |
|
52
|
|
|
* |
|
53
|
|
|
* @return list<string|int> |
|
|
|
|
|
|
54
|
|
|
*/ |
|
55
|
1 |
|
public static function values(): array |
|
56
|
|
|
{ |
|
57
|
1 |
|
return self::collect()->values(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Retrieve an array of values optionally keyed by the given key. |
|
62
|
|
|
* |
|
63
|
|
|
* @template TPluckValue |
|
64
|
|
|
* |
|
65
|
|
|
* @param (callable(self): TPluckValue)|string $value |
|
|
|
|
|
|
66
|
|
|
* @param (callable(self): array-key)|string|null $key |
|
67
|
|
|
* @return array<array-key, TPluckValue> |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
4 |
|
public static function pluck(callable|string $value, callable|string $key = null): array |
|
70
|
|
|
{ |
|
71
|
4 |
|
return self::collect()->pluck($value, $key); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Retrieve the result of mapping over all the cases. |
|
76
|
|
|
* |
|
77
|
|
|
* @template TMapValue |
|
78
|
|
|
* |
|
79
|
|
|
* @param callable(self, array-key): TMapValue $callback |
|
80
|
|
|
* @return array<array-key, TMapValue> |
|
|
|
|
|
|
81
|
|
|
*/ |
|
82
|
2 |
|
public static function map(callable $callback): array |
|
83
|
|
|
{ |
|
84
|
2 |
|
return self::collect()->map($callback); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Retrieve all the cases keyed by their own name. |
|
89
|
|
|
* |
|
90
|
|
|
* @return CasesCollection<array-key, self> |
|
91
|
|
|
*/ |
|
92
|
1 |
|
public static function keyByName(): CasesCollection |
|
93
|
|
|
{ |
|
94
|
1 |
|
return self::collect()->keyByName(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Retrieve all the cases keyed by the given key. |
|
99
|
|
|
* |
|
100
|
|
|
* @param (callable(self): array-key)|string $key |
|
|
|
|
|
|
101
|
|
|
* @return CasesCollection<array-key, self> |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public static function keyBy(callable|string $key): CasesCollection |
|
104
|
|
|
{ |
|
105
|
2 |
|
return self::collect()->keyBy($key); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Retrieve all the cases keyed by their own value. |
|
110
|
|
|
* |
|
111
|
|
|
* @return CasesCollection<array-key, self> |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public static function keyByValue(): CasesCollection |
|
114
|
|
|
{ |
|
115
|
1 |
|
return self::collect()->keyByValue(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Retrieve all the cases grouped by the given key. |
|
120
|
|
|
* |
|
121
|
|
|
* @param (callable(self): array-key)|string $key |
|
|
|
|
|
|
122
|
|
|
* @return CasesCollection<array-key, CasesCollection<array-key, self>> |
|
123
|
|
|
*/ |
|
124
|
4 |
|
public static function groupBy(callable|string $key): CasesCollection |
|
125
|
|
|
{ |
|
126
|
4 |
|
return self::collect()->groupBy($key); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Retrieve only the filtered cases. |
|
131
|
|
|
* |
|
132
|
|
|
* @param (callable(self): bool)|string $filter |
|
|
|
|
|
|
133
|
|
|
* @return CasesCollection<array-key, self> |
|
134
|
|
|
*/ |
|
135
|
3 |
|
public static function filter(callable|string $filter): CasesCollection |
|
136
|
|
|
{ |
|
137
|
3 |
|
return self::collect()->filter($filter); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Retrieve only the cases having the given names. |
|
142
|
|
|
* |
|
143
|
|
|
* @return CasesCollection<array-key, self> |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public static function only(string ...$names): CasesCollection |
|
146
|
|
|
{ |
|
147
|
1 |
|
return self::collect()->only(...$names); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Retrieve only the cases not having the given names. |
|
152
|
|
|
* |
|
153
|
|
|
* @return CasesCollection<array-key, self> |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public static function except(string ...$names): CasesCollection |
|
156
|
|
|
{ |
|
157
|
1 |
|
return self::collect()->except(...$names); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Retrieve only the cases having the given values. |
|
162
|
|
|
* |
|
163
|
|
|
* @return CasesCollection<array-key, self> |
|
164
|
|
|
*/ |
|
165
|
1 |
|
public static function onlyValues(string|int ...$values): CasesCollection |
|
166
|
|
|
{ |
|
167
|
1 |
|
return self::collect()->onlyValues(...$values); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Retrieve only the cases not having the given values. |
|
172
|
|
|
* |
|
173
|
|
|
* @return CasesCollection<array-key, self> |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public static function exceptValues(string|int ...$values): CasesCollection |
|
176
|
|
|
{ |
|
177
|
1 |
|
return self::collect()->exceptValues(...$values); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Retrieve all the cases sorted by their own name ascending. |
|
182
|
|
|
* |
|
183
|
|
|
* @return CasesCollection<array-key, self> |
|
184
|
|
|
*/ |
|
185
|
1 |
|
public static function sort(): CasesCollection |
|
186
|
|
|
{ |
|
187
|
1 |
|
return self::collect()->sort(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Retrieve all the cases sorted by the given key ascending. |
|
192
|
|
|
* |
|
193
|
|
|
* @param (callable(self): mixed)|string $key |
|
|
|
|
|
|
194
|
|
|
* @return CasesCollection<array-key, self> |
|
195
|
|
|
*/ |
|
196
|
2 |
|
public static function sortBy(callable|string $key): CasesCollection |
|
197
|
|
|
{ |
|
198
|
2 |
|
return self::collect()->sortBy($key); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Retrieve all the cases sorted by their own value ascending. |
|
203
|
|
|
* |
|
204
|
|
|
* @return CasesCollection<array-key, self> |
|
205
|
|
|
*/ |
|
206
|
1 |
|
public static function sortByValue(): CasesCollection |
|
207
|
|
|
{ |
|
208
|
1 |
|
return self::collect()->sortByValue(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Retrieve all the cases sorted by their own name descending. |
|
213
|
|
|
* |
|
214
|
|
|
* @return CasesCollection<array-key, self> |
|
215
|
|
|
*/ |
|
216
|
1 |
|
public static function sortDesc(): CasesCollection |
|
217
|
|
|
{ |
|
218
|
1 |
|
return self::collect()->sortDesc(); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Retrieve all the cases sorted by the given key descending. |
|
223
|
|
|
* |
|
224
|
|
|
* @param (callable(self): mixed)|string $key |
|
|
|
|
|
|
225
|
|
|
* @return CasesCollection<array-key, self> |
|
226
|
|
|
*/ |
|
227
|
2 |
|
public static function sortByDesc(callable|string $key): CasesCollection |
|
228
|
|
|
{ |
|
229
|
2 |
|
return self::collect()->sortByDesc($key); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Retrieve all the cases sorted by their own value descending. |
|
234
|
|
|
* |
|
235
|
|
|
* @return CasesCollection<array-key, self> |
|
236
|
|
|
*/ |
|
237
|
1 |
|
public static function sortByDescValue(): CasesCollection |
|
238
|
|
|
{ |
|
239
|
1 |
|
return self::collect()->sortByDescValue(); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|