Skip to content
This repository was archived by the owner on May 26, 2022. It is now read-only.

Commit 771afcb

Browse files
committed
Merge pull request #173 from sfichera/master
Support for variable EOL for CSV
2 parents e4cc8b4 + 86e2663 commit 771afcb

File tree

7 files changed

+75
-6
lines changed

7 files changed

+75
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ use Box\Spout\Common\Type;
110110
$reader = ReaderFactory::create(Type::CSV);
111111
$reader->setFieldDelimiter('|');
112112
$reader->setFieldEnclosure('@');
113+
$reader->setEndOfLineCharacter("\r");
113114
```
114115

115116
Additionally, if you need to read non UTF-8 files, you can specify the encoding of your file this way:

src/Spout/Reader/CSV/Reader.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class Reader extends AbstractReader
2929
/** @var string Encoding of the CSV file to be read */
3030
protected $encoding = EncodingHelper::ENCODING_UTF8;
3131

32+
/** @var string Defines the End of line */
33+
protected $endOfLineCharacter = "\n";
34+
3235
/**
3336
* Sets the field delimiter for the CSV.
3437
* Needs to be called before opening the reader.
@@ -68,6 +71,19 @@ public function setEncoding($encoding)
6871
return $this;
6972
}
7073

74+
/**
75+
* Sets the EOL for the CSV.
76+
* Needs to be called before opening the reader.
77+
*
78+
* @param string $endOfLineCharacter used to properly get lines from the CSV file.
79+
* @return Reader
80+
*/
81+
public function setEndOfLineCharacter($endOfLineCharacter)
82+
{
83+
$this->endOfLineCharacter = $endOfLineCharacter;
84+
return $this;
85+
}
86+
7187
/**
7288
* Opens the file at the given path to make it ready to be read.
7389
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
@@ -88,6 +104,7 @@ protected function openReader($filePath)
88104
$this->fieldDelimiter,
89105
$this->fieldEnclosure,
90106
$this->encoding,
107+
$this->endOfLineCharacter,
91108
$this->globalFunctionsHelper
92109
);
93110
}

src/Spout/Reader/CSV/RowIterator.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,23 @@ class RowIterator implements IteratorInterface
4949
/** @var string End of line delimiter, encoded using the same encoding as the CSV */
5050
protected $encodedEOLDelimiter;
5151

52+
/** @var string End of line delimiter, given by the user as input. */
53+
protected $inputEOLDelimiter;
54+
5255
/**
5356
* @param resource $filePointer Pointer to the CSV file to read
5457
* @param string $fieldDelimiter Character that delimits fields
5558
* @param string $fieldEnclosure Character that enclose fields
5659
* @param string $encoding Encoding of the CSV file to be read
5760
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
5861
*/
59-
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
62+
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineDelimiter, $globalFunctionsHelper)
6063
{
6164
$this->filePointer = $filePointer;
6265
$this->fieldDelimiter = $fieldDelimiter;
6366
$this->fieldEnclosure = $fieldEnclosure;
6467
$this->encoding = $encoding;
68+
$this->inputEOLDelimiter = $endOfLineDelimiter;
6569
$this->globalFunctionsHelper = $globalFunctionsHelper;
6670

6771
$this->encodingHelper = new EncodingHelper($globalFunctionsHelper);
@@ -172,7 +176,7 @@ protected function getNextUTF8EncodedLine()
172176
protected function getEncodedEOLDelimiter()
173177
{
174178
if (!isset($this->encodedEOLDelimiter)) {
175-
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8("\n", $this->encoding);
179+
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8($this->inputEOLDelimiter, $this->encoding);
176180
}
177181

178182
return $this->encodedEOLDelimiter;

src/Spout/Reader/CSV/Sheet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class Sheet implements SheetInterface
2121
* @param string $encoding Encoding of the CSV file to be read
2222
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
2323
*/
24-
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
24+
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper)
2525
{
26-
$this->rowIterator = new RowIterator($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper);
26+
$this->rowIterator = new RowIterator($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper);
2727
}
2828

2929
/**

src/Spout/Reader/CSV/SheetIterator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ class SheetIterator implements IteratorInterface
2525
* @param string $encoding Encoding of the CSV file to be read
2626
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
2727
*/
28-
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper)
28+
public function __construct($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper)
2929
{
30-
$this->sheet = new Sheet($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $globalFunctionsHelper);
30+
$this->sheet = new Sheet($filePointer, $fieldDelimiter, $fieldEnclosure, $encoding, $endOfLineCharacter, $globalFunctionsHelper);
3131
}
3232

3333
/**

tests/Spout/Reader/CSV/ReaderTest.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,4 +377,50 @@ private function getAllRowsForFile(
377377

378378
return $allRows;
379379
}
380+
381+
/**
382+
* @return array
383+
*/
384+
public function dataProviderForTestReadCustomEOL()
385+
{
386+
return [
387+
['csv_with_CR_EOL.csv', "\r"],
388+
['csv_standard.csv', "\n"],
389+
];
390+
}
391+
392+
/**
393+
* @dataProvider dataProviderForTestReadCustomEOL
394+
*
395+
* @param string $fileName
396+
* @param string $customEOL
397+
* @return void
398+
*/
399+
public function testReadCustomEOLs($fileName, $customEOL)
400+
{
401+
$allRows = [];
402+
$resourcePath = $this->getResourcePath($fileName);
403+
404+
/** @var \Box\Spout\Reader\CSV\Reader $reader */
405+
$reader = ReaderFactory::create(Type::CSV);
406+
$reader
407+
->setEndOfLineCharacter($customEOL)
408+
->open($resourcePath);
409+
410+
foreach ($reader->getSheetIterator() as $sheet) {
411+
foreach ($sheet->getRowIterator() as $row) {
412+
$allRows[] = $row;
413+
}
414+
}
415+
416+
$reader->close();
417+
418+
$expectedRows = [
419+
['csv--11', 'csv--12', 'csv--13'],
420+
['csv--21', 'csv--22', 'csv--23'],
421+
['csv--31', 'csv--32', 'csv--33'],
422+
];
423+
$this->assertEquals($expectedRows, $allRows);
424+
}
425+
380426
}

tests/resources/csv/csv_with_CR_EOL.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
csv--11,csv--12,csv--13csv--21,csv--22,csv--23csv--31,csv--32,csv--33

0 commit comments

Comments
 (0)