Skip to content

Commit

Permalink
Avoid parsing stop on empty rows of CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
mmenozzi committed Oct 22, 2018
1 parent 0c823c4 commit 1d36e14
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public function __construct(
public function parseRow(): Promise
{
return call(function () {
if ($this->fileHandle->eof()) {
return null;
}
$buffer = '';
$newLinePos = null;
while ($chunk = yield $this->fileHandle->read()) {
Expand All @@ -53,12 +56,12 @@ public function parseRow(): Promise
break;
}
}
$bufferSize = \strlen($buffer);
$seekOffset = $bufferSize-$newLinePos;
yield $this->fileHandle->seek(-($seekOffset-1), \SEEK_CUR);
$row = substr($buffer, 0, $newLinePos);
if (empty($row)) {
return null;
$row = $buffer;
if ($newLinePos !== false) {
$bufferSize = \strlen($buffer);
$seekOffset = $bufferSize-$newLinePos;
yield $this->fileHandle->seek(-($seekOffset-1), \SEEK_CUR);
$row = substr($buffer, 0, $newLinePos);
}
$this->rowsParsed++;
return str_getcsv($row, $this->delimiter, $this->enclosure, $this->escape);
Expand Down
19 changes: 19 additions & 0 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,23 @@ public function testParseRowsWithDifferentDelimiter()
$rows[2]
);
}

public function testParseFileWithEmptyRows()
{
$rows = [];
Loop::run(function () use (&$rows) {
$parser = new Parser(yield File\open(__DIR__ . '/empty-rows.csv', 'rb'));
while ($row = yield $parser->parseRow()) {
$rows[] = $row;
}
$this->assertEquals(10, $parser->getRowsParsed());
});
$this->assertCount(10, $rows);
$this->assertEquals(['sku', 'qty'], $rows[0]);
$this->assertEquals(['AAA', '123'], $rows[1]);
$this->assertEquals(['', '2'], $rows[3]);
$this->assertEquals(['A'], $rows[4]);
$this->assertEquals([''], $rows[8]);
$this->assertEquals(['test'], $rows[9]);
}
}
10 changes: 10 additions & 0 deletions tests/empty-rows.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
sku,qty
AAA,123
BBB,1
,2
A




test

0 comments on commit 1d36e14

Please sign in to comment.