From 175b0a530e4af0d2faa0891e1926bbd842f7cac1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 14 Jan 2023 18:30:39 +0700 Subject: [PATCH 1/3] Add $preserveKey flag ability to not preserve key on Finder::last() --- src/Finder.php | 25 +++++++++++++++++++++---- tests/FinderTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index 25446fb..63838a3 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -56,8 +56,12 @@ private static function resolveArrayFromTraversable(Traversable $traversable): a * @param array|Traversable $data * @param callable(mixed $datum, int|string|null $key=): bool $filter */ - public static function last(iterable $data, callable $filter, bool $returnKey = false): mixed - { + public static function last( + iterable $data, + callable $filter, + bool $returnKey = false, + bool $preserveKey = true + ): mixed { // convert to array when data is Traversable instance if ($data instanceof Traversable) { $data = self::resolveArrayFromTraversable($data); @@ -74,10 +78,15 @@ public static function last(iterable $data, callable $filter, bool $returnKey = end($data); // grab current key - $key = key($data); + $key = key($data); + $resortkey = -1; // key = null means no longer current data while ($key !== null) { + if (! $preserveKey && $returnKey) { + ++$resortkey; + } + $current = current($data); $isFound = $filter($current, $key); @@ -94,7 +103,15 @@ public static function last(iterable $data, callable $filter, bool $returnKey = continue; } - return $returnKey ? $key : $current; + if (! $returnKey) { + return $current; + } + + if ($preserveKey) { + return $key; + } + + return $resortkey; } return null; diff --git a/tests/FinderTest.php b/tests/FinderTest.php index eeaac0c..de1e73f 100644 --- a/tests/FinderTest.php +++ b/tests/FinderTest.php @@ -235,4 +235,31 @@ public function lastReturnKeyDataProvider(): array ], ]; } + + /** + * @dataProvider lastReturnKeyResortKeyDataProvider + */ + public function testLastReturnKeyResortKey(iterable $data, callable $filter, mixed $expected): void + { + $this->assertSame( + $expected, + Finder::last($data, $filter, true, false) + ); + } + + public function lastReturnKeyResortKeyDataProvider(): array + { + return [ + [ + [6, 7, 8, 9], + static fn($datum): bool => $datum > 5, + 0, + ], + [ + [6, 7, 8, 9], + static fn($datum): bool => $datum < 5, + null, + ], + ]; + } } From 8bc44d1067c436cec11a7e6f226131bc5d295fcb Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 14 Jan 2023 18:32:32 +0700 Subject: [PATCH 2/3] documentation --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 2927c73..5859446 100644 --- a/README.md +++ b/README.md @@ -270,12 +270,21 @@ var_dump(\ArrayLookup\Finder::last( // RETURN the Array key, pass true to 3rd arg +// ... with PRESERVE original key var_dump(\ArrayLookup\Finder::last( $data, static fn ($datum): bool => $datum > 5, true )); // 3 +// ... with RESORT key +var_dump(\ArrayLookup\Finder::last( + $data, + static fn ($datum): bool => $datum > 5, + true, + false +)); // 0 + var_dump(\ArrayLookup\Finder::last( $data, static fn ($datum): bool => $datum < 5, From 02c5b8e75a78bb7f5a9734c7e8b52002eb9fd022 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 14 Jan 2023 18:34:29 +0700 Subject: [PATCH 3/3] documentation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5859446..6eff9f8 100644 --- a/README.md +++ b/README.md @@ -277,7 +277,7 @@ var_dump(\ArrayLookup\Finder::last( true )); // 3 -// ... with RESORT key +// ... with RESORT key, first key is last record var_dump(\ArrayLookup\Finder::last( $data, static fn ($datum): bool => $datum > 5,