Skip to content

Commit

Permalink
Use single quote to escape formulas
Browse files Browse the repository at this point in the history
  • Loading branch information
jderusse committed Nov 15, 2021
1 parent 5cb0dc3 commit d834b28
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 11 deletions.
7 changes: 4 additions & 3 deletions Encoder/CsvEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class CsvEncoder implements EncoderInterface, DecoderInterface

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

private $formulasStartCharacters = ['=', '-', '+', '@'];
private const FORMULAS_START_CHARACTERS = ['=', '-', '+', '@', "\t", "\r"];

private $defaultContext = [
self::DELIMITER_KEY => ',',
self::ENCLOSURE_KEY => '"',
Expand Down Expand Up @@ -238,8 +239,8 @@ private function flatten(iterable $array, array &$result, string $keySeparator,
if (is_iterable($value)) {
$this->flatten($value, $result, $keySeparator, $parentKey.$key.$keySeparator, $escapeFormulas);
} else {
if ($escapeFormulas && \in_array(substr((string) $value, 0, 1), $this->formulasStartCharacters, true)) {
$result[$parentKey.$key] = "\t".$value;
if ($escapeFormulas && \in_array(substr((string) $value, 0, 1), self::FORMULAS_START_CHARACTERS, true)) {
$result[$parentKey.$key] = "'".$value;
} else {
// Ensures an actual value is used when dealing with true and false
$result[$parentKey.$key] = false === $value ? 0 : (true === $value ? 1 : $value);
Expand Down
85 changes: 77 additions & 8 deletions Tests/Encoder/CsvEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,31 +285,52 @@ private function doTestEncodeFormulas(bool $legacy = false)

$this->assertSame(<<<'CSV'
0
" =2+3"
'=2+3

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

$this->assertSame(<<<'CSV'
0
" -2+3"
'-2+3

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

$this->assertSame(<<<'CSV'
0
" +2+3"
'+2+3

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

$this->assertSame(<<<'CSV'
0
" @MyDataColumn"
'@MyDataColumn

CSV
, $this->encoder->encode(['@MyDataColumn'], 'csv'));

$this->assertSame(<<<'CSV'
0
"' tab"

CSV
, $this->encoder->encode(["\ttab"], 'csv'));

$this->assertSame(<<<'CSV'
0
"'=1+2"";=1+2"

CSV
, $this->encoder->encode(['=1+2";=1+2'], 'csv'));

$this->assertSame(<<<'CSV'
0
"'=1+2'"" ;,=1+2"

CSV
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv'));
}

public function testDoNotEncodeFormulas()
Expand Down Expand Up @@ -341,13 +362,34 @@ public function testDoNotEncodeFormulas()

CSV
, $this->encoder->encode(['@MyDataColumn'], 'csv'));

$this->assertSame(<<<'CSV'
0
" tab"

CSV
, $this->encoder->encode(["\ttab"], 'csv'));

$this->assertSame(<<<'CSV'
0
"=1+2"";=1+2"

CSV
, $this->encoder->encode(['=1+2";=1+2'], 'csv'));

$this->assertSame(<<<'CSV'
0
"=1+2'"" ;,=1+2"

CSV
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv'));
}

public function testEncodeFormulasWithSettingsPassedInContext()
{
$this->assertSame(<<<'CSV'
0
" =2+3"
'=2+3

CSV
, $this->encoder->encode(['=2+3'], 'csv', [
Expand All @@ -356,7 +398,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()

$this->assertSame(<<<'CSV'
0
" -2+3"
'-2+3

CSV
, $this->encoder->encode(['-2+3'], 'csv', [
Expand All @@ -365,7 +407,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()

$this->assertSame(<<<'CSV'
0
" +2+3"
'+2+3

CSV
, $this->encoder->encode(['+2+3'], 'csv', [
Expand All @@ -374,12 +416,39 @@ public function testEncodeFormulasWithSettingsPassedInContext()

$this->assertSame(<<<'CSV'
0
" @MyDataColumn"
'@MyDataColumn

CSV
, $this->encoder->encode(['@MyDataColumn'], 'csv', [
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
]));

$this->assertSame(<<<'CSV'
0
"' tab"

CSV
, $this->encoder->encode(["\ttab"], 'csv', [
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
]));

$this->assertSame(<<<'CSV'
0
"'=1+2"";=1+2"

CSV
, $this->encoder->encode(['=1+2";=1+2'], 'csv', [
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
]));

$this->assertSame(<<<'CSV'
0
"'=1+2'"" ;,=1+2"

CSV
, $this->encoder->encode(['=1+2\'" ;,=1+2'], 'csv', [
CsvEncoder::ESCAPE_FORMULAS_KEY => true,
]));
}

public function testEncodeWithoutHeader()
Expand Down

0 comments on commit d834b28

Please sign in to comment.