fix: make Buffer::nth() positional so it stays correct after deletions
Buffer::nth() resolved the requested position by adding it to the first row key and indexing the rows array by that computed key. That assumes contiguous keys, but Buffer::delete() removes rows with unset() without reindexing, leaving gaps. After a deletion nth() therefore returned the wrong record (or an empty array for a valid position, or a record for an out-of-range position). Resolve the position against the record order with array_values() so nth() matches getRecords() and ResultSet::nth().
This commit is contained in:
1 parent
7f690c67cd
commit
e77df85d0c
2 files changed
+22
-9
No files matched your search
+4
-9
@@ -335,16 +335,11 @@ final class Buffer implements TabularData
|
||||
private function nthRow(int $nth, string $method): array
|
||||
{
|
||||
-1 < $nth || throw InvalidArgument::dueToInvalidRecordOffset($nth, $method);
|
||||
if (null === ($first = $this->firstOffset())) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$offset = $first + $nth;
|
||||
if (!array_key_exists($offset, $this->rows)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return $this->rows[$nth + $first];
|
||||
// $nth is a positional index (like ResultSet::nth), so it must be
|
||||
// resolved against the record order rather than the row keys, which
|
||||
// may contain gaps after records have been deleted.
|
||||
return array_values($this->rows)[$nth] ?? [];
|
||||
}
|
||||
|
||||
public function fetchColumn(int|string $index = 0): Iterator
|
||||
|
||||
Reference in new issue
Block a user