8000 [Serializer] Add a Custom End Of Line in CSV File · symfony/symfony@20f0367 · GitHub
[go: up one dir, main page]

Skip to content

Commit 20f0367

Browse files
xfifixnicolas-grekas
authored andcommitted
[Serializer] Add a Custom End Of Line in CSV File
1 parent c8b48d8 commit 20f0367

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ CHANGELOG
55
---
66

77
* Add the ability to provide (de)normalization context using metadata (e.g. `@Symfony\Component\Serializer\Annotation\Context`)
8-
* deprecated `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead.
9-
* added normalization formats to `UidNormalizer`
8+
* Deprecate `ArrayDenormalizer::setSerializer()`, call `setDenormalizer()` instead
9+
* Add normalization formats to `UidNormalizer`
10+
* Add `CsvEncoder::END_OF_LINE` context option
1011

1112
5.2.0
1213
-----

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
3131
public const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas';
3232
public const AS_COLLECTION_KEY = 'as_collection';
3333
public const NO_HEADERS_KEY = 'no_headers';
34+
public const END_OF_LINE = 'csv_end_of_line';
3435
public const OUTPUT_UTF8_BOM_KEY = 'output_utf8_bom';
3536

3637
private const UTF8_BOM = "\xEF\xBB\xBF";
@@ -40,6 +41,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
4041
self::DELIMITER_KEY => ',',
4142
self::ENCLOSURE_KEY => '"',
4243
self::ESCAPE_CHAR_KEY => '',
44+
self::END_OF_LINE => "\n",
4345
self::ESCAPE_FORMULAS_KEY => false,
4446
self::HEADERS_KEY => [],
4547
self::KEY_SEPARATOR_KEY => '.',
@@ -94,11 +96,17 @@ public function encode($data, string $format, array $context = [])
9496

9597
if (!($context[self::NO_HEADERS_KEY] ?? $this->defaultContext[self::NO_HEADERS_KEY])) {
9698
fputcsv($handle, $headers, $delimiter, $enclosure, $escapeChar);
99+
if ("\n" !== ($context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE]) && 0 === fseek($handle, -1, \SEEK_CUR)) {
100+
fwrite($handle, $context[self::END_OF_LINE]);
101+
}
97102
}
98103

99104
$headers = array_fill_keys($headers, '');
100105
foreach ($data as $row) {
101106
fputcsv($handle, array_replace($headers, $row), $delimiter, $enclosure, $escapeChar);
107+
if ("\n" !== ($context[self::END_OF_LINE] ?? $this->defaultContext[self::END_OF_LINE]) && 0 === fseek($handle, -1, \SEEK_CUR)) {
108+
fwrite($handle, $context[self::END_OF_LINE]);
109+
}
102110
}
103111

104112
rewind($handle);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,11 @@ public function testBOMIsStripped()
621621
$this->encoder->decode($csv, 'csv', [CsvEncoder::AS_COLLECTION_KEY => false])
622622
);
623623
}
624+
625+
public function testEndOfLine()
626+
{
627+
$value = ['foo' => 'hello', 'bar' => 'test'];
628+
629+
$this->assertSame("foo,bar\r\nhello,test\r\n", $this->encoder->encode($value, 'csv', [CsvEncoder::END_OF_LINE => "\r\n"]));
630+
}
624631
}

0 commit comments

Comments
 (0)
0