Skip to content

Commit

Permalink
Merge branch '4.4' into 5.3
Browse files Browse the repository at this point in the history
* 4.4:
  Use single quote to escape formulas
  • Loading branch information
nicolas-grekas committed Nov 24, 2021
2 parents 12a52d2 + 1b2ae02 commit bf57083
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 @@ -36,7 +36,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 @@ -227,8 +228,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 @@ -257,31 +257,52 @@ public function testEncodeFormulas()

$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 @@ -313,13 +334,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 @@ -328,7 +370,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()

$this->assertSame(<<<'CSV'
0
" -2+3"
'-2+3
CSV
, $this->encoder->encode(['-2+3'], 'csv', [
Expand All @@ -337,7 +379,7 @@ public function testEncodeFormulasWithSettingsPassedInContext()

$this->assertSame(<<<'CSV'
0
" +2+3"
'+2+3
CSV
, $this->encoder->encode(['+2+3'], 'csv', [
Expand All @@ -346,12 +388,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 bf57083

Please sign in to comment.