@@ -27,6 +27,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
2727 const ESCAPE_CHAR_KEY = 'csv_escape_char ' ;
2828 const KEY_SEPARATOR_KEY = 'csv_key_separator ' ;
2929 const HEADERS_KEY = 'csv_headers ' ;
30+ const ESCAPE_FORMULAS = 'csv_escape_formulas ' ;
3031
3132 private $ delimiter ;
3233 private $ 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
8081 foreach ($ data as &$ 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 {
189191 foreach ($ array as $ key => $ value ) {
190192 if (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 ]) ? $ context [self ::ESCAPE_FORMULAS ] : $ this ->escapeFormulas ;
209212
210213 if (!is_array ($ headers )) {
211214 throw 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 /**
0 commit comments