Skip to content

Commit

Permalink
Add fix for when the buffer starts with a BOM
Browse files Browse the repository at this point in the history
Add fix for when the buffer starts with a BOM and change the offset to
ignore. Added additional test to cover the improved functionality.
  • Loading branch information
Luke Richards committed Mar 8, 2018
1 parent 5da338f commit 2262362
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,21 @@ protected function fetchLine(StreamInterface $stream, &$buffer)
return false;
}

// Check for UTF-8 BOM and remove
// > The Unicode Standard permits the BOM in UTF-8, but does not require or recommend its use.
// > Byte order has no meaning in UTF-8, so its only use in UTF-8 is to
// > signal at the start that the text stream is encoded in UTF-8.
// > https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8
$offset = 0;
if (substr($buffer, 0, 3) == "\xEF\xBB\xBF") {
$offset = 3;
}

// prepare the regex
$regex = $this->getRegex();

$idx = 0;
$row = array();
$offset = 0;
$idx = 0;
$row = array();
while ($offset < $bufferSize) {

// use the regex to pull out matches
Expand Down Expand Up @@ -349,4 +358,4 @@ protected function fetchLine(StreamInterface $stream, &$buffer)
// return the row
return $row;
}
}
}
18 changes: 17 additions & 1 deletion tests/CsvTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Phlib\Csv\Tests;

use Phlib\Csv\Adapter\AdapterInterface;
use Phlib\Csv\Csv;
use Psr\Http\Message\StreamInterface;

Expand Down Expand Up @@ -208,6 +207,23 @@ public function testMoreRowColumnsThanHeaders()
$csv->current();
}

public function testStartsWithBomb()
{
// Additional fields than the headers will be ignored
$csvData = <<<CSV
\xEF\xBB\xBFemail,name
aw@example.com,Adam
lr@example.com,Luke
CSV;
$csv = new Csv($this->getStreamInterface($csvData), true);

$expected = [
'email' => 'aw@example.com',
'name' => 'Adam'
];
$this->assertSame($expected, $csv->current());
}

/**
* @return StreamInterface
*/
Expand Down

0 comments on commit 2262362

Please sign in to comment.