Skip to content

Commit d0427c1

Browse files
committed
Arrays: searchKey() silently renamed to getKeyOffset()
1 parent 8a4b795 commit d0427c1

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed

src/Utils/Arrays.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,21 @@ public static function mergeTree(array $array1, array $array2): array
8484
* @param string|int $key
8585
* @return int|null offset if it is found, null otherwise
8686
*/
87-
public static function searchKey(array $array, $key): ?int
87+
public static function getKeyOffset(array $array, $key): ?int
8888
{
8989
return Helpers::falseToNull(array_search(self::toKey($key), array_keys($array), true));
9090
}
9191

9292

93+
/**
94+
* @deprecated use getKeyOffset()
95+
*/
96+
public static function searchKey(array $array, $key): ?int
97+
{
98+
return self::getKeyOffset($array, $key);
99+
}
100+
101+
93102
/**
94103
* Inserts the contents of the $inserted array into the $array immediately after the $key.
95104
* If $key is null (or does not exist), it is inserted at the end.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Utils\Arrays::getKeyOffset()
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Utils\Arrays;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
$arr = [
17+
'' => 'first',
18+
0 => 'second',
19+
7 => 'third',
20+
1 => 'fourth',
21+
];
22+
23+
Assert::same(3, Arrays::getKeyOffset($arr, '1'));
24+
Assert::same(3, Arrays::getKeyOffset($arr, 1));
25+
Assert::same(2, Arrays::getKeyOffset($arr, 7));
26+
Assert::same(1, Arrays::getKeyOffset($arr, 0));
27+
Assert::same(0, Arrays::getKeyOffset($arr, null));
28+
Assert::same(0, Arrays::getKeyOffset($arr, ''));
29+
Assert::null(Arrays::getKeyOffset($arr, 'undefined'));

tests/Utils/Arrays.searchKey().phpt

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)