Skip to content

Commit 81220c7

Browse files
committed
Arrays::firstKey(), lastKey(): predicate is optional [WIP]
1 parent 1eae810 commit 81220c7

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/Utils/Arrays.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,7 @@ public static function contains(array $array, mixed $value): bool
129129
*/
130130
public static function first(array $array, ?callable $predicate = null): mixed
131131
{
132-
$key = $predicate
133-
? self::firstKey($array, $predicate)
134-
: array_key_first($array);
132+
$key = self::firstKey($array, $predicate);
135133
return $key === null ? null : $array[$key];
136134
}
137135

@@ -145,19 +143,20 @@ public static function first(array $array, ?callable $predicate = null): mixed
145143
*/
146144
public static function last(array $array, ?callable $predicate = null): mixed
147145
{
148-
$key = $predicate
149-
? self::firstKey(array_reverse($array, preserve_keys: true), $predicate)
150-
: array_key_last($array);
146+
$key = self::lastKey($array, $predicate);
151147
return $key === null ? null : $array[$key];
152148
}
153149

154150

155151
/**
156-
* Returns the key of first matching item the specified predicate or null if there is no such item.
152+
* Returns the key of first item (matching the specified predicate if given) or null if there is no such item.
157153
* The callback has the signature `function ($value, $key, $array): bool`.
158154
*/
159-
public static function firstKey(array $array, callable $predicate): int|string|null
155+
public static function firstKey(array $array, ?callable $predicate = null): int|string|null
160156
{
157+
if (!$predicate) {
158+
return array_key_first($array);
159+
}
161160
foreach ($array as $k => $v) {
162161
if ($predicate($v, $k, $array)) {
163162
return $k;
@@ -168,12 +167,14 @@ public static function firstKey(array $array, callable $predicate): int|string|n
168167

169168

170169
/**
171-
* Returns the key of last matching item the specified predicate or null if there is no such item.
170+
* Returns the key of last item (matching the specified predicate if given) or null if there is no such item.
172171
* The callback has the signature `function ($value, $key, $array): bool`.
173172
*/
174-
public static function lastKey(array $array, callable $predicate): int|string|null
173+
public static function lastKey(array $array, ?callable $predicate = null): int|string|null
175174
{
176-
return self::firstKey(array_reverse($array, preserve_keys: true), $predicate);
175+
return $predicate
176+
? self::firstKey(array_reverse($array, preserve_keys: true), $predicate)
177+
: array_key_last($array);
177178
}
178179

179180

tests/Utils/Arrays.firstKey().phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,17 @@ use Tester\Assert;
88
require __DIR__ . '/../bootstrap.php';
99

1010

11+
test('no predicate', function () {
12+
Assert::null(Arrays::firstKey([]));
13+
Assert::same(0, Arrays::firstKey([null]));
14+
Assert::same(0, Arrays::firstKey([1, 2, 3]));
15+
Assert::same(5, Arrays::firstKey([5 => 1, 2, 3]));
16+
});
17+
1118
test('internal array pointer is not affected', function () {
1219
$arr = [1, 2, 3];
1320
end($arr);
14-
Assert::null(Arrays::firstKey($arr, fn() => false));
21+
Assert::same(0, Arrays::firstKey($arr));
1522
Assert::same(3, current($arr));
1623
});
1724

tests/Utils/Arrays.lastKey().phpt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ use Tester\Assert;
88
require __DIR__ . '/../bootstrap.php';
99

1010

11+
test('no predicate', function () {
12+
Assert::null(Arrays::lastKey([]));
13+
Assert::same(0, Arrays::lastKey([null]));
14+
Assert::same(2, Arrays::lastKey([1, 2, 3]));
15+
Assert::same(7, Arrays::lastKey([5 => 1, 2, 3]));
16+
});
17+
1118
test('internal array pointer is not affected', function () {
1219
$arr = [1, 2, 3];
13-
Assert::same(2, Arrays::lastKey($arr, fn() => true));
20+
Assert::same(2, Arrays::lastKey($arr));
1421
Assert::same(1, current($arr));
1522
});
1623

0 commit comments

Comments
 (0)