From b6ef3b29f6e184a40c7f5be76f383b5f84c43e31 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 2 Nov 2023 14:06:31 +0900 Subject: [PATCH] ArrayHandler add next/prev key search --- .phive/phars.xml | 2 +- src/Combined/ArrayHandler.php | 48 ++++++++++ .../CoreLibsCombinedArrayHandlerTest.php | 95 ++++++++++++++++++- tools/phpstan | 2 +- 4 files changed, 144 insertions(+), 3 deletions(-) diff --git a/.phive/phars.xml b/.phive/phars.xml index f6d3d32..4705afd 100644 --- a/.phive/phars.xml +++ b/.phive/phars.xml @@ -4,6 +4,6 @@ - + diff --git a/src/Combined/ArrayHandler.php b/src/Combined/ArrayHandler.php index d664310..bbe3943 100644 --- a/src/Combined/ArrayHandler.php +++ b/src/Combined/ArrayHandler.php @@ -236,6 +236,54 @@ class ArrayHandler return $hit_list; } + /** + * main wrapper function for next/prev key + * + * @param array $array array to search in + * @param int|string $key key for next/prev + * @param bool $next [=true] if to search next or prev + * @return int|string|null Next/prev key or null for end/first + */ + private static function arrayGetKey(array $array, int|string $key, bool $next = true): int|string|null + { + $keys = array_keys($array); + if (($position = array_search($key, $keys, true)) === false) { + return null; + } + $next_position = $next ? $position + 1 : $position - 1; + + if (!isset($keys[$next_position])) { + return null; + } + return $keys[$next_position]; + } + + /** + * Get previous array key from an array + * null on not found + * + * @param array $array + * @param int|string $key + * @return int|string|null Next key, or null for not found + */ + public static function arrayGetPrevKey(array $array, int|string $key): int|string|null + { + return self::arrayGetKey($array, $key, false); + } + + /** + * Get next array key from an array + * null on not found + * + * @param array $array + * @param int|string $key + * @return int|string|null Next key, or null for not found + */ + public static function arrayGetNextKey(array $array, int|string $key): int|string|null + { + return self::arrayGetKey($array, $key, true); + } + /** * correctly recursive merges as an array as array_merge_recursive * just glues things together diff --git a/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php b/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php index 25ade3a..8dc5729 100644 --- a/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php +++ b/test/phpunit/Combined/CoreLibsCombinedArrayHandlerTest.php @@ -1098,16 +1098,109 @@ final class CoreLibsCombinedArrayHandlerTest extends TestCase * @testdox arrayFlatForKey array $input will be $expected [$_dataName] * * @param array $input + * @param string $search * @param array $expected * @return void */ - public function testArrayFlatForKey(array $input, $search, array $expected): void + public function testArrayFlatForKey(array $input, string $search, array $expected): void { $this->assertEquals( $expected, \CoreLibs\Combined\ArrayHandler::arrayFlatForKey($input, $search) ); } + + /** + * Undocumented function + * + * @return array + */ + public function providerArrayGetNextPrevKey(): array + { + return [ + 'find, ok' => [ + 'input' => [ + 'a' => 'First', + 'b' => 'Second', + 'c' => 'Third', + ], + 'b', + 'a', + 'c' + ], + 'find, first' => [ + 'input' => [ + 'a' => 'First', + 'b' => 'Second', + 'c' => 'Third', + ], + 'a', + null, + 'b' + ], + 'find, last' => [ + 'input' => [ + 'a' => 'First', + 'b' => 'Second', + 'c' => 'Third', + ], + 'c', + 'b', + null + ], + 'find, not found' => [ + 'input' => [ + 'a' => 'First', + 'b' => 'Second', + 'c' => 'Third', + ], + 'z', + null, + null + ], + 'int, index' => [ + 'input' => [ + 'a', + 'b', + 'c' + ], + 1, + 0, + 2 + ] + ]; + } + + /** + * Undocumented function + * + * @covers ::arrayGetPrevKey, ::arrayGetNextKey + * @dataProvider providerArrayGetNextPrevKey + * @testdox arrayGetNextPrevKey get next/prev key for $search wtih $expected_prev/$expected_next [$_dataName] + * + * @param array $input + * @param int|string $search + * @param int|string|null $expected_prev + * @param int|string|null $expected_next + * @return void + */ + public function testArrayGetNextPrevKey( + array $input, + int|string $search, + int|string|null $expected_prev, + int|string|null $expected_next + ): void { + $this->assertEquals( + $expected_prev, + \CoreLibs\Combined\ArrayHandler::arrayGetPrevKey($input, $search), + 'Find prev key in array' + ); + $this->assertEquals( + $expected_next, + \CoreLibs\Combined\ArrayHandler::arrayGetNextKey($input, $search), + 'Find next key in array' + ); + } } // __END__ diff --git a/tools/phpstan b/tools/phpstan index 823d322..71878da 120000 --- a/tools/phpstan +++ b/tools/phpstan @@ -1 +1 @@ -/home/clemens/.phive/phars/phpstan-1.10.37.phar \ No newline at end of file +/home/clemens/.phive/phars/phpstan-1.10.40.phar \ No newline at end of file