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 | 13 | 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 | 13 | public function setDialect(Dialect $dialect) |
|
83 | |||
84 | /** |
||
85 | * Get dialect |
||
86 | * |
||
87 | * @return Dialect |
||
88 | */ |
||
89 | 13 | public function getDialect() |
|
93 | |||
94 | /** |
||
95 | * Fetch a single row |
||
96 | * |
||
97 | * Fetch the next row from the CSV data. If no more data available, returns false. |
||
98 | * |
||
99 | * @return array|false |
||
100 | */ |
||
101 | 2 | public function fetchRow() |
|
110 | |||
111 | /** |
||
112 | * Set input stream |
||
113 | * |
||
114 | * @param Streamable $stream The input stream to read from |
||
115 | * |
||
116 | * @return self |
||
117 | */ |
||
118 | 13 | protected function setInputStream(Streamable $stream) |
|
123 | |||
124 | /** |
||
125 | * Loads next line into memory |
||
126 | * |
||
127 | * Reads from input one character at a time until a newline is reached that isn't within quotes. Once a completed |
||
128 | * line has been loaded, it is assigned to the `$this->current` property. Subsequent calls will continue to load |
||
129 | * successive lines until the end of the input source is reached. |
||
130 | * |
||
131 | * @return self |
||
132 | */ |
||
133 | 13 | protected function loadLine() |
|
146 | |||
147 | /** |
||
148 | * Parse a line of CSV into individual fields |
||
149 | * |
||
150 | * Accepts a line (string) of CSV data that it then splits at the delimiter character. The method is smart, in that |
||
151 | * it knows not to split at delimiters within quotes. Ultimately, fields are placed into a collection and returned. |
||
152 | * |
||
153 | * @param string $line A single line of CSV data to parse into individual fields |
||
154 | * |
||
155 | * @return Collection |
||
156 | */ |
||
157 | 13 | protected function parseLine($line) |
|
173 | |||
174 | /** == BEGIN: SPL implementation methods == */ |
||
175 | |||
176 | /** |
||
177 | * Get current row |
||
178 | * |
||
179 | * @return array |
||
180 | */ |
||
181 | 12 | public function current() |
|
185 | |||
186 | /** |
||
187 | * Move pointer to beginning of the next line internally and then load the line |
||
188 | * |
||
189 | * @return self |
||
190 | */ |
||
191 | 4 | public function next() |
|
197 | |||
198 | /** |
||
199 | * Get current line number |
||
200 | * |
||
201 | * @return int |
||
202 | */ |
||
203 | 4 | public function key() |
|
207 | |||
208 | /** |
||
209 | * Have we reached the end of the CSV data? |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | 4 | public function valid() |
|
217 | |||
218 | /** |
||
219 | * Rewind to the beginning |
||
220 | * |
||
221 | * Rewinds the internal pointer to the beginning of the CSV data, load first line, and reset line number to 1. Also |
||
222 | * loads the header (if one exists) and uses its values as indexes within rows. |
||
223 | * |
||
224 | * @return self |
||
225 | */ |
||
226 | 13 | public function rewind() |
|
237 | |||
238 | /** |
||
239 | * Get number of lines in the CSV data (not including header) |
||
240 | * |
||
241 | * @return int |
||
242 | */ |
||
243 | 1 | public function count() |
|
247 | |||
248 | /** == END: SPL implementation methods == */ |
||
249 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.