@@ -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 }
0 commit comments