@@ -17,26 +17,27 @@ class Encoder extends EventEmitter implements WritableStreamInterface
17
17
private $ delimiter ;
18
18
private $ enclosure ;
19
19
private $ escapeChar ;
20
+ private $ eol ;
20
21
21
22
/**
22
23
* @param WritableStreamInterface $output
23
24
* @param string $delimiter
24
25
* @param string $enclosure
25
26
* @param string $escapeChar
27
+ * @param string $eol
26
28
* @throws \BadMethodCallException
27
29
*/
28
- public function __construct (WritableStreamInterface $ output , $ delimiter = ', ' , $ enclosure = '" ' , $ escapeChar = '\\' )
30
+ public function __construct (WritableStreamInterface $ output , $ delimiter = ', ' , $ enclosure = '" ' , $ escapeChar = '\\' , $ eol = "\n" )
29
31
{
30
- // @codeCoverageIgnoreStart
31
32
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
33
34
}
34
- // @codeCoverageIgnoreEnd
35
35
36
36
$ this ->output = $ output ;
37
37
$ this ->delimiter = $ delimiter ;
38
38
$ this ->enclosure = $ enclosure ;
39
39
$ this ->escapeChar = $ escapeChar ;
40
+ $ this ->eol = $ eol ;
40
41
41
42
if (!$ output ->isWritable ()) {
42
43
$ this ->close ();
@@ -58,12 +59,14 @@ public function write($data)
58
59
59
60
$ written = false ;
60
61
if (is_array ($ data )) {
61
- // custom escape character requires PHP 5.5.4+ (see constructor check)
62
+ // custom EOL requires PHP 8.1+, custom escape character requires PHP 5.5.4+ (see constructor check)
62
63
// @codeCoverageIgnoreStart
63
- if ($ this -> escapeChar === '\\' ) {
64
- $ written = fputcsv ($ this ->temp , $ data , $ this ->delimiter , $ this ->enclosure );
65
- } else {
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 ) {
66
67
$ written = fputcsv ($ this ->temp , $ data , $ this ->delimiter , $ this ->enclosure , $ this ->escapeChar );
68
+ } else {
69
+ $ written = fputcsv ($ this ->temp , $ data , $ this ->delimiter , $ this ->enclosure );
67
70
}
68
71
// @codeCoverageIgnoreEnd
69
72
}
@@ -77,6 +80,11 @@ public function write($data)
77
80
$ data = stream_get_contents ($ this ->temp );
78
81
ftruncate ($ this ->temp , 0 );
79
82
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
+
80
88
return $ this ->output ->write ($ data );
81
89
}
82
90
0 commit comments