@@ -14,27 +14,6 @@ class Reader implements \Iterator
1414 */
1515 private $ headers ;
1616
17- /**
18- * The field delimiter (one character only).
19- *
20- * @var string
21- */
22- private $ delimiter ;
23-
24- /**
25- * The field enclosure character (one character only).
26- *
27- * @var string
28- */
29- private $ enclosure ;
30-
31- /**
32- * The escape character (one character only).
33- *
34- * @var string
35- */
36- private $ escapeChar ;
37-
3817 /**
3918 * File pointer to the csv file.
4019 *
@@ -56,45 +35,32 @@ class Reader implements \Iterator
5635 */
5736 private $ current = null ;
5837
38+ /**
39+ * @var CsvOptions
40+ */
41+ private $ csvOptions ;
42+
5943 /**
6044 * Create a new Reader instance.
6145 *
62- * @param string $file The full path to the csv file.
63- * @param array $headers The column headers. If null, the headers will be derived from the first line in the
64- * file.
65- * @param string $delimiter The field delimiter (one character only).
66- * @param string $enclosure The field enclosure character (one character only).
67- * @param string $escapeChar The escape character (one character only).
46+ * @param string $file The full path to the csv file.
47+ * @param array $headers The column headers. If null, the headers will be derived from the first line in
48+ * the file.
49+ * @param CsvOptions $csvOptions Options for the csv file.
6850 *
6951 * @throws \InvalidArgumentException Thrown if $file is not readable.
70- * @throws \InvalidArgumentException Thrown if $delimiter is a single character string.
71- * @throws \InvalidArgumentException Thrown if $enclosure is a single character string.
72- * @throws \InvalidArgumentException Thrown if $escapeChar is a single character string.
7352 */
74- public function __construct ($ file , array $ headers = null , $ delimiter = ' , ' , $ enclosure = ' " ' , $ escapeChar = '\\' )
53+ public function __construct ($ file , array $ headers = null , CsvOptions $ csvOptions = null )
7554 {
7655 if (!is_readable ((string )$ file )) {
7756 throw new \InvalidArgumentException (
7857 '$file must be a string containing a full path to a readable delimited file '
7958 );
8059 }
8160
82- if (strlen ($ delimiter ) !== 1 ) {
83- throw new \InvalidArgumentException ('$delimiter must be a single character string ' );
84- }
85-
86- if (strlen ($ enclosure ) !== 1 ) {
87- throw new \InvalidArgumentException ('$enclosure must be a single character string ' );
88- }
89-
90- if (strlen ($ escapeChar ) !== 1 ) {
91- throw new \InvalidArgumentException ('$escapeChar must be a single character string ' );
92- }
61+ $ this ->csvOptions = $ csvOptions ?? new CsvOptions ();
9362
9463 $ this ->headers = $ headers ;
95- $ this ->delimiter = $ delimiter ;
96- $ this ->enclosure = $ enclosure ;
97- $ this ->escapeChar = $ escapeChar ;
9864 $ this ->handle = fopen ((string )$ file , 'r ' );
9965 }
10066
@@ -140,7 +106,13 @@ public function next()
140106 */
141107 private function readLine ()
142108 {
143- $ raw = fgetcsv ($ this ->handle , 0 , $ this ->delimiter , $ this ->enclosure , $ this ->escapeChar );
109+ $ raw = fgetcsv (
110+ $ this ->handle ,
111+ 0 ,
112+ $ this ->csvOptions ->getDelimiter (),
113+ $ this ->csvOptions ->getEnclosure (),
114+ $ this ->csvOptions ->getEscapeChar ()
115+ );
144116 if (empty ($ raw )) {
145117 throw new \Exception ('Empty line read ' );
146118 }
0 commit comments