Skip to content

Commit b6e8547

Browse files
committed
[Finder] Fix string key on rows get when preserveKey disabled
1 parent 14cc25b commit b6e8547

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/Finder.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use function current;
1313
use function end;
14+
use function is_string;
1415
use function iterator_to_array;
1516
use function key;
1617
use function prev;
@@ -137,8 +138,14 @@ public static function rows(iterable $data, callable $filter, bool $preserveKey
137138
continue;
138139
}
139140

140-
$rows[$preserveKey ? $key : $newKey] = $datum;
141-
++$newKey;
141+
if (is_string($key)) {
142+
$rowKey = $key;
143+
} else {
144+
$rowKey = $preserveKey ? $key : $newKey;
145+
++$newKey;
146+
}
147+
148+
$rows[$rowKey] = $datum;
142149
}
143150

144151
return $rows;

tests/FinderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ public static function rowsDataProvider(): array
281281
static fn($datum): bool => $datum < 5,
282282
[],
283283
],
284+
[
285+
[6, 7, 'foo' => 8, 9],
286+
static fn($datum): bool => $datum > 7,
287+
['foo' => 8, 9],
288+
],
284289
];
285290
}
286291

0 commit comments

Comments
 (0)