55namespace ArrayLookup ;
66
77use ArrayIterator ;
8+ use ArrayLookup \Assert \Filter ;
89use ArrayObject ;
910use Traversable ;
1011use Webmozart \Assert \Assert ;
@@ -24,12 +25,12 @@ final class Finder
2425 */
2526 public static function first (iterable $ data , callable $ filter , bool $ returnKey = false ): mixed
2627 {
28+ // filter must be a callable with bool return type
29+ Filter::boolean ($ filter );
30+
2731 foreach ($ data as $ key => $ datum ) {
2832 $ isFound = $ filter ($ datum , $ key );
2933
30- // returns of callable must be bool
31- Assert::boolean ($ isFound );
32-
3334 if (! $ isFound ) {
3435 continue ;
3536 }
@@ -71,6 +72,9 @@ public static function last(
7172 // ensure data is array for end(), key(), current(), prev() usage
7273 Assert::isArray ($ data );
7374
75+ // filter must be a callable with bool return type
76+ Filter::boolean ($ filter );
77+
7478 // Use end(), key(), current(), prev() usage instead of array_reverse()
7579 // to avoid immediatelly got "Out of memory" on many data
7680 // see https://3v4l.org/IHo2H vs https://3v4l.org/Wqejc
@@ -91,9 +95,6 @@ public static function last(
9195 $ current = current ($ data );
9296 $ isFound = $ filter ($ current , $ key );
9397
94- // returns of callable must be bool
95- Assert::boolean ($ isFound );
96-
9798 if (! $ isFound ) {
9899 // go to previous row
99100 prev ($ data );
@@ -133,12 +134,12 @@ public static function rows(
133134 $ newKey = 0 ;
134135 $ totalFound = 0 ;
135136
137+ // filter must be a callable with bool return type
138+ Filter::boolean ($ filter );
139+
136140 foreach ($ data as $ key => $ datum ) {
137141 $ isFound = $ filter ($ datum , $ key );
138142
139- // returns of callable must be bool
140- Assert::boolean ($ isFound );
141-
142143 if (! $ isFound ) {
143144 continue ;
144145 }
0 commit comments