Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 09a5ed5

Browse files
committed
Allows overriding of $escapeFormulas in context
1 parent fc743a1 commit 09a5ed5

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ public function testEncodeCustomHeaders()
173173
$this->assertEquals($csv, $this->encoder->encode($value, 'csv', $context));
174174
}
175175

176-
public function testEncodeFormulaValues()
176+
public function testEncodeFormulas()
177177
{
178178
$this->encoder = new CsvEncoder(',', '"', '\\', '.', true);
179179

@@ -206,7 +206,7 @@ public function testEncodeFormulaValues()
206206
, $this->encoder->encode(array('@MyDataColumn'), 'csv'));
207207
}
208208

209-
public function testDoNotEncodeFormulaValues()
209+
public function testDoNotEncodeFormulas()
210210
{
211211
$this->assertSame(<<<'CSV'
212212
0
@@ -237,6 +237,45 @@ public function testDoNotEncodeFormulaValues()
237237
, $this->encoder->encode(array('@MyDataColumn'), 'csv'));
238238
}
239239

240+
public function testEncodeFormulasWithSettingsPassedInContext()
241+
{
242+
$this->assertSame(<<<'CSV'
243+
0
244+
" =2+3"
245+
246+
CSV
247+
, $this->encoder->encode(array('=2+3'), 'csv', array(
248+
CsvEncoder::ESCAPE_FORMULAS => true,
249+
)));
250+
251+
$this->assertSame(<<<'CSV'
252+
0
253+
" -2+3"
254+
255+
CSV
256+
, $this->encoder->encode(array('-2+3'), 'csv', array(
257+
CsvEncoder::ESCAPE_FORMULAS => true,
258+
)));
259+
260+
$this->assertSame(<<<'CSV'
261+
0
262+
" +2+3"
263+
264+
CSV
265+
, $this->encoder->encode(array('+2+3'), 'csv', array(
266+
CsvEncoder::ESCAPE_FORMULAS => true,
267+
)));
268+
269+
$this->assertSame(<<<'CSV'
270+
0
271+
" @MyDataColumn"
272+
273+
CSV
274+
, $this->encoder->encode(array('@MyDataColumn'), 'csv', array(
275+
CsvEncoder::ESCAPE_FORMULAS => true,
276+
)));
277+
}
278+
240279
public function testSupportsDecoding()
241280
{
242281
$this->assertTrue($this->encoder->supportsDecoding('csv'));

0 commit comments

Comments
 (0)