Skip to content

Commit

Permalink
bug #34019 [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used …
Browse files Browse the repository at this point in the history
…in constructor (Dario Savella)

This PR was squashed before being merged into the 4.3 branch.

Discussion
----------

[Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used in constructor

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| License       | MIT

My first pull request...
The following code:
```
$data = <<<EOD
a,b
c,d
EOD;
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY=>true]);
var_dump($encoder->decode($data,'csv'));
```
produces:
```
array(2) {
  'a' =>
  string(1) "c"
  'b' =>
  string(1) "d"
}
```
instead of the expected:
```
array(2) {
  [0] =>
  array(2) {
    [0] =>
    string(1) "a"
    [1] =>
    string(1) "b"
  }
  [1] =>
  array(2) {
    [0] =>
    string(1) "c"
    [1] =>
    string(1) "d"
  }
}
```

Commits
-------

a0430f6 [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used in constructor
  • Loading branch information
nicolas-grekas committed Nov 28, 2019
2 parents ab5e7fa + a0430f6 commit 50db43f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -40,6 +40,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
self::HEADERS_KEY => [],
self::KEY_SEPARATOR_KEY => '.',
self::NO_HEADERS_KEY => false,
self::AS_COLLECTION_KEY => false,
];

/**
Expand Down Expand Up @@ -101,7 +102,7 @@ public function encode($data, $format, array $context = [])

$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));

if (!($context[self::NO_HEADERS_KEY] ?? false)) {
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
}

Expand Down Expand Up @@ -147,7 +148,7 @@ public function decode($data, $format, array $context = [])
if (null === $headers) {
$nbHeaders = $nbCols;

if ($context[self::NO_HEADERS_KEY] ?? false) {
if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
for ($i = 0; $i < $nbCols; ++$i) {
$headers[] = [$i];
}
Expand Down Expand Up @@ -187,7 +188,7 @@ public function decode($data, $format, array $context = [])
}
fclose($handle);

if ($context[self::AS_COLLECTION_KEY] ?? false) {
if ($context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY]) {
return $result;
}

Expand Down
53 changes: 53 additions & 0 deletions src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php
Expand Up @@ -202,6 +202,24 @@ public function testEncodeCustomSettingsPassedInContext()
]));
}

public function testEncodeCustomSettingsPassedInConstructor()
{
$encoder = new CsvEncoder([
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
]);
$value = ['a' => 'he\'llo', 'c' => ['d' => 'foo']];

$this->assertSame(<<<'CSV'
a;c-d
'he''llo';foo
CSV
, $encoder->encode($value, 'csv'));
}

public function testEncodeEmptyArray()
{
$this->assertEquals("\n\n", $this->encoder->encode([], 'csv'));
Expand Down Expand Up @@ -373,6 +391,15 @@ public function testEncodeWithoutHeader()
, $this->encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
$this->assertSame(<<<'CSV'
a,b
c,d
CSV
, $encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
}

public function testSupportsDecoding()
Expand Down Expand Up @@ -524,6 +551,23 @@ public function testDecodeCustomSettingsPassedInContext()
]));
}

public function testDecodeCustomSettingsPassedInConstructor()
{
$encoder = new CsvEncoder([
CsvEncoder::DELIMITER_KEY => ';',
CsvEncoder::ENCLOSURE_KEY => "'",
CsvEncoder::ESCAPE_CHAR_KEY => '|',
CsvEncoder::KEY_SEPARATOR_KEY => '-',
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
]);
$expected = [['a' => 'hell\'o', 'bar' => ['baz' => 'b']]];
$this->assertEquals($expected, $encoder->decode(<<<'CSV'
a;bar-baz
'hell''o';b;c
CSV
, 'csv'));
}

public function testDecodeMalformedCollection()
{
$expected = [
Expand Down Expand Up @@ -553,6 +597,15 @@ public function testDecodeWithoutHeader()
a,b
c,d
CSV
, 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
]));
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
$this->assertEquals([['a', 'b'], ['c', 'd']], $encoder->decode(<<<'CSV'
a,b
c,d
CSV
, 'csv', [
CsvEncoder::NO_HEADERS_KEY => true,
Expand Down

0 comments on commit 50db43f

Please sign in to comment.