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