Skip to content

Commit 4b3c27e

Browse files
committed
Arrays::first() & last(): added parameter $else
1 parent 526b40c commit 4b3c27e

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/Utils/Arrays.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

tests/Utils/Arrays.first().phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@ test('with predicate', function () {
3838
test('predicate arguments', function () {
3939
Arrays::first([2 => 'x'], fn() => Assert::same(['x', 2, [2 => 'x']], func_get_args()));
4040
});
41+
42+
test('else', function () {
43+
Assert::same(123, Arrays::first([], else: fn() => 123));
44+
});

tests/Utils/Arrays.last().phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ test('with predicate', function () {
3737
test('predicate arguments', function () {
3838
Arrays::last([2 => 'x'], fn() => Assert::same(['x', 2, [2 => 'x']], func_get_args()));
3939
});
40+
41+
test('else', function () {
42+
Assert::same(123, Arrays::last([], else: fn() => 123));
43+
});

0 commit comments

Comments
 (0)