@@ -48,38 +48,38 @@ public static function containsKey(iterable $iterable, mixed $key): bool
48
48
49
49
50
50
/**
51
- * Returns the first item (matching the specified predicate if given) or null if there is no such item.
52
- * The callback has the signature `function ($value, $key, $iterable): bool`.
51
+ * Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null .
52
+ * The $predicate has the signature `function ($value, $key, $iterable): bool`.
53
53
* @template T
54
54
* @param iterable<T> $iterable
55
55
* @return ?T
56
56
*/
57
- public static function first (iterable $ iterable , ?callable $ predicate = null ): mixed
57
+ public static function first (iterable $ iterable , ?callable $ predicate = null , ? callable $ else = null ): mixed
58
58
{
59
59
foreach ($ iterable as $ k => $ v ) {
60
60
if (!$ predicate || $ predicate ($ v , $ k , $ iterable )) {
61
61
return $ v ;
62
62
}
63
63
}
64
- return null ;
64
+ return $ else ? $ else () : null ;
65
65
}
66
66
67
67
68
68
/**
69
- * Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
70
- * The callback has the signature `function ($value, $key, $iterable): bool`.
69
+ * Returns the key of first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null .
70
+ * The $predicate has the signature `function ($value, $key, $iterable): bool`.
71
71
* @template T
72
72
* @param iterable<T, mixed> $iterable
73
73
* @return ?T
74
74
*/
75
- public static function firstKey (iterable $ iterable , ?callable $ predicate = null ): mixed
75
+ public static function firstKey (iterable $ iterable , ?callable $ predicate = null , ? callable $ else = null ): mixed
76
76
{
77
77
foreach ($ iterable as $ k => $ v ) {
78
78
if (!$ predicate || $ predicate ($ v , $ k , $ iterable )) {
79
79
return $ k ;
80
80
}
81
81
}
82
- return null ;
82
+ return $ else ? $ else () : null ;
83
83
}
84
84
85
85
0 commit comments