|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Eccube\Service; |
|
25
|
|
|
|
|
26
|
|
|
use Eccube\Application; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Copyright (C) 2012-2014 David de Boer <[email protected]> |
|
31
|
|
|
* |
|
32
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
33
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
34
|
|
|
* the Software without restriction, including without limitation the rights to |
|
35
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
|
36
|
|
|
* the Software, and to permit persons to whom the Software is furnished to do so, |
|
37
|
|
|
* subject to the following conditions: |
|
38
|
|
|
* |
|
39
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
40
|
|
|
* copies or substantial portions of the Software. |
|
41
|
|
|
* |
|
42
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
43
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
44
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
45
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
46
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
47
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
48
|
|
|
* SOFTWARE. |
|
49
|
|
|
*/ |
|
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 = '\\') |
|
115
|
|
|
{ |
|
116
|
14 |
|
ini_set('auto_detect_line_endings', true); |
|
117
|
|
|
|
|
118
|
14 |
|
$this->file = $file; |
|
119
|
14 |
|
$this->file->setFlags( |
|
120
|
14 |
|
\SplFileObject::READ_CSV | |
|
121
|
14 |
|
\SplFileObject::SKIP_EMPTY | |
|
122
|
14 |
|
\SplFileObject::READ_AHEAD | |
|
123
|
|
|
\SplFileObject::DROP_NEW_LINE |
|
124
|
14 |
|
); |
|
125
|
14 |
|
$this->file->setCsvControl( |
|
126
|
14 |
|
$delimiter, |
|
127
|
14 |
|
$enclosure, |
|
128
|
|
|
$escape |
|
129
|
14 |
|
); |
|
130
|
14 |
|
} |
|
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() |
|
140
|
|
|
{ |
|
141
|
|
|
// If the CSV has no column headers just return the line |
|
142
|
9 |
|
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
|
9 |
|
if ($this->valid()) { |
|
148
|
9 |
|
$current = $this->file->current(); |
|
149
|
|
|
$current = $this->convertEncodingRows($current); |
|
150
|
|
|
|
|
151
|
9 |
|
$line = $current; |
|
152
|
1 |
|
|
|
153
|
1 |
|
// See if values for duplicate headers should be merged |
|
154
|
|
|
if (self::DUPLICATE_HEADERS_MERGE === $this->duplicateHeadersFlag) { |
|
155
|
|
|
$line = $this->mergeDuplicates($line); |
|
156
|
9 |
|
} |
|
157
|
9 |
|
|
|
158
|
|
|
// Count the number of elements in both: they must be equal. |
|
159
|
|
|
if (count($this->columnHeaders) === count($line)) { |
|
160
|
|
|
return array_combine(array_keys($this->columnHeaders), $line); |
|
161
|
|
|
} else { |
|
162
|
|
|
return $line; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
|
|
|
|
|
165
|
|
|
}; |
|
166
|
|
|
|
|
167
|
|
|
return null; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Get column headers |
|
172
|
8 |
|
* |
|
173
|
|
|
* @return array |
|
174
|
8 |
|
*/ |
|
175
|
|
|
public function getColumnHeaders() |
|
176
|
|
|
{ |
|
177
|
|
|
return array_keys($this->columnHeaders); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set column headers |
|
182
|
11 |
|
* |
|
183
|
|
|
* @param array $columnHeaders |
|
184
|
11 |
|
*/ |
|
185
|
11 |
|
public function setColumnHeaders(array $columnHeaders) |
|
186
|
11 |
|
{ |
|
187
|
|
|
$columnHeaders = $this->convertEncodingRows($columnHeaders); |
|
188
|
|
|
$this->columnHeaders = array_count_values($columnHeaders); |
|
189
|
|
|
$this->headersCount = count($columnHeaders); |
|
190
|
|
|
} |
|
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) |
|
205
|
|
|
{ |
|
206
|
9 |
|
$this->duplicateHeadersFlag = $duplicates; |
|
207
|
|
|
$this->headerRowNumber = $rowNumber; |
|
208
|
|
|
$headers = $this->readHeaderRow($rowNumber); |
|
209
|
9 |
|
|
|
210
|
9 |
|
if ($headers === false) { |
|
211
|
|
|
return false; |
|
212
|
|
|
} |
|
213
|
|
|
$this->setColumnHeaders($headers); |
|
|
|
|
|
|
214
|
|
|
return true; |
|
|
|
|
|
|
215
|
|
|
} |
|
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() |
|
225
|
9 |
|
{ |
|
226
|
13 |
|
$this->file->rewind(); |
|
227
|
|
|
if (null !== $this->headerRowNumber) { |
|
228
|
|
|
$this->file->seek($this->headerRowNumber + 1); |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
9 |
|
|
|
232
|
|
|
/** |
|
233
|
9 |
|
* {@inheritdoc} |
|
234
|
9 |
|
*/ |
|
235
|
|
|
public function count() |
|
236
|
9 |
|
{ |
|
237
|
|
|
if (null === $this->count) { |
|
238
|
9 |
|
$position = $this->key(); |
|
239
|
9 |
|
|
|
240
|
|
|
$this->count = iterator_count($this); |
|
241
|
9 |
|
|
|
242
|
|
|
$this->seek($position); |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
return $this->count; |
|
246
|
|
|
} |
|
247
|
12 |
|
|
|
248
|
|
|
/** |
|
249
|
12 |
|
* {@inheritdoc} |
|
250
|
12 |
|
*/ |
|
251
|
|
|
public function next() |
|
252
|
|
|
{ |
|
253
|
|
|
$this->file->next(); |
|
254
|
|
|
} |
|
255
|
13 |
|
|
|
256
|
|
|
/** |
|
257
|
13 |
|
* {@inheritdoc} |
|
258
|
|
|
*/ |
|
259
|
|
|
public function valid() |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->file->valid(); |
|
262
|
|
|
} |
|
263
|
9 |
|
|
|
264
|
|
|
/** |
|
265
|
9 |
|
* {@inheritdoc} |
|
266
|
|
|
*/ |
|
267
|
|
|
public function key() |
|
268
|
|
|
{ |
|
269
|
|
|
return $this->file->key(); |
|
270
|
|
|
} |
|
271
|
10 |
|
|
|
272
|
|
|
/** |
|
273
|
10 |
|
* {@inheritdoc} |
|
274
|
10 |
|
*/ |
|
275
|
|
|
public function seek($pointer) |
|
276
|
|
|
{ |
|
277
|
|
|
$this->file->seek($pointer); |
|
278
|
|
|
} |
|
279
|
1 |
|
|
|
280
|
|
|
/** |
|
281
|
1 |
|
* {@inheritdoc} |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getFields() |
|
284
|
|
|
{ |
|
285
|
|
|
return $this->getColumnHeaders(); |
|
286
|
|
|
} |
|
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) |
|
296
|
|
|
{ |
|
297
|
|
|
$this->seek($number); |
|
298
|
|
|
|
|
299
|
|
|
return $this->current(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Get rows that have an invalid number of columns |
|
304
|
|
|
* |
|
305
|
|
|
* @return array |
|
306
|
|
|
*/ |
|
307
|
|
|
public function getErrors() |
|
308
|
|
|
{ |
|
309
|
|
|
if (0 === $this->key()) { |
|
310
|
|
|
// Iterator has not yet been processed, so do that now |
|
311
|
|
|
foreach ($this as $row) { /* noop */ |
|
|
|
|
|
|
312
|
|
|
} |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
return $this->errors; |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* Does the reader contain any invalid rows? |
|
320
|
|
|
* |
|
321
|
|
|
* @return boolean |
|
322
|
|
|
*/ |
|
323
|
|
|
public function hasErrors() |
|
324
|
|
|
{ |
|
325
|
|
|
return count($this->getErrors()) > 0; |
|
326
|
|
|
} |
|
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) |
|
337
|
9 |
|
{ |
|
338
|
|
|
$this->file->seek($rowNumber); |
|
339
|
|
|
$headers = $this->file->current(); |
|
340
|
|
|
|
|
341
|
|
|
return $headers; |
|
342
|
|
|
} |
|
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) |
|
359
|
|
|
{ |
|
360
|
|
|
$incrementedHeaders = array(); |
|
361
|
|
|
foreach (array_count_values($headers) as $header => $count) { |
|
362
|
|
|
if ($count > 1) { |
|
363
|
|
|
$incrementedHeaders[] = $header; |
|
364
|
|
|
for ($i = 1; $i < $count; $i++) { |
|
365
|
|
|
$incrementedHeaders[] = $header . $i; |
|
|
|
|
|
|
366
|
|
|
} |
|
367
|
|
|
} else { |
|
368
|
|
|
$incrementedHeaders[] = $header; |
|
369
|
|
|
} |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
return $incrementedHeaders; |
|
373
|
|
|
} |
|
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) |
|
390
|
1 |
|
{ |
|
391
|
1 |
|
$values = array(); |
|
392
|
1 |
|
|
|
393
|
1 |
|
$i = 0; |
|
394
|
1 |
|
foreach ($this->columnHeaders as $count) { |
|
395
|
|
|
if (1 === $count) { |
|
396
|
|
|
$values[] = $line[$i]; |
|
397
|
1 |
|
} else { |
|
398
|
1 |
|
$values[] = array_slice($line, $i, $count); |
|
399
|
|
|
} |
|
400
|
1 |
|
|
|
401
|
|
|
$i += $count; |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
|
|
return $values; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* 行の文字エンコーディングを変換する. |
|
409
|
|
|
* |
|
410
|
|
|
* Windows 版 PHP7 環境では、ファイルエンコーディングが CP932 になるため UTF-8 に変換する. |
|
411
|
|
|
* それ以外の環境では何もしない。 |
|
412
|
|
|
*/ |
|
413
|
|
|
protected function convertEncodingRows($row) { |
|
414
|
|
|
if ('\\' === DIRECTORY_SEPARATOR && PHP_VERSION_ID >= 70000) { |
|
415
|
|
|
foreach ($row as &$col) { |
|
416
|
|
|
$col = mb_convert_encoding($col , 'UTF-8', 'SJIS-win'); |
|
|
|
|
|
|
417
|
|
|
} |
|
418
|
|
|
} |
|
419
|
|
|
return $row; |
|
|
|
|
|
|
420
|
|
|
} |
|
421
|
|
|
} |
|
422
|
|
|
|