Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1e721a3

Browse filesBrowse files
Merge branch '4.4' into 5.3
* 4.4: Use single quote to escape formulas
2 parents 11f922f + 3379d3e commit 1e721a3
Copy full SHA for 1e721a3

File tree

Expand file treeCollapse file tree

2 files changed

+81
-11
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+81
-11
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3636

3737
private const UTF8_BOM = "\xEF\xBB\xBF";
3838

39-
private $formulasStartCharacters = ['=', '-', '+', '@'];
39+
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
40+
4041
private $defaultContext = [
4142
self::DELIMITER_KEY => ',',
4243
self::ENCLOSURE_KEY => '"',
@@ -227,8 +228,8 @@ private function flatten(iterable $array, array &$result, string $keySeparator,
227228
if (is_iterable($value)) {
228229
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator, $escapeFormulas);
229230
} else {
230-
if ($escapeFormulas && \in_array(substr((string) $value, 0, 1), $this->formulasStartCharacters, true)) {
231-
$result[$parentKey.$key] = "\t".$value;
231+
if ($escapeFormulas && \in_array(substr((string) $value, 0, 1), self::FORMULAS_START_CHARACTERS, true)) {
232+
$result[$parentKey.$key] = "'".$value;
232233
} else {
233234
// Ensures an actual value is used when dealing with true and false
234235
$result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value);

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
+77-8Lines changed: 77 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,31 +257,52 @@ public function testEncodeFormulas()
257257

258258
$this->assertSame(<<<'CSV'
259259
0
260-
" =2+3"
260+
'=2+3
261261

262262
CSV
263263
, $this->encoder->encode(['=2+3'], 'csv'));
264264

265265
$this->assertSame(<<<'CSV'
266266
0
267-
" -2+3"
267+
'-2+3
268268

269269
CSV
270270
, $this->encoder->encode(['-2+3'], 'csv'));
271271

272272
$this->assertSame(<<<'CSV'
273273
0
274-
" +2+3"
274+
'+2+3
275275

276276
CSV
277277
, $this->encoder->encode(['+2+3'], 'csv'));
278278

279279
$this->assertSame(<<<'CSV'
280280
0
281-
" @MyDataColumn"
281+
'@MyDataColumn
282282

283283
CSV
284284
, $this->encoder->encode(['@MyDataColumn'], 'csv'));
285+
286+
$this->assertSame(<<<'CSV'
287+
0
288+
"' tab"
289+
290+
CSV
291+
, $this->encoder->encode(["\ttab"], 'csv'));
292+
293+
$this->assertSame(<<<'CSV'
294+
0
295+
"'=1+2"";=1+2"
296+
297+
CSV
298+
, $this->encoder->encode(['=1+2";=1+2'], 'csv'));
299+
300+
$this->assertSame(<<<'CSV'
301+
0
302+
"'=1+2'"" ;,=1+2"
303+
304+
CSV
305+
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv'));
285306
}
286307

287308
public function testDoNotEncodeFormulas()
@@ -313,13 +334,34 @@ public function testDoNotEncodeFormulas()
313334

314335
CSV
315336
, $this->encoder->encode(['@MyDataColumn'], 'csv'));
337+
338+
$this->assertSame(<<<'CSV'
339+
0
340+
" tab"
341+
342+
CSV
343+
, $this->encoder->encode(["\ttab"], 'csv'));
344+
345+
$this->assertSame(<<<'CSV'
346+
0
347+
"=1+2"";=1+2"
348+
349+
CSV
350+
, $this->encoder->encode(['=1+2";=1+2'], 'csv'));
351+
352+
$this->assertSame(<<<'CSV'
353+
0
354+
"=1+2'"" ;,=1+2"
355+
356+
CSV
357+
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv'));
316358
}
317359

318360
public function testEncodeFormulasWithSettingsPassedInContext()
319361
{
320362
$this->assertSame(<<<'CSV'
321363
0
322-
" =2+3"
364+
'=2+3
323365

324366
CSV
325367
, $this->encoder->encode(['=2+3'], 'csv', [
@@ -328,7 +370,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()
328370

329371
$this->assertSame(<<<'CSV'
330372
0
331-
" -2+3"
373+
'-2+3
332374

333375
CSV
334376
, $this->encoder->encode(['-2+3'], 'csv', [
@@ -337,7 +379,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()
337379

338380
$this->assertSame(<<<'CSV'
339381
0
340-
" +2+3"
382+
'+2+3
341383

342384
CSV
343385
, $this->encoder->encode(['+2+3'], 'csv', [
@@ -346,12 +388,39 @@ public function testEncodeFormulasWithSettingsPassedInContext()
346388

347389
$this->assertSame(<<<'CSV'
348390
0
349-
" @MyDataColumn"
391+
'@MyDataColumn
350392

351393
CSV
352394
, $this->encoder->encode(['@MyDataColumn'], 'csv', [
353395
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
354396
]));
397+
398+
$this->assertSame(<<<'CSV'
399+
0
400+
"' tab"
401+
402+
CSV
403+
, $this->encoder->encode(["\ttab"], 'csv', [
404+
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
405+
]));
406+
407+
$this->assertSame(<<<'CSV'
408+
0
409+
"'=1+2"";=1+2"
410+
411+
CSV
412+
, $this->encoder->encode(['=1+2";=1+2'], 'csv', [
413+
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
414+
]));
415+
416+
$this->assertSame(<<<'CSV'
417+
0
418+
"'=1+2'"" ;,=1+2"
419+
420+
CSV
421+
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv', [
422+
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
423+
]));
355424
}
356425

357426
public function testEncodeWithoutHeader()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.