From d567b6f0dba0ddf5922d4f78a41e46f287ed243c Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Tue, 19 Nov 2024 10:22:38 +1300 Subject: [PATCH] Allow configuring escape for CsvWriter PHP 8.4 deprecates calls to `fputcsv` that don't pass `''` as the `$escape` argument. Without this change, it's impossible to avoid a deprecation warning when using `CsvWriter`. If a new major version of this library is released, it should change the default to empty string to avoid the warning by default. --- src/CsvWriter.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/CsvWriter.php b/src/CsvWriter.php index 2766b69..54bdb35 100644 --- a/src/CsvWriter.php +++ b/src/CsvWriter.php @@ -33,14 +33,20 @@ class CsvWriter extends AbstractStreamWriter */ protected $prependHeaderRow; + /** + * @var string + */ + private $escape; + /** * @param string $delimiter The delimiter * @param string $enclosure The enclosure * @param resource $stream * @param boolean $utf8Encoding * @param boolean $prependHeaderRow + * @param string $escape */ - public function __construct($delimiter = ',', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false) + public function __construct($delimiter = ',', $enclosure = '"', $stream = null, $utf8Encoding = false, $prependHeaderRow = false, $escape = '\\') { parent::__construct($stream); @@ -48,6 +54,7 @@ public function __construct($delimiter = ',', $enclosure = '"', $stream = null, $this->enclosure = $enclosure; $this->utf8Encoding = $utf8Encoding; $this->prependHeaderRow = $prependHeaderRow; + $this->escape = $escape; } /** @@ -67,9 +74,9 @@ public function writeItem(array $item) { if ($this->prependHeaderRow && 1 == $this->row++) { $headers = array_keys($item); - fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure); + fputcsv($this->getStream(), $headers, $this->delimiter, $this->enclosure, $this->escape); } - fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure); + fputcsv($this->getStream(), $item, $this->delimiter, $this->enclosure, $this->escape); } }