diff options
author | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-01-15 12:55:32 +0100 |
---|---|---|
committer | Ignace Nyamagana Butera <nyamsprod@gmail.com> | 2015-01-15 13:00:30 +0100 |
commit | 22560a9980684a124c1dc629aaec15cef77c49ab (patch) | |
tree | eeeec90ddc97aaaf50c741d920621303cf4b8393 | |
parent | a7f92b56d37bfce593903b4afe211ce9429188ab (diff) | |
download | csv-22560a9980684a124c1dc629aaec15cef77c49ab.zip csv-22560a9980684a124c1dc629aaec15cef77c49ab.tar.gz csv-22560a9980684a124c1dc629aaec15cef77c49ab.tar.bz2 |
adding BOM to output #65
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | src/AbstractCsv.php | 8 | ||||
-rw-r--r-- | src/Config/Controls.php | 31 | ||||
-rw-r--r-- | test/CsvTest.php | 7 |
4 files changed, 51 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e46b898..f09f18c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ #Changelog All Notable changes to `League\Csv` will be documented in this file + +## 6.3.0 - XXXX-XX-XX + +### Added +- `AbstractCSV::setBOMOnOutput` and `AbstractCSV::hasBOMOnOutput` to manage outputting the CSV with or without BOM. for BC by default no BOM sequence is added to the CSV on output. + ## 6.2.0 - 2014-12-12 ### Added diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index 8758caf..31a2ee3 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -317,12 +317,18 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate if (! is_null($filename) && self::isValidString($filename)) { $filename = (string) $filename; $filename = filter_var($filename, FILTER_UNSAFE_RAW, ['flags' => FILTER_FLAG_STRIP_LOW]); + $bom = ''; + if ($this->bom_on_output) { + $bom = chr(239).chr(187).chr(191); + } header('Content-Type: application/octet-stream'); header('Content-Transfer-Encoding: binary'); header('Content-Disposition: attachment; filename="'.$filename.'"'); if (! $iterator instanceof SplTempFileObject) { - header('Content-Length: '.$iterator->getSize()); + $size = $iterator->getSize() + strlen($bom); + header('Content-Length: '.$size); } + echo $bom; } //@codeCoverageIgnoreEnd $iterator->rewind(); diff --git a/src/Config/Controls.php b/src/Config/Controls.php index e98058d..85cafdf 100644 --- a/src/Config/Controls.php +++ b/src/Config/Controls.php @@ -55,6 +55,13 @@ trait Controls protected $flags = SplFileObject::READ_CSV; /** + * Tells whether we need to add the BOM sequences at the beginning + * of CSV output + * @var boolean + */ + protected $bom_on_output = false; + + /** * return a SplFileOjbect * * @return \SplFileOjbect @@ -231,4 +238,28 @@ trait Controls { return $this->flags; } + + /** + * Should we add the BOM sequence + * + * @param bool $use_bom + * + * @return $this + */ + public function setBOMOnOutput($use_bom) + { + $this->bom_on_output = (bool) $use_bom; + + return $this; + } + + /** + * returns the BOM status + * + * @return bool + */ + public function hasBOMOnOutput() + { + return $this->bom_on_output; + } } diff --git a/test/CsvTest.php b/test/CsvTest.php index f7e65ed..267790d 100644 --- a/test/CsvTest.php +++ b/test/CsvTest.php @@ -105,6 +105,13 @@ class CsvTest extends PHPUnit_Framework_TestCase $this->assertSame([','], $this->csv->detectDelimiterList()); } + public function testBOMSettings() + { + $this->assertFalse($this->csv->hasBOMOnOutput()); + $this->csv->setBOMOnOutput(true); + $this->assertTrue($this->csv->hasBOMOnOutput()); + } + /** * @expectedException InvalidArgumentException * @expectedExceptionMessage `$nb_rows` must be a valid positive integer |