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 | 4 | public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\') |
|
115 | { |
||
116 | ini_set('auto_detect_line_endings', true); |
||
117 | |||
118 | 4 | $this->file = $file; |
|
119 | 4 | $this->file->setFlags( |
|
120 | 4 | \SplFileObject::READ_CSV | |
|
121 | 4 | \SplFileObject::SKIP_EMPTY | |
|
122 | 4 | \SplFileObject::READ_AHEAD | |
|
123 | 4 | \SplFileObject::DROP_NEW_LINE |
|
124 | ); |
||
125 | 4 | $this->file->setCsvControl( |
|
126 | $delimiter, |
||
127 | $enclosure, |
||
128 | $escape |
||
129 | ); |
||
130 | } |
||
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 | public function current() |
||
140 | { |
||
141 | // If the CSV has no column headers just return the line |
||
142 | if (empty($this->columnHeaders)) { |
||
143 | return $this->file->current(); |
||
144 | } |
||
145 | |||
146 | // Since the CSV has column headers use them to construct an associative array for the columns in this line |
||
147 | if ($this->valid()) { |
||
148 | $line = $this->file->current(); |
||
149 | |||
150 | // See if values for duplicate headers should be merged |
||
151 | if (self::DUPLICATE_HEADERS_MERGE === $this->duplicateHeadersFlag) { |
||
152 | $line = $this->mergeDuplicates($line); |
||
153 | } |
||
154 | |||
155 | // Count the number of elements in both: they must be equal. |
||
156 | if (count($this->columnHeaders) === count($line)) { |
||
157 | return array_combine(array_keys($this->columnHeaders), $line); |
||
158 | } else { |
||
159 | return $line; |
||
160 | } |
||
161 | |||
162 | }; |
||
163 | |||
164 | return null; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get column headers |
||
169 | * |
||
170 | * @return array |
||
171 | */ |
||
172 | public function getColumnHeaders() |
||
176 | |||
177 | /** |
||
178 | * Set column headers |
||
179 | * |
||
180 | * @param array $columnHeaders |
||
181 | */ |
||
182 | public function setColumnHeaders(array $columnHeaders) |
||
183 | { |
||
184 | $this->columnHeaders = array_count_values($columnHeaders); |
||
185 | $this->headersCount = count($columnHeaders); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Set header row number |
||
190 | * |
||
191 | * @param integer $rowNumber Number of the row that contains column header names |
||
192 | * @param integer $duplicates How to handle duplicates (optional). One of: |
||
193 | * - CsvReader::DUPLICATE_HEADERS_INCREMENT; |
||
194 | * increments duplicates (dup, dup1, dup2 etc.) |
||
195 | * - CsvReader::DUPLICATE_HEADERS_MERGE; merges |
||
196 | * values for duplicate headers into an array |
||
197 | * (dup => [value1, value2, value3]) |
||
198 | * @return boolean |
||
199 | */ |
||
200 | 1 | public function setHeaderRowNumber($rowNumber, $duplicates = null) |
|
201 | { |
||
202 | 1 | $this->duplicateHeadersFlag = $duplicates; |
|
203 | 1 | $this->headerRowNumber = $rowNumber; |
|
204 | $headers = $this->readHeaderRow($rowNumber); |
||
205 | |||
206 | 1 | if ($headers === false) { |
|
207 | return false; |
||
208 | } |
||
209 | $this->setColumnHeaders($headers); |
||
210 | 1 | return true; |
|
211 | 1 | } |
|
212 | |||
213 | /** |
||
214 | * Rewind the file pointer |
||
215 | * |
||
216 | * If a header row has been set, the pointer is set just below the header |
||
217 | * row. That way, when you iterate over the rows, that header row is |
||
218 | * skipped. |
||
219 | */ |
||
220 | 3 | public function rewind() |
|
221 | { |
||
222 | $this->file->rewind(); |
||
223 | 3 | if (null !== $this->headerRowNumber) { |
|
224 | $this->file->seek($this->headerRowNumber + 1); |
||
225 | } |
||
226 | 3 | } |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 2 | public function count() |
|
232 | { |
||
233 | 2 | if (null === $this->count) { |
|
234 | $position = $this->key(); |
||
235 | |||
236 | $this->count = iterator_count($this); |
||
237 | |||
238 | $this->seek($position); |
||
239 | } |
||
240 | |||
241 | 2 | return $this->count; |
|
242 | 2 | } |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function next() |
||
248 | { |
||
249 | $this->file->next(); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function valid() |
||
256 | { |
||
257 | return $this->file->valid(); |
||
258 | } |
||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | public function key() |
||
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 2 | public function seek($pointer) |
|
272 | { |
||
273 | $this->file->seek($pointer); |
||
274 | 2 | } |
|
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function getFields() |
||
280 | { |
||
281 | return $this->getColumnHeaders(); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Get a row |
||
286 | * |
||
287 | * @param integer $number Row number |
||
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getRow($number) |
||
292 | { |
||
293 | $this->seek($number); |
||
294 | |||
295 | return $this->current(); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Get rows that have an invalid number of columns |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | public function getErrors() |
||
304 | { |
||
305 | if (0 === $this->key()) { |
||
306 | // Iterator has not yet been processed, so do that now |
||
307 | foreach ($this as $row) { /* noop */ |
||
308 | } |
||
309 | } |
||
310 | |||
311 | return $this->errors; |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Does the reader contain any invalid rows? |
||
316 | * |
||
317 | * @return boolean |
||
318 | */ |
||
319 | public function hasErrors() |
||
323 | |||
324 | /** |
||
325 | * Read header row from CSV file |
||
326 | * |
||
327 | * @param integer $rowNumber Row number |
||
328 | * |
||
329 | * @return array |
||
330 | * |
||
331 | */ |
||
332 | 1 | protected function readHeaderRow($rowNumber) |
|
333 | { |
||
334 | $this->file->seek($rowNumber); |
||
335 | $headers = $this->file->current(); |
||
336 | |||
337 | 1 | return $headers; |
|
338 | } |
||
339 | |||
340 | /** |
||
341 | * Add an increment to duplicate headers |
||
342 | * |
||
343 | * So the following line: |
||
344 | * |duplicate|duplicate|duplicate| |
||
345 | * |first |second |third | |
||
346 | * |
||
347 | * Yields value: |
||
348 | * $duplicate => 'first', $duplicate1 => 'second', $duplicate2 => 'third' |
||
349 | * |
||
350 | * @param array $headers |
||
351 | * |
||
352 | * @return array |
||
353 | */ |
||
354 | protected function incrementHeaders(array $headers) |
||
370 | |||
371 | /** |
||
372 | * Merges values for duplicate headers into an array |
||
373 | * |
||
374 | * So the following line: |
||
375 | * |duplicate|duplicate|duplicate| |
||
376 | * |first |second |third | |
||
377 | * |
||
378 | * Yields value: |
||
379 | * $duplicate => ['first', 'second', 'third'] |
||
380 | * |
||
381 | * @param array $line |
||
382 | * |
||
383 | * @return array |
||
384 | */ |
||
385 | protected function mergeDuplicates(array $line) |
||
402 | |||
403 | } |
||
404 |