@@ -27,6 +27,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
27
27
const ESCAPE_CHAR_KEY = 'csv_escape_char ' ;
28
28
const KEY_SEPARATOR_KEY = 'csv_key_separator ' ;
29
29
const HEADERS_KEY = 'csv_headers ' ;
30
+ const ESCAPE_FORMULAS = 'csv_escape_formulas ' ;
30
31
31
32
private $ delimiter ;
32
33
private $ enclosure ;
@@ -75,11 +76,11 @@ public function encode($data, $format, array $context = array())
75
76
}
76
77
}
77
78
78
- list ($ delimiter , $ enclosure , $ escapeChar , $ keySeparator , $ headers ) = $ this ->getCsvOptions ($ context );
79
+ list ($ delimiter , $ enclosure , $ escapeChar , $ keySeparator , $ headers, $ escapeFormulas ) = $ this ->getCsvOptions ($ context );
79
80
80
81
foreach ($ data as &$ value ) {
81
82
$ flattened = array ();
82
- $ this ->flatten ($ value , $ flattened , $ keySeparator );
83
+ $ this ->flatten ($ value , $ flattened , $ keySeparator, '' , $ escapeFormulas );
83
84
$ value = $ flattened ;
84
85
}
85
86
unset($ value );
@@ -183,14 +184,15 @@ public function supportsDecoding($format)
183
184
* @param array $result
184
185
* @param string $keySeparator
185
186
* @param string $parentKey
187
+ * @param bool $escapeFormulas
186
188
*/
187
- private function flatten (array $ array , array &$ result , $ keySeparator , $ parentKey = '' )
189
+ private function flatten (array $ array , array &$ result , $ keySeparator , $ parentKey = '' , $ escapeFormulas = false )
188
190
{
189
191
foreach ($ array as $ key => $ value ) {
190
192
if (is_array ($ value )) {
191
- $ this ->flatten ($ value , $ result , $ keySeparator , $ parentKey .$ key .$ keySeparator );
193
+ $ this ->flatten ($ value , $ result , $ keySeparator , $ parentKey .$ key .$ keySeparator, $ escapeFormulas );
192
194
} else {
193
- if ($ this -> escapeFormulas && \in_array (mb_substr ($ value , 0 , 1 ), $ this ->formulasStartCharacters , true )) {
195
+ if ($ escapeFormulas && \in_array (mb_substr ($ value , 0 , 1 ), $ this ->formulasStartCharacters , true )) {
194
196
$ result [$ parentKey .$ key ] = "\t" .$ value ;
195
197
} else {
196
198
$ result [$ parentKey .$ key ] = $ value ;
@@ -206,12 +208,13 @@ private function getCsvOptions(array $context)
206
208
$ escapeChar = isset ($ context [self ::ESCAPE_CHAR_KEY ]) ? $ context [self ::ESCAPE_CHAR_KEY ] : $ this ->escapeChar ;
207
209
$ keySeparator = isset ($ context [self ::KEY_SEPARATOR_KEY ]) ? $ context [self ::KEY_SEPARATOR_KEY ] : $ this ->keySeparator ;
208
210
$ headers = isset ($ context [self ::HEADERS_KEY ]) ? $ context [self ::HEADERS_KEY ] : array ();
211
+ $ escapeFormulas = isset ($ context [self ::ESCAPE_FORMULAS ]) ? $ context [self ::ESCAPE_FORMULAS ] : $ this ->escapeFormulas ;
209
212
210
213
if (!is_array ($ headers )) {
211
214
throw new InvalidArgumentException (sprintf ('The "%s" context variable must be an array or null, given "%s". ' , self ::HEADERS_KEY , gettype ($ headers )));
212
215
}
213
216
214
- return array ($ delimiter , $ enclosure , $ escapeChar , $ keySeparator , $ headers );
217
+ return array ($ delimiter , $ enclosure , $ escapeChar , $ keySeparator , $ headers, $ escapeFormulas );
215
218
}
216
219
217
220
/**
0 commit comments