Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
771afcb5f1 | ||
|
|
86e26632f6 | ||
|
|
8614f79da3 | ||
|
|
4827e56cac | ||
|
|
03e85ffc21 | ||
|
|
e4cc8b4eaa | ||
|
|
73341d06a9 |
No files matched your search
@@ -110,6 +110,7 @@ use Box\Spout\Common\Type;
|
|||||||
$reader = ReaderFactory::create(Type::CSV);
|
$reader = ReaderFactory::create(Type::CSV);
|
||||||
$reader->setFieldDelimiter('|');
|
$reader->setFieldDelimiter('|');
|
||||||
$reader->setFieldEnclosure('@');
|
$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:
|
Additionally, if you need to read non UTF-8 files, you can specify the encoding of your file this way:
|
||||||
@@ -159,7 +160,7 @@ Font | Bold | `StyleBuilder::setFontBold()`
|
|||||||
| Strikethrough | `StyleBuilder::setFontStrikethrough()`
|
| Strikethrough | `StyleBuilder::setFontStrikethrough()`
|
||||||
| Font name | `StyleBuilder::setFontName('Arial')`
|
| Font name | `StyleBuilder::setFontName('Arial')`
|
||||||
| Font size | `StyleBuilder::setFontSize(14)`
|
| Font size | `StyleBuilder::setFontSize(14)`
|
||||||
| Font color | `StyleBuilder::setFontSize(Color::BLUE)`<br>`StyleBuilder::setFontSize(Color::rgb(0, 128, 255))`
|
| Font color | `StyleBuilder::setFontColor(Color::BLUE)`<br>`StyleBuilder::setFontColor(Color::rgb(0, 128, 255))`
|
||||||
Alignment | Wrap text | `StyleBuilder::setShouldWrapText()`
|
Alignment | Wrap text | `StyleBuilder::setShouldWrapText()`
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ class Reader extends AbstractReader
|
|||||||
/** @var string Encoding of the CSV file to be read */
|
/** @var string Encoding of the CSV file to be read */
|
||||||
protected $encoding = EncodingHelper::ENCODING_UTF8;
|
protected $encoding = EncodingHelper::ENCODING_UTF8;
|
||||||
|
|
||||||
|
/** @var string Defines the End of line */
|
||||||
|
protected $endOfLineCharacter = "\n";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the field delimiter for the CSV.
|
* Sets the field delimiter for the CSV.
|
||||||
* Needs to be called before opening the reader.
|
* Needs to be called before opening the reader.
|
||||||
@@ -68,6 +71,19 @@ class Reader extends AbstractReader
|
|||||||
return $this;
|
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.
|
* 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.
|
* If setEncoding() was not called, it assumes that the file is encoded in UTF-8.
|
||||||
@@ -88,6 +104,7 @@ class Reader extends AbstractReader
|
|||||||
$this->fieldDelimiter,
|
$this->fieldDelimiter,
|
||||||
$this->fieldEnclosure,
|
$this->fieldEnclosure,
|
||||||
$this->encoding,
|
$this->encoding,
|
||||||
|
$this->endOfLineCharacter,
|
||||||
$this->globalFunctionsHelper
|
$this->globalFunctionsHelper
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ class RowIterator implements IteratorInterface
|
|||||||
/** @var string End of line delimiter, encoded using the same encoding as the CSV */
|
/** @var string End of line delimiter, encoded using the same encoding as the CSV */
|
||||||
protected $encodedEOLDelimiter;
|
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 resource $filePointer Pointer to the CSV file to read
|
||||||
* @param string $fieldDelimiter Character that delimits fields
|
* @param string $fieldDelimiter Character that delimits fields
|
||||||
@@ -56,12 +59,13 @@ class RowIterator implements IteratorInterface
|
|||||||
* @param string $encoding Encoding of the CSV file to be read
|
* @param string $encoding Encoding of the CSV file to be read
|
||||||
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
* @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->filePointer = $filePointer;
|
||||||
$this->fieldDelimiter = $fieldDelimiter;
|
$this->fieldDelimiter = $fieldDelimiter;
|
||||||
$this->fieldEnclosure = $fieldEnclosure;
|
$this->fieldEnclosure = $fieldEnclosure;
|
||||||
$this->encoding = $encoding;
|
$this->encoding = $encoding;
|
||||||
|
$this->inputEOLDelimiter = $endOfLineDelimiter;
|
||||||
$this->globalFunctionsHelper = $globalFunctionsHelper;
|
$this->globalFunctionsHelper = $globalFunctionsHelper;
|
||||||
|
|
||||||
$this->encodingHelper = new EncodingHelper($globalFunctionsHelper);
|
$this->encodingHelper = new EncodingHelper($globalFunctionsHelper);
|
||||||
@@ -172,7 +176,7 @@ class RowIterator implements IteratorInterface
|
|||||||
protected function getEncodedEOLDelimiter()
|
protected function getEncodedEOLDelimiter()
|
||||||
{
|
{
|
||||||
if (!isset($this->encodedEOLDelimiter)) {
|
if (!isset($this->encodedEOLDelimiter)) {
|
||||||
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8("\n", $this->encoding);
|
$this->encodedEOLDelimiter = $this->encodingHelper->attemptConversionFromUTF8($this->inputEOLDelimiter, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->encodedEOLDelimiter;
|
return $this->encodedEOLDelimiter;
|
||||||
|
|||||||
@@ -21,9 +21,9 @@ class Sheet implements SheetInterface
|
|||||||
* @param string $encoding Encoding of the CSV file to be read
|
* @param string $encoding Encoding of the CSV file to be read
|
||||||
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ class SheetIterator implements IteratorInterface
|
|||||||
* @param string $encoding Encoding of the CSV file to be read
|
* @param string $encoding Encoding of the CSV file to be read
|
||||||
* @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper
|
* @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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -377,4 +377,50 @@ class ReaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
|
|
||||||
return $allRows;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
csv--11,csv--12,csv--13␍csv--21,csv--22,csv--23␍csv--31,csv--32,csv--33
|
||||||
|
Reference in new issue
Block a user