Skip to content

Commit b70db5f

Browse files
committed
Adding validation on ResultSet header argument
1 parent 05d0169 commit b70db5f

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

src/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ protected function computeHeader(array $header)
316316
return $header;
317317
}
318318

319-
throw new SyntaxError('The header record must be empty or a flat array with unique string values');
319+
throw new SyntaxError('The header record must be an empty or a flat array with unique string values.');
320320
}
321321

322322
/**

src/ResultSet.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,20 @@ class ResultSet implements Countable, IteratorAggregate, JsonSerializable
5252
public function __construct(Iterator $records, array $header)
5353
{
5454
$this->records = $records;
55+
$this->validateHeader($header);
5556
$this->header = $header;
5657
}
5758

59+
/**
60+
* @throws SyntaxError if the header syntax is invalid
61+
*/
62+
protected function validateHeader(array $header): void
63+
{
64+
if ($header !== array_unique(array_filter($header, 'is_string'))) {
65+
throw new SyntaxError('The header record must be an empty or a flat array with unique string values.');
66+
}
67+
}
68+
5869
/**
5970
* {@inheritdoc}
6071
*/
@@ -86,6 +97,7 @@ public function getIterator(): Generator
8697
*/
8798
public function getRecords(array $header = []): Generator
8899
{
100+
$this->validateHeader($header);
89101
$records = $this->combineHeader($header);
90102
foreach ($records as $offset => $value) {
91103
yield $offset => $value;

tests/ReaderTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ public function testCall(): void
183183
$csv = Reader::createFromFileObject($file);
184184
$csv->setHeaderOffset(0);
185185

186-
$res = (new Statement())->process($csv);
186+
$res = Statement::create()->process($csv);
187187
self::assertEquals($csv->fetchOne(3), $res->fetchOne(3));
188188
self::assertEquals($csv->fetchColumn('firstname'), $res->fetchColumn('firstname'));
189189
self::assertEquals($csv->fetchPairs('lastname', 0), $res->fetchPairs('lastname', 0));

tests/ResultSetTest.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use League\Csv\InvalidArgument;
1717
use League\Csv\Reader;
1818
use League\Csv\Statement;
19+
use League\Csv\SyntaxError;
1920
use OutOfBoundsException;
2021
use PHPUnit\Framework\TestCase;
2122
use SplTempFileObject;
@@ -193,7 +194,8 @@ public function testIntervalThrowException(): void
193194
* @covers League\Csv\Statement::where
194195
* @covers League\Csv\Statement::create
195196
* @covers League\Csv\Statement::process
196-
* @covers League\Csv\Statement::combineHeader
197+
* @covers League\Csv\ResultSet::combineHeader
198+
* @covers League\Csv\ResultSet::getRecords
197199
*/
198200
public function testFilter(): void
199201
{
@@ -516,4 +518,20 @@ public function testJsonSerialize(): void
516518
json_encode($result)
517519
);
518520
}
521+
522+
/**
523+
* @covers ::validateHeader
524+
*/
525+
public function testHeaderThrowsExceptionOnError(): void
526+
{
527+
self::expectException(SyntaxError::class);
528+
$csv = Reader::createFromString(
529+
'field1,field1,field3
530+
1,2,3
531+
4,5,6'
532+
);
533+
$csv->setDelimiter(',');
534+
$resultSet = Statement::create()->process($csv);
535+
Statement::create()->process($resultSet, ['foo', 'foo']);
536+
}
519537
}

0 commit comments

Comments
 (0)