8000 bug #45391 [Serializer] Ensuring end of line character apply with con… · symfony/symfony@c5be706 · GitHub
[go: up one dir, main page]

Skip to content

Commit c5be706

Browse files
bug #45391 [Serializer] Ensuring end of line character apply with constructor settings in CSV encoder (bizley)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [Serializer] Ensuring end of line character apply with constructor settings in CSV encoder | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | | License | MIT | Doc PR | While providing END_OF_LINE different than `\n` through constructor, context value was still used which ended in error if not present in context. Commits ------- e831aa7 [Serializer] Ensuring end of line character apply with constructor settings in CSV encoder
2 parents 402fdd0 + e831aa7 commit c5be706

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,19 +94,20 @@ public function encode($data, string $format, array $context = [])
9494
unset($value);
9595

9696
$headers = array_merge(array_values($headers), array_diff($this->extractHeaders($data), $headers));
97+
$endOfLine = $context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE];
9798

9899
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
99100
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
100-
if ("\n" !== ($context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE]) && 0 === fseek($handle, -1, \SEEK_CUR)) {
101-
fwrite($handle, $context[self::END_OF_LINE]);
101+
if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
102+
fwrite($handle, $endOfLine);
102103
}
103104
}
104105

105106
$headers = array_fill_keys($headers, '');
106107
foreach ($data as $row) {
107108
fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure, $escapeChar);
108-
if ("\n" !== ($context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE]) && 0 === fseek($handle, -1, \SEEK_CUR)) {
109-
fwrite($handle, $context[self::END_OF_LINE]);
109+
if ("\n" !== $endOfLine && 0 === fseek($handle, -1, \SEEK_CUR)) {
110+
fwrite($handle, $endOfLine);
110111
}
111112
}
112113

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,4 +697,12 @@ public function testEndOfLine()
697697

698698
$this->assertSame("foo,bar\r\nhello,test\r\n", $this->encoder->encode($value, 'csv', [CsvEncoder::END_OF_LINE => "\r\n"]));
699699
}
700+
701+
public function testEndOfLinePassedInConstructor()
702+
{
703+
$value = ['foo' => 'hello', 'bar' => 'test'];
704+
705+
$encoder = new CsvEncoder([CsvEncoder::END_OF_LINE => "\r\n"]);
706+
$this->assertSame("foo,bar\r\nhello,test\r\n", $encoder->encode($value, 'csv'));
707+
}
700708
}

0 commit comments

Comments
 (0)
0