@@ -27,6 +27,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2727const ESCAPE_CHAR_KEY ='csv_escape_char ' ;
2828const KEY_SEPARATOR_KEY ='csv_key_separator ' ;
2929const HEADERS_KEY ='csv_headers ' ;
30+ const ESCAPE_FORMULAS_KEY ='csv_escape_formulas ' ;
3031
3132private $ delimiter ;
3233private $ enclosure ;
@@ -75,11 +76,11 @@ public function encode($data, $format, array $context = array())
7576 }
7677 }
7778
78- list ($ delimiter ,$ enclosure ,$ escapeChar ,$ keySeparator ,$ headers ) =$ this ->getCsvOptions ($ context );
79+ list ($ delimiter ,$ enclosure ,$ escapeChar ,$ keySeparator ,$ headers, $ escapeFormulas ) =$ this ->getCsvOptions ($ context );
7980
8081foreach ($ dataas &$ value ) {
8182$ flattened =array ();
82- $ this ->flatten ($ value ,$ flattened ,$ keySeparator );
83+ $ this ->flatten ($ value ,$ flattened ,$ keySeparator, '' , $ escapeFormulas );
8384$ value =$ flattened ;
8485 }
8586 unset($ value );
@@ -183,14 +184,15 @@ public function supportsDecoding($format)
183184 * @param array $result
184185 * @param string $keySeparator
185186 * @param string $parentKey
187+ * @param bool $escapeFormulas
186188 */
187- private function flatten (array $ array ,array &$ result ,$ keySeparator ,$ parentKey ='' )
189+ private function flatten (array $ array ,array &$ result ,$ keySeparator ,$ parentKey ='' , $ escapeFormulas = false )
188190 {
189191foreach ($ arrayas $ key =>$ value ) {
190192if (is_array ($ value )) {
191- $ this ->flatten ($ value ,$ result ,$ keySeparator ,$ parentKey .$ key .$ keySeparator );
193+ $ this ->flatten ($ value ,$ result ,$ keySeparator ,$ parentKey .$ key .$ keySeparator, $ escapeFormulas );
192194 }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 )) {
194196$ result [$ parentKey .$ key ] ="\t" .$ value ;
195197 }else {
196198$ result [$ parentKey .$ key ] =$ value ;
@@ -206,12 +208,13 @@ private function getCsvOptions(array $context)
206208$ escapeChar =isset ($ context [self ::ESCAPE_CHAR_KEY ]) ?$ context [self ::ESCAPE_CHAR_KEY ] :$ this ->escapeChar ;
207209$ keySeparator =isset ($ context [self ::KEY_SEPARATOR_KEY ]) ?$ context [self ::KEY_SEPARATOR_KEY ] :$ this ->keySeparator ;
208210$ headers =isset ($ context [self ::HEADERS_KEY ]) ?$ context [self ::HEADERS_KEY ] :array ();
211+ $ escapeFormulas =isset ($ context [self ::ESCAPE_FORMULAS_KEY ]) ?$ context [self ::ESCAPE_FORMULAS_KEY ] :$ this ->escapeFormulas ;
209212
210213if (!is_array ($ headers )) {
211214throw new InvalidArgumentException (sprintf ('The "%s" context variable must be an array or null, given "%s". ' ,self ::HEADERS_KEY ,gettype ($ headers )));
212215 }
213216
214- return array ($ delimiter ,$ enclosure ,$ escapeChar ,$ keySeparator ,$ headers );
217+ return array ($ delimiter ,$ enclosure ,$ escapeChar ,$ keySeparator ,$ headers, $ escapeFormulas );
215218 }
216219
217220/**