Skip to content

Commit

Permalink
Bugfix issue #524
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 10, 2024
1 parent 9b1a03f commit 0504116
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ All Notable changes to `Csv` will be documented in this file
- the `AbstractCsv` BOM related properties are moved to being `Bom` instances instead of nullable string.
- `setOutpuBOM` will only accept valid BOM sequences all other values except the empty string will throw a `ValueError` exception;
- The package no longer requires the `ext-mbstring` extension to work. But you should have it install in your system in order to use the `mbstring` related stream filters.
- Issue [#524](https://github.com/thephpleague/csv/issues/524) fix issue with `ResultSet::chunkBy` not working as documented.

### Removed

Expand Down
2 changes: 2 additions & 0 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ public function chunkBy(int $recordsCount): iterable
++$nbRecords;
if ($nbRecords === $recordsCount) {
yield new self($records, $header);
$records = [];
$nbRecords = 0;
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,4 +410,24 @@ public function testHeaderMapperOnResultSetAlwaysIgnoreTheColumnName(): void
->process($reader)
->getRecords(['lastname' => 'nom de famille', 'firstname' => 'prenom', 'e-mail' => 'e-mail'])];
}

public function testChunkByIssue524(): void
{
$csv = <<<CSV
firstname,lastname,e-mail
john,doe,john.doe@example.com
jane,doe,jane.doe@example.com
jose,doe,jose.doe@example.com
jeny,doe,jeny.doe@example.com
jack,doe,jack.doe@example.com
CSV;
$reader = Reader::createFromString($csv)->setHeaderOffset(0);

$total = [];
foreach ($reader->chunkBy(2) as $row) {
$total[] = count($row);
}

self::assertSame([2, 2, 1], $total);
}
}

0 comments on commit 0504116

Please sign in to comment.