Set BOM as optional on CSV writer (#265)
This commit is contained in:
parent
aa25678a83
commit
b02d13cd40
10
README.md
10
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');
|
$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
|
||||||
|
@ -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,8 +77,10 @@ class Writer extends AbstractWriter
|
|||||||
*/
|
*/
|
||||||
protected function openWriter()
|
protected function openWriter()
|
||||||
{
|
{
|
||||||
// Adds UTF-8 BOM for Unicode compatibility
|
if ($this->shouldAddBOM) {
|
||||||
$this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
|
// Adds UTF-8 BOM for Unicode compatibility
|
||||||
|
$this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -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
|
||||||
*/
|
*/
|
||||||
@ -148,13 +161,14 @@ class WriterTest extends \PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $allRows
|
* @param array $allRows
|
||||||
* @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);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user