Set BOM as optional on CSV writer (#265)

This commit is contained in:
Marie 2016-07-07 15:21:41 +02:00 committed by Adrien Loison
parent aa25678a83
commit b02d13cd40
3 changed files with 45 additions and 5 deletions

View File

@ -118,7 +118,15 @@ Additionally, if you need to read non UTF-8 files, you can specify the encoding
$reader->setEncoding('UTF-16LE'); $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 ### Configuring the XLSX and ODS readers and writers

View File

@ -29,6 +29,9 @@ class Writer extends AbstractWriter
/** @var int */ /** @var int */
protected $lastWrittenRowIndex = 0; protected $lastWrittenRowIndex = 0;
/** @var bool */
protected $shouldAddBOM = true;
/** /**
* Sets the field delimiter for the CSV * Sets the field delimiter for the CSV
* *
@ -55,6 +58,18 @@ class Writer extends AbstractWriter
return $this; 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. * Opens the CSV streamer and makes it ready to accept data.
* *
@ -62,9 +77,11 @@ class Writer extends AbstractWriter
*/ */
protected function openWriter() protected function openWriter()
{ {
if ($this->shouldAddBOM) {
// Adds UTF-8 BOM for Unicode compatibility // Adds UTF-8 BOM for Unicode compatibility
$this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8); $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
} }
}
/** /**
* Adds data to the currently opened writer. * Adds data to the currently opened writer.

View File

@ -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'); $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 * @return void
*/ */
@ -152,9 +165,10 @@ class WriterTest extends \PHPUnit_Framework_TestCase
* @param string $fileName * @param string $fileName
* @param string $fieldDelimiter * @param string $fieldDelimiter
* @param string $fieldEnclosure * @param string $fieldEnclosure
* @param bool $shouldAddBOM
* @return null|string * @return null|string
*/ */
private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"') private function writeToCsvFileAndReturnWrittenContent($allRows, $fileName, $fieldDelimiter = ',', $fieldEnclosure = '"', $shouldAddBOM = true)
{ {
$this->createGeneratedFolderIfNeeded($fileName); $this->createGeneratedFolderIfNeeded($fileName);
$resourcePath = $this->getGeneratedResourcePath($fileName); $resourcePath = $this->getGeneratedResourcePath($fileName);
@ -162,6 +176,7 @@ class WriterTest extends \PHPUnit_Framework_TestCase
$writer = WriterFactory::create(Type::CSV); $writer = WriterFactory::create(Type::CSV);
$writer->setFieldDelimiter($fieldDelimiter); $writer->setFieldDelimiter($fieldDelimiter);
$writer->setFieldEnclosure($fieldEnclosure); $writer->setFieldEnclosure($fieldEnclosure);
$writer->setShouldAddBOM($shouldAddBOM);
$writer->openToFile($resourcePath); $writer->openToFile($resourcePath);
$writer->addRows($allRows); $writer->addRows($allRows);