@@ -17,26 +17,27 @@ 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
3334 }
34- // @codeCoverageIgnoreEnd
3535
3636 $ this ->output = $ output ;
3737 $ this ->delimiter = $ delimiter ;
3838 $ this ->enclosure = $ enclosure ;
3939 $ this ->escapeChar = $ escapeChar ;
40+ $ this ->eol = $ eol ;
4041
4142 if (!$ output ->isWritable ()) {
4243 $ this ->close ();
@@ -58,12 +59,14 @@ public function write($data)
5859
5960 $ written = false ;
6061 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)
6263 // @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 ) {
6667 $ written = fputcsv ($ this ->temp , $ data , $ this ->delimiter , $ this ->enclosure , $ this ->escapeChar );
68+ } else {
69+ $ written = fputcsv ($ this ->temp , $ data , $ this ->delimiter , $ this ->enclosure );
6770 }
6871 // @codeCoverageIgnoreEnd
6972 }
@@ -77,6 +80,11 @@ public function write($data)
7780 $ data = stream_get_contents ($ this ->temp );
7881 ftruncate ($ this ->temp , 0 );
7982
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+
8088 return $ this ->output ->write ($ data );
8189 }
8290
0 commit comments