1 | <?php |
||
13 | class CsvReader implements CountableReader, \SeekableIterator |
||
|
|||
14 | { |
||
15 | const DUPLICATE_HEADERS_INCREMENT = 1; |
||
16 | const DUPLICATE_HEADERS_MERGE = 2; |
||
17 | |||
18 | /** |
||
19 | * Number of the row that contains the column names |
||
20 | * |
||
21 | * @var integer |
||
22 | */ |
||
23 | protected $headerRowNumber; |
||
24 | |||
25 | /** |
||
26 | * CSV file |
||
27 | * |
||
28 | * @var \SplFileObject |
||
29 | */ |
||
30 | protected $file; |
||
31 | |||
32 | /** |
||
33 | * Column headers as read from the CSV file |
||
34 | * |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $columnHeaders = []; |
||
38 | |||
39 | /** |
||
40 | * Number of column headers, stored and re-used for performance |
||
41 | * |
||
42 | * In case of duplicate headers, this is always the number of unmerged headers. |
||
43 | * |
||
44 | * @var integer |
||
45 | */ |
||
46 | protected $headersCount; |
||
47 | |||
48 | /** |
||
49 | * Total number of rows in the CSV file |
||
50 | * |
||
51 | * @var integer |
||
52 | */ |
||
53 | protected $count; |
||
54 | |||
55 | /** |
||
56 | * Faulty CSV rows |
||
57 | * |
||
58 | * @var array |
||
59 | */ |
||
60 | protected $errors = []; |
||
61 | |||
62 | /** |
||
63 | * Strict parsing - skip any lines mismatching header length |
||
64 | * |
||
65 | * @var boolean |
||
66 | */ |
||
67 | protected $strict = true; |
||
68 | |||
69 | /** |
||
70 | * How to handle duplicate headers |
||
71 | * |
||
72 | * @var integer |
||
73 | */ |
||
74 | protected $duplicateHeadersFlag; |
||
75 | |||
76 | /** |
||
77 | * @param \SplFileObject $file |
||
78 | * @param string $delimiter |
||
79 | * @param string $enclosure |
||
80 | * @param string $escape |
||
81 | */ |
||
82 | 19 | public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') |
|
99 | |||
100 | /** |
||
101 | * Return the current row as an array |
||
102 | * |
||
103 | * If a header row has been set, an associative array will be returned |
||
104 | * |
||
105 | * @return array |
||
106 | */ |
||
107 | 11 | public function current() |
|
146 | |||
147 | /** |
||
148 | * Get column headers |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | 3 | public function getColumnHeaders() |
|
156 | |||
157 | /** |
||
158 | * Set column headers |
||
159 | * |
||
160 | * @param array $columnHeaders |
||
161 | */ |
||
162 | 15 | public function setColumnHeaders(array $columnHeaders) |
|
167 | |||
168 | /** |
||
169 | * Set header row number |
||
170 | * |
||
171 | * @param integer $rowNumber Number of the row that contains column header names |
||
172 | * @param integer $duplicates How to handle duplicates (optional). One of: |
||
173 | * - CsvReader::DUPLICATE_HEADERS_INCREMENT; |
||
174 | * increments duplicates (dup, dup1, dup2 etc.) |
||
175 | * - CsvReader::DUPLICATE_HEADERS_MERGE; merges |
||
176 | * values for duplicate headers into an array |
||
177 | * (dup => [value1, value2, value3]) |
||
178 | * |
||
179 | * @throws DuplicateHeadersException If duplicate headers are encountered |
||
180 | * and no duplicate handling has been |
||
181 | * specified |
||
182 | */ |
||
183 | 11 | public function setHeaderRowNumber($rowNumber, $duplicates = null) |
|
191 | |||
192 | /** |
||
193 | * Rewind the file pointer |
||
194 | * |
||
195 | * If a header row has been set, the pointer is set just below the header |
||
196 | * row. That way, when you iterate over the rows, that header row is |
||
197 | * skipped. |
||
198 | */ |
||
199 | 17 | public function rewind() |
|
206 | |||
207 | /** |
||
208 | * {@inheritdoc} |
||
209 | */ |
||
210 | 9 | public function count() |
|
222 | |||
223 | /** |
||
224 | * {@inheritdoc} |
||
225 | */ |
||
226 | 15 | public function next() |
|
230 | |||
231 | /** |
||
232 | * {@inheritdoc} |
||
233 | */ |
||
234 | 15 | public function valid() |
|
238 | |||
239 | /** |
||
240 | * {@inheritdoc} |
||
241 | */ |
||
242 | 12 | public function key() |
|
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | 10 | public function seek($pointer) |
|
254 | |||
255 | /** |
||
256 | * Get a row |
||
257 | * |
||
258 | * @param integer $number Row number |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | 2 | public function getRow($number) |
|
268 | |||
269 | /** |
||
270 | * Get rows that have an invalid number of columns |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | 5 | public function getErrors() |
|
283 | |||
284 | /** |
||
285 | * Does the reader contain any invalid rows? |
||
286 | * |
||
287 | * @return boolean |
||
288 | */ |
||
289 | 5 | public function hasErrors() |
|
293 | |||
294 | /** |
||
295 | * Should the reader use strict parsing? |
||
296 | * |
||
297 | * @return boolean |
||
298 | */ |
||
299 | 11 | public function isStrict() |
|
303 | |||
304 | /** |
||
305 | * Set strict parsing |
||
306 | * |
||
307 | * @param boolean $strict |
||
308 | */ |
||
309 | 6 | public function setStrict($strict) |
|
313 | |||
314 | /** |
||
315 | * Read header row from CSV file |
||
316 | * |
||
317 | * @param integer $rowNumber Row number |
||
318 | * |
||
319 | * @return array |
||
320 | * |
||
321 | * @throws DuplicateHeadersException |
||
322 | */ |
||
323 | 11 | protected function readHeaderRow($rowNumber) |
|
344 | |||
345 | /** |
||
346 | * Add an increment to duplicate headers |
||
347 | * |
||
348 | * So the following line: |
||
349 | * |duplicate|duplicate|duplicate| |
||
350 | * |first |second |third | |
||
351 | * |
||
352 | * Yields value: |
||
353 | * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third' |
||
354 | * |
||
355 | * @param array $headers |
||
356 | * |
||
357 | * @return array |
||
358 | */ |
||
359 | 1 | protected function incrementHeaders(array $headers) |
|
375 | |||
376 | /** |
||
377 | * Merges values for duplicate headers into an array |
||
378 | * |
||
379 | * So the following line: |
||
380 | * |duplicate|duplicate|duplicate| |
||
381 | * |first |second |third | |
||
382 | * |
||
383 | * Yields value: |
||
384 | * $duplicate => ['first', 'second', 'third'] |
||
385 | * |
||
386 | * @param array $line |
||
387 | * |
||
388 | * @return array |
||
389 | */ |
||
390 | 1 | protected function mergeDuplicates(array $line) |
|
407 | } |
||
408 |