From b6e8547b583c082fc9d2d7546c8d1be5ae801bf7 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 7 Mar 2024 05:43:46 +0700 Subject: [PATCH] [Finder] Fix string key on rows get when preserveKey disabled --- src/Finder.php | 11 +++++++++-- tests/FinderTest.php | 5 +++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Finder.php b/src/Finder.php index d75bea0..e753306 100644 --- a/src/Finder.php +++ b/src/Finder.php @@ -11,6 +11,7 @@ use function current; use function end; +use function is_string; use function iterator_to_array; use function key; use function prev; @@ -137,8 +138,14 @@ public static function rows(iterable $data, callable $filter, bool $preserveKey continue; } - $rows[$preserveKey ? $key : $newKey] = $datum; - ++$newKey; + if (is_string($key)) { + $rowKey = $key; + } else { + $rowKey = $preserveKey ? $key : $newKey; + ++$newKey; + } + + $rows[$rowKey] = $datum; } return $rows; diff --git a/tests/FinderTest.php b/tests/FinderTest.php index 05a43e0..13511ad 100644 --- a/tests/FinderTest.php +++ b/tests/FinderTest.php @@ -281,6 +281,11 @@ public static function rowsDataProvider(): array static fn($datum): bool => $datum < 5, [], ], + [ + [6, 7, 'foo' => 8, 9], + static fn($datum): bool => $datum > 7, + ['foo' => 8, 9], + ], ]; }