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');
```
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

View File

@ -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);
}
}
/**

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');
}
/**
* @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);