Skip to content

Commit

Permalink
Making sure the mapper works on column index only
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 30, 2023
1 parent bf2b1ac commit 81ee34e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,12 @@ public function getRecords(array $headerMapper = []): Iterator
protected function combineHeader(array $headerMapper): Iterator
{
return match (true) {
$headerMapper === $this->header,
[] === $headerMapper => $this->records,
$headerMapper === $this->header, [] === $headerMapper => $this->records,
default => new MapIterator($this->records, function (array $record) use ($headerMapper): array {
$assocRecord = [];
$row = array_values($record);
foreach ($headerMapper as $offset => $headerName) {
$assocRecord[$headerName] = $record[$offset] ?? null;
$assocRecord[$headerName] = $row[$offset] ?? null;
}

return $assocRecord;
Expand Down
36 changes: 36 additions & 0 deletions src/ResultSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,40 @@ public function testHeaderMapperOnResultSet(): void
'does not exists' => null,
], [...$results][0]);
}

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

$resultSet = Statement::create()->process($reader);

self::assertSame(
['nom de famille' => 'doe', 'prenom' => 'john', 'e-mail' => 'john.doe@example.com'],
[...$resultSet->getRecords([1 => 'nom de famille', 0 => 'prenom', 2 => 'e-mail'])][0]
);
}

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

$resultSet = Statement::create()->process($reader);

self::assertSame(
['nom de famille' => null, 'prenom' => null, 'e-mail' => null],
[...$resultSet->getRecords(['lastname' => 'nom de famille', 'firstname' => 'prenom', 'e-mail' => 'e-mail'])][0]
);
}
}

0 comments on commit 81ee34e

Please sign in to comment.