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 | /** @var bool Determines whether we've reached the end of the data */ |
||
42 | protected $valid; |
||
43 | |||
44 | /** |
||
45 | * Reader constructor. |
||
46 | * |
||
47 | * Although this is the constructor, I don't envision it being used much in userland. I think much more common |
||
48 | * methods of creating readers will be available within CSVelte base class such as CSVelte::fromPath(), |
||
49 | * CSVelte::fromString(), CSVelte::fromSplFileObject, CSVelte::toSplFileObject, CSVelte::toPath(), etc. |
||
50 | * |
||
51 | * @param Streamable $input The source being read from |
||
52 | * @param Dialect $dialect The dialect being read |
||
53 | */ |
||
54 | 18 | public function __construct(Streamable $input, Dialect $dialect = null) |
|
62 | |||
63 | /** |
||
64 | * Get csv data as a two-dimensional array |
||
65 | * |
||
66 | * @return array |
||
67 | */ |
||
68 | 6 | public function toArray() |
|
72 | |||
73 | /** |
||
74 | * Set the CSV dialect |
||
75 | * |
||
76 | * @param Dialect $dialect The *dialect* of CSV to read |
||
77 | * |
||
78 | * @return self |
||
79 | */ |
||
80 | 18 | public function setDialect(Dialect $dialect) |
|
86 | |||
87 | /** |
||
88 | * Get dialect |
||
89 | * |
||
90 | * @return Dialect |
||
91 | */ |
||
92 | 18 | public function getDialect() |
|
96 | |||
97 | /** |
||
98 | * Get a single row |
||
99 | * |
||
100 | * Get the next row from the CSV data. If no more data available, returns false. |
||
101 | * |
||
102 | * @param int $offset An optional row offset (negative offsets not yet supported) |
||
103 | * |
||
104 | * @return array|false |
||
105 | */ |
||
106 | 4 | public function getRow($offset = null) |
|
126 | |||
127 | /** |
||
128 | * Get the column at given $index |
||
129 | * |
||
130 | * @param int|string $index The column index |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | 1 | public function getColumn($index) |
|
140 | |||
141 | /** |
||
142 | * Set input stream |
||
143 | * |
||
144 | * @param Streamable $stream The input stream to read from |
||
145 | * |
||
146 | * @return self |
||
147 | */ |
||
148 | 18 | protected function setInputStream(Streamable $stream) |
|
153 | |||
154 | /** |
||
155 | * Loads next line into memory |
||
156 | * |
||
157 | * Reads from input one character at a time until a newline is reached that isn't within quotes. Once a completed |
||
158 | * line has been loaded, it is assigned to the `$this->current` property. Subsequent calls will continue to load |
||
159 | * successive lines until the end of the input source is reached. |
||
160 | * |
||
161 | * @return self |
||
162 | */ |
||
163 | 18 | protected function loadLine() |
|
176 | |||
177 | /** |
||
178 | * Parse a line of CSV into individual fields |
||
179 | * |
||
180 | * Accepts a line (string) of CSV data that it then splits at the delimiter character. The method is smart, in that |
||
181 | * it knows not to split at delimiters within quotes. Ultimately, fields are placed into a collection and returned. |
||
182 | * |
||
183 | * @param string $line A single line of CSV data to parse into individual fields |
||
184 | * |
||
185 | * @return Collection |
||
186 | */ |
||
187 | 18 | protected function parseLine($line) |
|
206 | |||
207 | /** == BEGIN: SPL implementation methods == */ |
||
208 | |||
209 | /** |
||
210 | * Get current row |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | 17 | public function current() |
|
218 | |||
219 | /** |
||
220 | * Move pointer to beginning of the next line internally and then load the line |
||
221 | * |
||
222 | * @return self |
||
223 | */ |
||
224 | 10 | public function next() |
|
230 | |||
231 | /** |
||
232 | * Get current line number |
||
233 | * |
||
234 | * @return int |
||
235 | */ |
||
236 | 10 | public function key() |
|
240 | |||
241 | /** |
||
242 | * Have we reached the end of the CSV data? |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | 9 | public function valid() |
|
256 | |||
257 | /** |
||
258 | * Rewind to the beginning |
||
259 | * |
||
260 | * Rewinds the internal pointer to the beginning of the CSV data, load first line, and reset line number to 1. Also |
||
261 | * loads the header (if one exists) and uses its values as indexes within rows. |
||
262 | * |
||
263 | * @return self |
||
264 | */ |
||
265 | 18 | public function rewind() |
|
277 | |||
278 | /** |
||
279 | * Get number of lines in the CSV data (not including header) |
||
280 | * |
||
281 | * @return int |
||
282 | */ |
||
283 | 1 | public function count() |
|
287 | |||
288 | /** == END: SPL implementation methods == */ |
||
289 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.