1 | <?php |
||
12 | class CsvReader implements CountableReader, \SeekableIterator |
||
13 | { |
||
14 | const DUPLICATE_HEADERS_INCREMENT = 1; |
||
15 | const DUPLICATE_HEADERS_MERGE = 2; |
||
16 | |||
17 | /** |
||
18 | * Number of the row that contains the column names |
||
19 | * |
||
20 | * @var integer |
||
21 | */ |
||
22 | protected $headerRowNumber; |
||
23 | |||
24 | /** |
||
25 | * CSV file |
||
26 | * |
||
27 | * @var \SplFileObject |
||
28 | */ |
||
29 | protected $file; |
||
30 | |||
31 | /** |
||
32 | * Column headers as read from the CSV file |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $columnHeaders = []; |
||
37 | |||
38 | /** |
||
39 | * Number of column headers, stored and re-used for performance |
||
40 | * |
||
41 | * In case of duplicate headers, this is always the number of unmerged headers. |
||
42 | * |
||
43 | * @var integer |
||
44 | */ |
||
45 | protected $headersCount; |
||
46 | |||
47 | /** |
||
48 | * Total number of rows in the CSV file |
||
49 | * |
||
50 | * @var integer |
||
51 | */ |
||
52 | protected $count; |
||
53 | |||
54 | /** |
||
55 | * Faulty CSV rows |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | protected $errors = []; |
||
60 | |||
61 | /** |
||
62 | * Strict parsing - skip any lines mismatching header length |
||
63 | * |
||
64 | * @var boolean |
||
65 | */ |
||
66 | protected $strict = true; |
||
67 | |||
68 | /** |
||
69 | * How to handle duplicate headers |
||
70 | * |
||
71 | * @var integer |
||
72 | */ |
||
73 | protected $duplicateHeadersFlag; |
||
74 | |||
75 | /** |
||
76 | * @param \SplFileObject $file |
||
77 | * @param string $delimiter |
||
78 | * @param string $enclosure |
||
79 | * @param string $escape |
||
80 | */ |
||
81 | 19 | public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') |
|
98 | |||
99 | /** |
||
100 | * Return the current row as an array |
||
101 | * |
||
102 | * If a header row has been set, an associative array will be returned |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | 11 | public function current() |
|
145 | |||
146 | /** |
||
147 | * Get column headers |
||
148 | * |
||
149 | * @return array |
||
150 | */ |
||
151 | 4 | public function getColumnHeaders() |
|
155 | |||
156 | /** |
||
157 | * Set column headers |
||
158 | * |
||
159 | * @param array $columnHeaders |
||
160 | */ |
||
161 | 15 | public function setColumnHeaders(array $columnHeaders) |
|
166 | |||
167 | /** |
||
168 | * Set header row number |
||
169 | * |
||
170 | * @param integer $rowNumber Number of the row that contains column header names |
||
171 | * @param integer $duplicates How to handle duplicates (optional). One of: |
||
172 | * - CsvReader::DUPLICATE_HEADERS_INCREMENT; |
||
173 | * increments duplicates (dup, dup1, dup2 etc.) |
||
174 | * - CsvReader::DUPLICATE_HEADERS_MERGE; merges |
||
175 | * values for duplicate headers into an array |
||
176 | * (dup => [value1, value2, value3]) |
||
177 | * |
||
178 | * @throws DuplicateHeadersException If duplicate headers are encountered |
||
179 | * and no duplicate handling has been |
||
180 | * specified |
||
181 | */ |
||
182 | 11 | public function setHeaderRowNumber($rowNumber, $duplicates = null) |
|
190 | |||
191 | /** |
||
192 | * Rewind the file pointer |
||
193 | * |
||
194 | * If a header row has been set, the pointer is set just below the header |
||
195 | * row. That way, when you iterate over the rows, that header row is |
||
196 | * skipped. |
||
197 | */ |
||
198 | 17 | public function rewind() |
|
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | */ |
||
209 | 9 | public function count() |
|
221 | |||
222 | /** |
||
223 | * {@inheritdoc} |
||
224 | */ |
||
225 | 15 | public function next() |
|
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | 15 | public function valid() |
|
237 | |||
238 | /** |
||
239 | * {@inheritdoc} |
||
240 | */ |
||
241 | 12 | public function key() |
|
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | 10 | public function seek($pointer) |
|
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | */ |
||
257 | 1 | public function getFields() |
|
261 | |||
262 | /** |
||
263 | * Get a row |
||
264 | * |
||
265 | * @param integer $number Row number |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | 2 | public function getRow($number) |
|
275 | |||
276 | /** |
||
277 | * Get rows that have an invalid number of columns |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | 5 | public function getErrors() |
|
290 | |||
291 | /** |
||
292 | * Does the reader contain any invalid rows? |
||
293 | * |
||
294 | * @return boolean |
||
295 | */ |
||
296 | 5 | public function hasErrors() |
|
300 | |||
301 | /** |
||
302 | * Should the reader use strict parsing? |
||
303 | * |
||
304 | * @return boolean |
||
305 | */ |
||
306 | 11 | public function isStrict() |
|
310 | |||
311 | /** |
||
312 | * Set strict parsing |
||
313 | * |
||
314 | * @param boolean $strict |
||
315 | */ |
||
316 | 6 | public function setStrict($strict) |
|
320 | |||
321 | /** |
||
322 | * Read header row from CSV file |
||
323 | * |
||
324 | * @param integer $rowNumber Row number |
||
325 | * |
||
326 | * @return array |
||
327 | * |
||
328 | * @throws DuplicateHeadersException |
||
329 | */ |
||
330 | 11 | protected function readHeaderRow($rowNumber) |
|
351 | |||
352 | /** |
||
353 | * Add an increment to duplicate headers |
||
354 | * |
||
355 | * So the following line: |
||
356 | * |duplicate|duplicate|duplicate| |
||
357 | * |first |second |third | |
||
358 | * |
||
359 | * Yields value: |
||
360 | * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third' |
||
361 | * |
||
362 | * @param array $headers |
||
363 | * |
||
364 | * @return array |
||
365 | */ |
||
366 | 1 | protected function incrementHeaders(array $headers) |
|
382 | |||
383 | /** |
||
384 | * Merges values for duplicate headers into an array |
||
385 | * |
||
386 | * So the following line: |
||
387 | * |duplicate|duplicate|duplicate| |
||
388 | * |first |second |third | |
||
389 | * |
||
390 | * Yields value: |
||
391 | * $duplicate => ['first', 'second', 'third'] |
||
392 | * |
||
393 | * @param array $line |
||
394 | * |
||
395 | * @return array |
||
396 | */ |
||
397 | 1 | protected function mergeDuplicates(array $line) |
|
414 | } |
||
415 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.