Skip to content

Commit 8fb5cd9

Browse files
committed
Option to customise CSV EOL characters
1 parent 9953e12 commit 8fb5cd9

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
### Unreleased
22

3+
### v2.3.1 (2025-03-12)
4+
5+
* Support option to customize EOL character in CSVWriter
6+
37
### v2.3.0 (2025-03-10)
48

59
* Support option to write CSV column headers unquoted

src/CSV/CSVWriter.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class CSVWriter
3535
protected $options = [
3636
'write_utf8_bom' => FALSE,
3737
'quote_headers' => TRUE,
38+
'eol' => PHP_EOL,
3839
];
3940

4041
/**
@@ -79,7 +80,7 @@ public function write(array $row)
7980
throw MismatchedSchemaException::forSchema($this->expect_schema, $row_schema);
8081
}
8182

82-
\fputcsv($this->resource, $row);
83+
\fputcsv($this->resource, $row, eol: $this->options['eol']);
8384
}
8485

8586
protected function isResourceOpen()
@@ -103,7 +104,7 @@ public function close()
103104
private function writeHeaders(array $keys): void
104105
{
105106
if ($this->options['quote_headers']) {
106-
\fputcsv($this->resource, $keys);
107+
\fputcsv($this->resource, $keys, eol: $this->options['eol']);
107108
} else {
108109
array_walk(
109110
$keys,
@@ -112,7 +113,7 @@ private function writeHeaders(array $keys): void
112113
"Column header `$str` cannot contain comma if headers are not quoted"
113114
)
114115
);
115-
fwrite($this->resource, implode(',', $keys).PHP_EOL);
116+
fwrite($this->resource, implode(',', $keys).$this->options['eol']);
116117
}
117118
}
118119
}

test/unit/CSV/CSVWriterTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,48 @@ public function test_it_throws_if_column_headers_contain_comma_when_unquoted():
194194
fclose($file);
195195
}
196196

197+
public static function providerLineEndings(): array
198+
{
199+
return [
200+
'default' => [
201+
[],
202+
"\"our big\",is\ndata,here\n",
203+
],
204+
'default, with unquoted headers' => [
205+
['quote_headers' => false],
206+
"our big,is\ndata,here\n",
207+
],
208+
'unix' => [
209+
['eol' => "\n"],
210+
"\"our big\",is\ndata,here\n",
211+
],
212+
'mac' => [
213+
['eol' => "\r"],
214+
"\"our big\",is\rdata,here\r",
215+
],
216+
'windows' => [
217+
['eol' => "\r\n"],
218+
"\"our big\",is\r\ndata,here\r\n",
219+
],
220+
'windows, with unquoted headers' => [
221+
['eol' => "\r\n", 'quote_headers' => false],
222+
"our big,is\r\ndata,here\r\n",
223+
],
224+
];
225+
}
226+
227+
#[DataProvider('providerLineEndings')]
228+
public function test_its_line_endings_can_be_configured(array $options, string $expect): void
229+
{
230+
$file = fopen('php://memory', 'w');
231+
$subj = $this->newSubject();
232+
$subj->open($file, $options);
233+
$subj->write(['our big' => 'data', 'is' => 'here']);
234+
rewind($file);
235+
$this->assertSame($expect, stream_get_contents($file));
236+
fclose($file);
237+
}
238+
197239
#[TestWith([true])]
198240
#[TestWith([false])]
199241
public function test_it_optionally_writes_byte_order_mark_at_start_of_file($write_bom)

0 commit comments

Comments
 (0)