File tree Expand file tree Collapse file tree 3 files changed +113
-1
lines changed
Expand file tree Collapse file tree 3 files changed +113
-1
lines changed Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ Features
1717
1818- [x] Verify at least times: ` once() ` , ` twice() ` , ` times() `
1919- [x] Verify exact times: ` once() ` , ` twice() ` , ` times() `
20- - [x] Search data: ` first() ` , ` last() `
20+ - [x] Search data: ` first() ` , ` last() ` , ` rows() `
2121
2222Installation
2323------------
@@ -319,3 +319,34 @@ var_dump(Finder::last(
319319 static fn ($datum, $key): bool => $datum < 5 && $key >= 0
320320)); // null
321321```
322+
323+ * 3. ` Finder::rows() ` *
324+
325+ It get rows data filtered found.
326+
327+ ``` php
328+ use ArrayLookup\Finder;
329+
330+ $data = [6, 7, 8, 9];
331+ var_dump(Finder::rows(
332+ $data,
333+ static fn($datum): bool => $datum > 6
334+ )); // [7, 8, 9]
335+
336+ var_dump(Finder::rows(
337+ $data,
338+ static fn ($datum): bool => $datum < 5
339+ )); // []
340+
341+ // ... with PRESERVE original key
342+ var_dump(Finder::rows(
343+ $data,
344+ static fn ($datum): bool => $datum > 6,
345+ true
346+ )); // [1 => 7, 2 => 8, 3 => 9]
347+
348+ var_dump(Finder::rows(
349+ $data,
350+ static fn ($datum): bool => $datum < 5,
351+ true
352+ )); // []
Original file line number Diff line number Diff line change @@ -116,4 +116,31 @@ public static function last(
116116
117117 return null ;
118118 }
119+
120+ /**
121+ * @param array<int|string, mixed>|Traversable<int|string, mixed> $data
122+ * @param callable(mixed $datum, int|string|null $key=): bool $filter
123+ * @return mixed[]
124+ */
125+ public static function rows (iterable $ data , callable $ filter , bool $ preserveKey = false ): array
126+ {
127+ $ rows = [];
128+ $ newKey = 0 ;
129+
130+ foreach ($ data as $ key => $ datum ) {
131+ $ isFound = $ filter ($ datum , $ key );
132+
133+ // returns of callable must be bool
134+ Assert::boolean ($ isFound );
135+
136+ if (! $ isFound ) {
137+ continue ;
138+ }
139+
140+ $ rows [$ preserveKey ? $ key : $ newKey ] = $ datum ;
141+ ++$ newKey ;
142+ }
143+
144+ return $ rows ;
145+ }
119146}
Original file line number Diff line number Diff line change @@ -253,4 +253,58 @@ public static function lastReturnKeyResortKeyDataProvider(): array
253253 ],
254254 ];
255255 }
256+
257+ #[DataProvider('rowsDataProvider ' )]
258+ public function testRows (iterable $ data , callable $ filter , array $ expected ): void
259+ {
260+ $ this ->assertSame (
261+ $ expected ,
262+ Finder::rows ($ data , $ filter )
263+ );
264+ }
265+
266+ public static function rowsDataProvider (): array
267+ {
268+ return [
269+ [
270+ [6 , 7 , 8 , 9 ],
271+ static fn ($ datum ): bool => $ datum > 6 ,
272+ [7 , 8 , 9 ],
273+ ],
274+ [
275+ [6 , 7 , 8 , 9 ],
276+ static fn ($ datum ): bool => $ datum < 5 ,
277+ [],
278+ ],
279+ ];
280+ }
281+
282+ #[DataProvider('rowsDataProviderPreserveKey ' )]
283+ public function testRowsPreserveKey (iterable $ data , callable $ filter , array $ expected ): void
284+ {
285+ $ this ->assertSame (
286+ $ expected ,
287+ Finder::rows ($ data , $ filter , true )
288+ );
289+ }
290+
291+ public static function rowsDataProviderPreserveKey (): array
292+ {
293+ return [
294+ [
295+ [6 , 7 , 8 , 9 ],
296+ static fn ($ datum ): bool => $ datum > 6 ,
297+ [
298+ 1 => 7 ,
299+ 2 => 8 ,
300+ 3 => 9
301+ ],
302+ ],
303+ [
304+ [6 , 7 , 8 , 9 ],
305+ static fn ($ datum ): bool => $ datum < 5 ,
306+ [],
307+ ],
308+ ];
309+ }
256310}
You can’t perform that action at this time.
0 commit comments