@@ -31,6 +31,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
31
31
public const ESCAPE_FORMULAS_KEY = 'csv_escape_formulas ' ;
32
32
public const AS_COLLECTION_KEY = 'as_collection ' ;
33
33
public const NO_HEADERS_KEY = 'no_headers ' ;
34
+ public const END_OF_LINE = 'csv_end_of_line ' ;
34
35
public const OUTPUT_UTF8_BOM_KEY = 'output_utf8_bom ' ;
35
36
36
37
private const UTF8_BOM = "\xEF\xBB\xBF" ;
@@ -40,6 +41,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
40
41
self ::DELIMITER_KEY => ', ' ,
41
42
self ::ENCLOSURE_KEY => '" ' ,
42
43
self ::ESCAPE_CHAR_KEY => '' ,
44
+ self ::END_OF_LINE => "\n" ,
43
45
self ::ESCAPE_FORMULAS_KEY => false ,
44
46
self ::HEADERS_KEY => [],
45
47
self ::KEY_SEPARATOR_KEY => '. ' ,
@@ -94,11 +96,17 @@ public function encode($data, string $format, array $context = [])
94
96
95
97
if (!($ context [self ::NO_HEADERS_KEY ] ?? $ this ->defaultContext [self ::NO_HEADERS_KEY ])) {
96
98
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
+ }
97
102
}
98
103
99
104
$ headers = array_fill_keys ($ headers , '' );
100
105
foreach ($ data as $ row ) {
101
106
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
+ }
102
110
}
103
111
104
112
rewind ($ handle );
0 commit comments