From b02d13cd406cf3489b490215fa2316b2b7c484ec Mon Sep 17 00:00:00 2001 From: Marie Date: Thu, 7 Jul 2016 15:21:41 +0200 Subject: [PATCH] Set BOM as optional on CSV writer (#265) --- README.md | 10 +++++++++- src/Spout/Writer/CSV/Writer.php | 21 +++++++++++++++++++-- tests/Spout/Writer/CSV/WriterTest.php | 19 +++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index fd896d3..eb80938 100644 --- a/README.md +++ b/README.md @@ -118,7 +118,15 @@ Additionally, if you need to read non UTF-8 files, you can specify the encoding $reader->setEncoding('UTF-16LE'); ``` -The writer always generate CSV files encoded in UTF-8, with a BOM. +By default, the writer generates CSV files encoded in UTF-8, with a BOM. +It is however possible to not include the BOM: +```php +use Box\Spout\Writer\WriterFactory; +use Box\Spout\Common\Type; + +$writer = WriterFactory::create(Type::CSV); +$writer->setShouldAddBOM(false); +``` ### Configuring the XLSX and ODS readers and writers diff --git a/src/Spout/Writer/CSV/Writer.php b/src/Spout/Writer/CSV/Writer.php index 4327902..f7f1fda 100644 --- a/src/Spout/Writer/CSV/Writer.php +++ b/src/Spout/Writer/CSV/Writer.php @@ -29,6 +29,9 @@ class Writer extends AbstractWriter /** @var int */ protected $lastWrittenRowIndex = 0; + /** @var bool */ + protected $shouldAddBOM = true; + /** * Sets the field delimiter for the CSV * @@ -55,6 +58,18 @@ class Writer extends AbstractWriter return $this; } + /** + * Set if a BOM has to be added to the file + * + * @param bool $shouldAddBOM + * @return Writer + */ + public function setShouldAddBOM($shouldAddBOM) + { + $this->shouldAddBOM = (bool) $shouldAddBOM; + return $this; + } + /** * Opens the CSV streamer and makes it ready to accept data. * @@ -62,8 +77,10 @@ class Writer extends AbstractWriter */ protected function openWriter() { - // Adds UTF-8 BOM for Unicode compatibility - $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8); + if ($this->shouldAddBOM) { + // Adds UTF-8 BOM for Unicode compatibility + $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8); + } } /** diff --git a/tests/Spout/Writer/CSV/WriterTest.php b/tests/Spout/Writer/CSV/WriterTest.php index ee343c2..77a6638 100644 --- a/tests/Spout/Writer/CSV/WriterTest.php +++ b/tests/Spout/Writer/CSV/WriterTest.php @@ -74,6 +74,19 @@ class WriterTest extends \PHPUnit_Framework_TestCase $this->assertContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should contain a UTF-8 BOM'); } + /** + * @return void + */ + public function testWriteShouldNotAddUtf8Bom() + { + $allRows = [ + ['csv--11', 'csv--12'], + ]; + $writtenContent = $this->writeToCsvFileAndReturnWrittenContent($allRows, 'csv_no_bom.csv', ',', '"', false); + + $this->assertNotContains(EncodingHelper::BOM_UTF8, $writtenContent, 'The CSV file should not contain a UTF-8 BOM'); + } + /** * @return void */ @@ -148,13 +161,14 @@ class WriterTest extends \PHPUnit_Framework_TestCase } /** - * @param array $allRows + * @param array $allRows * @param string $fileName * @param string $fieldDelimiter * @param string $fieldEnclosure + * @param bool $shouldAddBOM * @return null|string */ - private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"') + private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true) { $this->createGeneratedFolderIfNeeded($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName); @@ -162,6 +176,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase $writer = WriterFactory::create(Type::CSV); $writer->setFieldDelimiter($fieldDelimiter); $writer->setFieldEnclosure($fieldEnclosure); + $writer->setShouldAddBOM($shouldAddBOM); $writer->openToFile($resourcePath); $writer->addRows($allRows);