Skip to content

Commit 6f1c98a

Browse files
committed
Support custom EOL character on all PHP versions
1 parent 097411b commit 6f1c98a

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

src/Encoder.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ public function __construct(WritableStreamInterface $output, $delimiter = ',', $
3333
throw new \BadMethodCallException('Custom escape character only supported on PHP 5.5.4+'); // @codeCoverageIgnore
3434
}
3535

36-
if ($eol !== "\n" && PHP_VERSION_ID < 80100) {
37-
throw new \BadMethodCallException('Custom EOL character only supported on PHP 8.1+'); // @codeCoverageIgnore
38-
}
39-
4036
$this->output = $output;
4137
$this->delimiter = $delimiter;
4238
$this->enclosure = $enclosure;
@@ -63,14 +59,14 @@ public function write($data)
6359

6460
$written = false;
6561
if (is_array($data)) {
66-
// custom escape character requires PHP 5.5.4+, custom EOL requires PHP 8.1+ (see constructor check)
62+
// custom EOL requires PHP 8.1+, custom escape character requires PHP 5.5.4+ (see constructor check)
6763
// @codeCoverageIgnoreStart
68-
if ($this->escapeChar === '\\' && $this->eol === "\n") {
69-
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure);
70-
} elseif ($this->eol === "\n") {
64+
if (\PHP_VERSION_ID >= 80100) {
65+
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure, $this->escapeChar, $this->eol);
66+
} elseif (\PHP_VERSION_ID >= 50504) {
7167
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure, $this->escapeChar);
7268
} else {
73-
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure, $this->escapeChar, $this->eol);
69+
$written = fputcsv($this->temp, $data, $this->delimiter, $this->enclosure);
7470
}
7571
// @codeCoverageIgnoreEnd
7672
}
@@ -84,6 +80,11 @@ public function write($data)
8480
$data = stream_get_contents($this->temp);
8581
ftruncate($this->temp, 0);
8682

83+
// manually replace custom EOL on PHP < 8.1
84+
if (\PHP_VERSION_ID < 80100 && $this->eol !== "\n") {
85+
$data = \substr($data, 0, -1) . $this->eol;
86+
}
87+
8788
return $this->output->write($data);
8889
}
8990

tests/EncoderTest.php

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

47-
/**
48-
* @requires PHP 8.1
49-
*/
5047
public function testWriteArrayWithCustomEolForWindows()
5148
{
5249
$this->output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

0 commit comments

Comments
 (0)