summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md6
-rw-r--r--src/AbstractCsv.php8
-rw-r--r--src/Config/Controls.php31
-rw-r--r--test/CsvTest.php7
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