Skip to content

Commit 097411b

Browse files
committed
Support custom EOL character on PHP 8.1+
1 parent 073ca2a commit 097411b

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
php:
13+
- 8.1
1314
- 8.0
1415
- 7.4
1516
- 7.3

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,10 @@ test,1,24
317317
```
318318

319319
The `Encoder` supports the same optional parameters as the underlying
320-
[`fputcsv()`](https://www.php.net/fputcsv) function.
320+
[`fputcsv()`](https://www.php.net/manual/en/function.fputcsv.php) function.
321321
This means that, by default, CSV fields will be delimited by a comma (`,`), will
322-
use a quote enclosure character (`"`) and a backslash escape character (`\`).
322+
use a quote enclosure character (`"`), a backslash escape character (`\`), and
323+
a Unix-style EOL (`\n` or `LF`).
323324
This behavior can be controlled through the optional constructor parameters:
324325

325326
```php
@@ -385,7 +386,7 @@ See also the [CHANGELOG](CHANGELOG.md) for details about version upgrades.
385386
This project aims to run on any platform and thus does not require any PHP
386387
extensions and supports running on legacy PHP 5.3 through current PHP 8+ and
387388
HHVM.
388-
It's *highly recommended to use PHP 7+* for this project.
389+
It's *highly recommended to use the latest supported PHP version PHP 7+* for this project.
389390

390391
## Tests
391392

src/Encoder.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,31 @@ class Encoder extends EventEmitter implements WritableStreamInterface
1717
private $delimiter;
1818
private $enclosure;
1919
private $escapeChar;
20+
private $eol;
2021

2122
/**
2223
* @param WritableStreamInterface $output
2324
* @param string $delimiter
2425
* @param string $enclosure
2526
* @param string $escapeChar
27+
* @param string $eol
2628
* @throws \BadMethodCallException
2729
*/
28-
public function __construct(WritableStreamInterface $output, $delimiter = ',', $enclosure = '"', $escapeChar = '\\')
30+
public function __construct(WritableStreamInterface $output, $delimiter = ',', $enclosure = '"', $escapeChar = '\\', $eol = "\n")
2931
{
30-
// @codeCoverageIgnoreStart
3132
if ($escapeChar !== '\\' && PHP_VERSION_ID < 50504) {
32-
throw new \BadMethodCallException('Custom escape character only supported on PHP 5.5.4+');
33+
throw new \BadMethodCallException('Custom escape character only supported on PHP 5.5.4+'); // @codeCoverageIgnore
34+
}
35+
36+
if ($eol !== "\n" && PHP_VERSION_ID < 80100) {
37+
throw new \BadMethodCallException('Custom EOL character only supported on PHP 8.1+'); // @codeCoverageIgnore
3338
}
34-
// @codeCoverageIgnoreEnd
3539

3640
$this->output = $output;
3741
$this->delimiter = $delimiter;
3842
$this->enclosure = $enclosure;
3943
$this->escapeChar = $escapeChar;
44+
$this->eol = $eol;
4045

4146
if (!$output->isWritable()) {
4247
$this->close();
@@ -58,12 +63,14 @@ public function write($data)
5863

5964
$written = false;
6065
if (is_array($data)) {
61-
// custom escape character requires PHP 5.5.4+ (see constructor check)
66+
// custom escape character requires PHP 5.5.4+, custom EOL requires PHP 8.1+ (see constructor check)
6267
// @codeCoverageIgnoreStart
63-
if ($this->escapeChar === '\\') {
68+
if ($this->escapeChar === '\\' && $this->eol === "\n") {
6469
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure);
65-
} else {
70+
} elseif ($this->eol === "\n") {
6671
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure, $this->escapeChar);
72+
} else {
73+
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure, $this->escapeChar, $this->eol);
6774
}
6875
// @codeCoverageIgnoreEnd
6976
}

tests/EncoderTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public function testWriteArrayWithCustomDelimiter()
4444
$this->encoder->write(array('hello', 'world'));
4545
}
4646

47+
/**
48+
* @requires PHP 8.1
49+
*/
50+
public function testWriteArrayWithCustomEolForWindows()
51+
{
52+
$this->output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
53+
$this->output->expects($this->once())->method('isWritable')->willReturn(true);
54+
$this->encoder = new Encoder($this->output, ',', '"', '\\', "\r\n");
55+
56+
$this->output->expects($this->once())->method('write')->with("hello,world\r\n");
57+
58+
$this->encoder->write(array('hello', 'world'));
59+
}
60+
4761
public function testWriteArrayTwiceWillSeparateWithNewlineAfterEachWrite()
4862
{
4963
$this->output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

0 commit comments

Comments
 (0)