8000 bug #34019 [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used … · symfony/symfony@50db43f · GitHub
[go: up one dir, main page]

Skip to content

Commit 50db43f

Browse files
bug #34019 [Serializer] CsvEncoder::NO_HEADERS_KEY ignored when used 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
2 parents ab5e7fa + a0430f6 commit 50db43f

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
4040
self::HEADERS_KEY => [],
4141
self::KEY_SEPARATOR_KEY => '.',
4242
self::NO_HEADERS_KEY => false,
43+
self::AS_COLLECTION_KEY => false,
4344
];
4445

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

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

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

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

150-
if ($context[self::NO_HEADERS_KEY] ?? false) {
151+
if ($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY]) {
151152
for ($i = 0; $i < $nbCols; ++$i) {
152153
$headers[] = [$i];
153154
}
@@ -187,7 +188,7 @@ public function decode($data, $format, array $context = [])
187188
}
188189
fclose($handle);
189190

190-
if ($context[self::AS_COLLECTION_KEY] ?? false) {
191+
if ($context[self::AS_COLLECTION_KEY] ?? $this->defaultContext[self::AS_COLLECTION_KEY]) {
191192
return $result;
192193
}
193194

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,24 @@ public function testEncodeCustomSettingsPassedInContext()
202202
]));
203203
}
204204

205+
public function testEncodeCustomSettingsPassedInConstructor()
206+
{
207+
$encoder = new CsvEncoder([
208+
CsvEncoder::DELIMITER_KEY => ';',
209+
CsvEncoder::ENCLOSURE_KEY => "'",
210+
CsvEncoder::ESCAPE_CHAR_KEY => '|',
211+
CsvEncoder::KEY_SEPARATOR_KEY => '-',
212+
]);
213+
$value = ['a' => 'he\'llo', 'c' => ['d' => 'foo']];
214+
215+
$this->assertSame(<<<'CSV'
216+
a;c-d
217+
'he''llo';foo
218+
219+
CSV
220+
, $encoder->encode($value, 'csv'));
221+
}
222+
205223
public function testEncodeEmptyArray()
206224
{
207225
$this->assertEquals("\n\n", $this->encoder->encode([], 'csv'));
@@ -373,6 +391,15 @@ public function testEncodeWithoutHeader()
373391
, $this->encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
374392
CsvEncoder::NO_HEADERS_KEY => true,
375393
]));
394+
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
395+
$this->assertSame(<<<'CSV'
396+
a,b
397+
c,d
398+
399+
CSV
400+
, $encoder->encode([['a', 'b'], ['c', 'd']], 'csv', [
401+
CsvEncoder::NO_HEADERS_KEY => true,
402+
]));
376403
}
377404

378405
public function testSupportsDecoding()
@@ -524,6 +551,23 @@ public function testDecodeCustomSettingsPassedInContext()
524551
]));
525552
}
526553

554+
public function testDecodeCustomSettingsPassedInConstructor()
555+
{
556+
$encoder = new CsvEncoder([
557+
CsvEncoder::DELIMITER_KEY => ';',
558+
CsvEncoder::ENCLOSURE_KEY => "'",
559+
CsvEncoder::ESCAPE_CHAR_KEY => '|',
560+
CsvEncoder::KEY_SEPARATOR_KEY => '-',
561+
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
562+
]);
563+
$expected = [['a' => 'hell\'o', 'bar' => ['baz' => 'b']]];
564+
$this->assertEquals($expected, $encoder->decode(<<<'CSV'
565+
a;bar-baz
566+
'hell''o';b;c
567+
CSV
568+
, 'csv'));
569+
}
570+
527571
public function testDecodeMalformedCollection()
528572
{
529573
$expected = [
@@ -553,6 +597,15 @@ public function testDecodeWithoutHeader()
553597
a,b
554598
c,d
555599

600+
CSV
601+
, 'csv', [
602+
CsvEncoder::NO_HEADERS_KEY => true,
603+
]));
604+
$encoder = new CsvEncoder([CsvEncoder::NO_HEADERS_KEY => true]);
605+
$this->assertEquals([['a', 'b'], ['c', 'd']], $encoder->decode(<<<'CSV'
606+
a,b
607+
c,d
608+
556609
CSV
557610
, 'csv', [
558611
CsvEncoder::NO_HEADERS_KEY => true,

0 commit comments

Comments
 (0)
0