@@ -121,30 +121,34 @@ public static function contains(array $array, mixed $value): bool
121121
122122
123123 /**
124- * Returns the first item from the array (matching the specified predicate if given) or null if there is no such item.
124+ * Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null .
125125 * The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
126126 * @template T
127127 * @param array<T> $array
128128 * @return ?T
129129 */
130- public static function first (array $ array , ?callable $ predicate = null ): mixed
130+ public static function first (array $ array , ?callable $ predicate = null , ? callable $ else = null ): mixed
131131 {
132132 $ key = self ::firstKey ($ array , $ predicate );
133- return $ key === null ? null : $ array [$ key ];
133+ return $ key === null
134+ ? ($ else ? $ else () : null )
135+ : $ array [$ key ];
134136 }
135137
136138
137139 /**
138- * Returns the last item from the array (matching the specified predicate if given) or null if there is no such item.
140+ * Returns the last item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null .
139141 * The $predicate has the signature `function (mixed $value, int|string $key, array $array): bool`.
140142 * @template T
141143 * @param array<T> $array
142144 * @return ?T
143145 */
144- public static function last (array $ array , ?callable $ predicate = null ): mixed
146+ public static function last (array $ array , ?callable $ predicate = null , ? callable $ else = null ): mixed
145147 {
146148 $ key = self ::lastKey ($ array , $ predicate );
147- return $ key === null ? null : $ array [$ key ];
149+ return $ key === null
150+ ? ($ else ? $ else () : null )
151+ : $ array [$ key ];
148152 }
149153
150154
0 commit comments