Skip to content

Commit

Permalink
Merge pull request box#173 from sfichera/master
Browse files Browse the repository at this point in the history
Support for variable EOL for CSV
  • Loading branch information
adrilo committed Feb 14, 2016
2 parents e4cc8b4 + 86e2663 commit 771afcb
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ use Box\Spout\Common\Type;
$reader = ReaderFactory::create(Type::CSV);
$reader->setFieldDelimiter('|');
$reader->setFieldEnclosure('@');
$reader->setEndOfLineCharacter("\r");
```

Additionally, if you need to read non UTF-8 files, you can specify the encoding of your file this way:
Expand Down
17 changes: 17 additions & 0 deletions src/Spout/Reader/CSV/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class Reader extends AbstractReader
/** @var string Encoding of the CSV file to be read */
protected $encoding = EncodingHelper::ENCODING_UTF8;

/** @var string Defines the End of line */
protected $endOfLineCharacter = "\n";

/**
* Sets the field delimiter for the CSV.
* Needs to be called before opening the reader.
Expand Down Expand Up @@ -68,6 +71,19 @@ public function setEncoding($encoding)
return $this;
}

/**
* Sets the EOL for the CSV.
* Needs to be called before opening the reader.
*
* @param string $endOfLineCharacter used to properly get lines from the CSV file.
* @return Reader
*/
public function setEndOfLineCharacter($endOfLineCharacter)
{
$this->endOfLineCharacter = $endOfLineCharacter;
return $this;
}

/**
* Opens the file at the given path to make it ready to be read.
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
Expand All @@ -88,6 +104,7 @@ protected function openReader($filePath)
$this->fieldDelimiter,
$this->fieldEnclosure,
$this->encoding,
$this->endOfLineCharacter,
$this->globalFunctionsHelper
);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Spout/Reader/CSV/RowIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,23 @@ class RowIterator implements IteratorInterface
/** @var string End of line delimiter, encoded using the same encoding as the CSV */
protected $encodedEOLDelimiter;

/** @var string End of line delimiter, given by the user as input. */
protected $inputEOLDelimiter;

/**
* @param resource $filePointer Pointer to the CSV file to read
* @param string $fieldDelimiter Character that delimits fields
* @param string $fieldEnclosure Character that enclose fields
* @param string $encoding Encoding of the CSV file to be read
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineDelimiter, $globalFunctionsHelper)
{
$this->filePointer = $filePointer;
$this->fieldDelimiter = $fieldDelimiter;
$this->fieldEnclosure = $fieldEnclosure;
$this->encoding = $encoding;
$this->inputEOLDelimiter = $endOfLineDelimiter;
$this->globalFunctionsHelper = $globalFunctionsHelper;

$this->encodingHelper = new EncodingHelper($globalFunctionsHelper);
Expand Down Expand Up @@ -172,7 +176,7 @@ protected function getNextUTF8EncodedLine()
protected function getEncodedEOLDelimiter()
{
if (!isset($this->encodedEOLDelimiter)) {
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8("\n", $this->encoding);
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8($this->inputEOLDelimiter, $this->encoding);
}

return $this->encodedEOLDelimiter;
Expand Down
4 changes: 2 additions & 2 deletions src/Spout/Reader/CSV/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class Sheet implements SheetInterface
* @param string $encoding Encoding of the CSV file to be read
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper)
{
$this->rowIterator = new RowIterator($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper);
$this->rowIterator = new RowIterator($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Spout/Reader/CSV/SheetIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ class SheetIterator implements IteratorInterface
* @param string $encoding Encoding of the CSV file to be read
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
*/
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper)
{
$this->sheet = new Sheet($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper);
$this->sheet = new Sheet($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper);
}

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/Spout/Reader/CSV/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,50 @@ private function getAllRowsForFile(

return $allRows;
}

/**
* @return array
*/
public function dataProviderForTestReadCustomEOL()
{
return [
['csv_with_CR_EOL.csv', "\r"],
['csv_standard.csv', "\n"],
];
}

/**
* @dataProvider dataProviderForTestReadCustomEOL
*
* @param string $fileName
* @param string $customEOL
* @return void
*/
public function testReadCustomEOLs($fileName, $customEOL)
{
$allRows = [];
$resourcePath = $this->getResourcePath($fileName);

/** @var \Box\Spout\Reader\CSV\Reader $reader */
$reader = ReaderFactory::create(Type::CSV);
$reader
->setEndOfLineCharacter($customEOL)
->open($resourcePath);

foreach ($reader->getSheetIterator() as $sheet) {
foreach ($sheet->getRowIterator() as $row) {
$allRows[] = $row;
}
}

$reader->close();

$expectedRows = [
['csv--11', 'csv--12', 'csv--13'],
['csv--21', 'csv--22', 'csv--23'],
['csv--31', 'csv--32', 'csv--33'],
];
$this->assertEquals($expectedRows, $allRows);
}

}
1 change: 1 addition & 0 deletions tests/resources/csv/csv_with_CR_EOL.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
csv--11,csv--12,csv--13csv--21,csv--22,csv--23csv--31,csv--32,csv--33
Expand Down

0 comments on commit 771afcb

Please sign in to comment.