Skip to content

Commit b1cf3bd

Browse files
author
Peter Mead
committed
[Serializer] CsvEncoder to escape values starting with line feed when escape formulas is enabled
1 parent 87c43fb commit b1cf3bd

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Encoder/CsvEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3939

4040
private const UTF8_BOM = "\xEF\xBB\xBF";
4141

42-
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];
42+
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r", "\n"];
4343

4444
private array $defaultContext = [
4545
self::DELIMITER_KEY => ',',

Tests/Encoder/CsvEncoderTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,15 @@ public function testEncodeFormulas()
310310
$this->encoder->encode(["\ttab"], 'csv')
311311
);
312312

313+
$this->assertSame(<<<'CSV'
314+
0
315+
"'
316+
line feed"
317+
318+
CSV,
319+
$this->encoder->encode(["\nline feed"], 'csv')
320+
);
321+
313322
$this->assertSame(<<<'CSV'
314323
0
315324
"'=1+2"";=1+2"
@@ -438,6 +447,17 @@ public function testEncodeFormulasWithSettingsPassedInContext()
438447
])
439448
);
440449

450+
$this->assertSame(<<<'CSV'
451+
0
452+
"'
453+
line feed"
454+
455+
CSV,
456+
$this->encoder->encode(["\nline feed"], 'csv', [
457+
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
458+
])
459+
);
460+
441461
$this->assertSame(<<<'CSV'
442462
0
443463
"'=1+2"";=1+2"

0 commit comments

Comments
 (0)