1 | <?php |
||
24 | class Reader implements Iterator, Countable |
||
25 | { |
||
26 | /** @var Streamable The input stream to read from */ |
||
27 | protected $input; |
||
28 | |||
29 | /** @var Dialect The *dialect* of CSV to read */ |
||
30 | protected $dialect; |
||
31 | |||
32 | /** @var Collection The line currently sitting in memory */ |
||
33 | protected $current; |
||
34 | |||
35 | /** @var Collection The header row */ |
||
36 | protected $header; |
||
37 | |||
38 | /** @var int The current line number */ |
||
39 | protected $lineNo; |
||
40 | |||
41 | /** |
||
42 | * Reader constructor. |
||
43 | * |
||
44 | * Although this is the constructor, I don't envision it being used much in userland. I think much more common |
||
45 | * methods of creating readers will be available within CSVelte base class such as CSVelte::fromPath(), |
||
46 | * CSVelte::fromString(), CSVelte::fromSplFileObject, CSVelte::toSplFileObject, CSVelte::toPath(), etc. |
||
47 | * |
||
48 | * @param Streamable $input The source being read from |
||
49 | * @param Dialect $dialect The dialect being read |
||
50 | */ |
||
51 | 11 | public function __construct(Streamable $input, Dialect $dialect = null) |
|
59 | |||
60 | /** |
||
61 | * Get csv data as a two-dimensional array |
||
62 | * |
||
63 | * @return array |
||
64 | */ |
||
65 | 1 | public function toArray() |
|
69 | |||
70 | /** |
||
71 | * Set the CSV dialect |
||
72 | * |
||
73 | * @param Dialect $dialect The *dialect* of CSV to read |
||
74 | * |
||
75 | * @return self |
||
76 | */ |
||
77 | 11 | public function setDialect(Dialect $dialect) |
|
83 | |||
84 | /** |
||
85 | * Get dialect |
||
86 | * |
||
87 | * @return Dialect |
||
88 | */ |
||
89 | 11 | public function getDialect() |
|
93 | |||
94 | /** |
||
95 | * Set input stream |
||
96 | * |
||
97 | * @param Streamable $stream The input stream to read from |
||
98 | * |
||
99 | * @return self |
||
100 | */ |
||
101 | 11 | protected function setInputStream(Streamable $stream) |
|
106 | |||
107 | /** |
||
108 | * Loads next line into memory |
||
109 | * |
||
110 | * Reads from input one character at a time until a newline is reached that isn't within quotes. Once a completed |
||
111 | * line has been loaded, it is assigned to the `$this->current` property. Subsequent calls will continue to load |
||
112 | * successive lines until the end of the input source is reached. |
||
113 | * |
||
114 | * @return self |
||
115 | */ |
||
116 | 11 | protected function loadLine() |
|
129 | |||
130 | /** |
||
131 | * Parse a line of CSV into individual fields |
||
132 | * |
||
133 | * Accepts a line (string) of CSV data that it then splits at the delimiter character. The method is smart, in that |
||
134 | * it knows not to split at delimiters within quotes. Ultimately, fields are placed into a collection and returned. |
||
135 | * |
||
136 | * @param string $line A single line of CSV data to parse into individual fields |
||
137 | * |
||
138 | * @return Collection |
||
139 | */ |
||
140 | 11 | protected function parseLine($line) |
|
156 | |||
157 | /** == BEGIN: SPL implementation methods == */ |
||
158 | |||
159 | /** |
||
160 | * Get current row |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | 10 | public function current() |
|
168 | |||
169 | /** |
||
170 | * Move pointer to beginning of the next line internally and then load the line |
||
171 | * |
||
172 | * @return self |
||
173 | */ |
||
174 | 3 | public function next() |
|
180 | |||
181 | /** |
||
182 | * Get current line number |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | 3 | public function key() |
|
190 | |||
191 | /** |
||
192 | * Have we reached the end of the CSV data? |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 2 | public function valid() |
|
200 | |||
201 | /** |
||
202 | * Rewind to the beginning |
||
203 | * |
||
204 | * Rewinds the internal pointer to the beginning of the CSV data, load first line, and reset line number to 1. Also |
||
205 | * loads the header (if one exists) and uses its values as indexes within rows. |
||
206 | * |
||
207 | * @return self |
||
208 | */ |
||
209 | 11 | public function rewind() |
|
220 | |||
221 | /** |
||
222 | * Get number of lines in the CSV data (not including header) |
||
223 | * |
||
224 | * @return int |
||
225 | */ |
||
226 | 1 | public function count() |
|
230 | |||
231 | /** == END: SPL implementation methods == */ |
||
232 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.