@@ -100,7 +100,7 @@ class CsvView extends SerializedView
100100 /**
101101 * List of bom signs for encodings.
102102 *
103- * @var array
103+ * @var array<string, string>
104104 */
105105 protected array $ bomMap ;
106106
@@ -129,6 +129,7 @@ class CsvView extends SerializedView
129129 * - 'delimiter': (default ',') CSV Delimiter, defaults to comma
130130 * - 'enclosure': (default '"') CSV Enclosure for use with fputcsv()
131131 * - 'newline': (default '\n') CSV Newline replacement for use with fputcsv()
132+ * - 'escape': (default '\\') CSV escape character for use with fputcsv()
132133 * - 'eol': (default '\n') End-of-line character the csv
133134 * - 'bom': (default false) Adds BOM (byte order mark) header
134135 * - 'setSeparator': (default false) Adds sep=[_delimiter] in the first line
@@ -146,6 +147,7 @@ class CsvView extends SerializedView
146147 'delimiter ' => ', ' ,
147148 'enclosure ' => '" ' ,
148149 'newline ' => "\n" ,
150+ 'escape ' => '\\' ,
149151 'eol ' => PHP_EOL ,
150152 'null ' => '' ,
151153 'bom ' => false ,
@@ -193,7 +195,7 @@ public static function contentType(): string
193195 /**
194196 * Serialize view vars.
195197 *
196- * @param array|string $serialize The name(s) of the view variable(s) that
198+ * @param array<string> |string $serialize The name(s) of the view variable(s) that
197199 * need(s) to be serialized
198200 * @return string The serialized data or false.
199201 */
@@ -295,7 +297,7 @@ protected function _renderRow(?array $row = null): string
295297 * data by writing the array to a temporary file and
296298 * returning its contents
297299 *
298- * @param array|null $row Row data
300+ * @param array<string|null> |null $row Row data
299301 * @return string|false String with the row in csv-syntax, false on fputscv failure
300302 */
301303 protected function _generateRow (?array $ row = null ): string |false
@@ -333,20 +335,23 @@ protected function _generateRow(?array $row = null): string|false
333335 $ delimiter = $ this ->getConfig ('delimiter ' );
334336 $ enclosure = $ this ->getConfig ('enclosure ' );
335337 $ newline = $ this ->getConfig ('newline ' );
338+ $ escape = $ this ->getConfig ('escape ' );
336339
340+ /** @phpstan-ignore-next-line */
337341 $ row = str_replace (["\r\n" , "\n" , "\r" ], $ newline , $ row );
338342 if ($ enclosure === '' ) {
339343 // fputcsv does not supports empty enclosure
340344 if (fputs ($ fp , implode ($ delimiter , $ row ) . "\n" ) === false ) {
341345 return false ;
342346 }
343347 } else {
344- if (fputcsv ($ fp , $ row , $ delimiter , $ enclosure ) === false ) {
348+ if (fputcsv ($ fp , $ row , $ delimiter , $ enclosure, $ escape ) === false ) {
345349 return false ;
346350 }
347351 }
348352
349353 rewind ($ fp );
354+ unset($ row );
350355
351356 $ csv = '' ;
352357 while (($ buffer = fgets ($ fp , 4096 )) !== false ) {
0 commit comments