1 | <?php |
||
50 | class CsvImportService implements \Iterator, \SeekableIterator, \Countable |
||
51 | { |
||
52 | |||
53 | const DUPLICATE_HEADERS_INCREMENT = 1; |
||
54 | const DUPLICATE_HEADERS_MERGE = 2; |
||
55 | |||
56 | /** |
||
57 | * Number of the row that contains the column names |
||
58 | * |
||
59 | * @var integer |
||
60 | */ |
||
61 | protected $headerRowNumber; |
||
62 | |||
63 | /** |
||
64 | * CSV file |
||
65 | * |
||
66 | * @var \SplFileObject |
||
67 | */ |
||
68 | protected $file; |
||
69 | |||
70 | /** |
||
71 | * Column headers as read from the CSV file |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $columnHeaders = array(); |
||
76 | |||
77 | /** |
||
78 | * Number of column headers, stored and re-used for performance |
||
79 | * |
||
80 | * In case of duplicate headers, this is always the number of unmerged headers. |
||
81 | * |
||
82 | * @var integer |
||
83 | */ |
||
84 | protected $headersCount; |
||
85 | |||
86 | /** |
||
87 | * Total number of rows in the CSV file |
||
88 | * |
||
89 | * @var integer |
||
90 | */ |
||
91 | protected $count; |
||
92 | |||
93 | /** |
||
94 | * Faulty CSV rows |
||
95 | * |
||
96 | * @var array |
||
97 | */ |
||
98 | protected $errors = array(); |
||
99 | |||
100 | /** |
||
101 | * How to handle duplicate headers |
||
102 | * |
||
103 | * @var integer |
||
104 | */ |
||
105 | protected $duplicateHeadersFlag; |
||
106 | |||
107 | |||
108 | /** |
||
109 | * @param \SplFileObject $file |
||
110 | * @param string $delimiter |
||
|
|||
111 | * @param string $enclosure |
||
112 | * @param string $escape |
||
113 | */ |
||
114 | 14 | public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') |
|
131 | |||
132 | /** |
||
133 | * Return the current row as an array |
||
134 | * |
||
135 | * If a header row has been set, an associative array will be returned |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | 9 | public function current() |
|
169 | |||
170 | /** |
||
171 | * Get column headers |
||
172 | 8 | * |
|
173 | * @return array |
||
174 | 8 | */ |
|
175 | public function getColumnHeaders() |
||
179 | |||
180 | /** |
||
181 | * Set column headers |
||
182 | 11 | * |
|
183 | * @param array $columnHeaders |
||
184 | 11 | */ |
|
185 | 11 | public function setColumnHeaders(array $columnHeaders) |
|
191 | |||
192 | /** |
||
193 | * Set header row number |
||
194 | * |
||
195 | * @param integer $rowNumber Number of the row that contains column header names |
||
196 | * @param integer $duplicates How to handle duplicates (optional). One of: |
||
197 | * - CsvReader::DUPLICATE_HEADERS_INCREMENT; |
||
198 | * increments duplicates (dup, dup1, dup2 etc.) |
||
199 | * - CsvReader::DUPLICATE_HEADERS_MERGE; merges |
||
200 | 9 | * values for duplicate headers into an array |
|
201 | * (dup => [value1, value2, value3]) |
||
202 | 9 | * @return boolean |
|
203 | 9 | */ |
|
204 | 9 | public function setHeaderRowNumber($rowNumber, $duplicates = null) |
|
216 | |||
217 | /** |
||
218 | * Rewind the file pointer |
||
219 | * |
||
220 | 13 | * If a header row has been set, the pointer is set just below the header |
|
221 | * row. That way, when you iterate over the rows, that header row is |
||
222 | 13 | * skipped. |
|
223 | 13 | */ |
|
224 | 9 | public function rewind() |
|
231 | 9 | ||
232 | /** |
||
233 | 9 | * {@inheritdoc} |
|
234 | 9 | */ |
|
235 | public function count() |
||
247 | 12 | ||
248 | /** |
||
249 | 12 | * {@inheritdoc} |
|
250 | 12 | */ |
|
251 | public function next() |
||
255 | 13 | ||
256 | /** |
||
257 | 13 | * {@inheritdoc} |
|
258 | */ |
||
259 | public function valid() |
||
263 | 9 | ||
264 | /** |
||
265 | 9 | * {@inheritdoc} |
|
266 | */ |
||
267 | public function key() |
||
271 | 10 | ||
272 | /** |
||
273 | 10 | * {@inheritdoc} |
|
274 | 10 | */ |
|
275 | public function seek($pointer) |
||
279 | 1 | ||
280 | /** |
||
281 | 1 | * {@inheritdoc} |
|
282 | */ |
||
283 | public function getFields() |
||
287 | |||
288 | /** |
||
289 | * Get a row |
||
290 | * |
||
291 | 1 | * @param integer $number Row number |
|
292 | * |
||
293 | 1 | * @return array |
|
294 | */ |
||
295 | 1 | public function getRow($number) |
|
301 | |||
302 | /** |
||
303 | * Get rows that have an invalid number of columns |
||
304 | * |
||
305 | * @return array |
||
306 | */ |
||
307 | public function getErrors() |
||
317 | |||
318 | /** |
||
319 | * Does the reader contain any invalid rows? |
||
320 | * |
||
321 | * @return boolean |
||
322 | */ |
||
323 | public function hasErrors() |
||
327 | |||
328 | /** |
||
329 | * Read header row from CSV file |
||
330 | * |
||
331 | * @param integer $rowNumber Row number |
||
332 | 9 | * |
|
333 | * @return array |
||
334 | 9 | * |
|
335 | 9 | */ |
|
336 | protected function readHeaderRow($rowNumber) |
||
343 | |||
344 | /** |
||
345 | * Add an increment to duplicate headers |
||
346 | * |
||
347 | * So the following line: |
||
348 | * |duplicate|duplicate|duplicate| |
||
349 | * |first |second |third | |
||
350 | * |
||
351 | * Yields value: |
||
352 | * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third' |
||
353 | * |
||
354 | * @param array $headers |
||
355 | * |
||
356 | * @return array |
||
357 | */ |
||
358 | protected function incrementHeaders(array $headers) |
||
374 | |||
375 | /** |
||
376 | * Merges values for duplicate headers into an array |
||
377 | * |
||
378 | * So the following line: |
||
379 | * |duplicate|duplicate|duplicate| |
||
380 | * |first |second |third | |
||
381 | * |
||
382 | * Yields value: |
||
383 | * $duplicate => ['first', 'second', 'third'] |
||
384 | * |
||
385 | 1 | * @param array $line |
|
386 | * |
||
387 | 1 | * @return array |
|
388 | */ |
||
389 | 1 | protected function mergeDuplicates(array $line) |
|
406 | |||
407 | /** |
||
408 | * 行の文字エンコーディングを変換する. |
||
409 | * |
||
410 | * Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する. |
||
411 | * それ以外の環境では何もしない。 |
||
412 | */ |
||
413 | protected function convertEncodingRows($row) { |
||
421 | } |
||
422 |