1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the League.csv library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/thephpleague/csv/ |
7
|
|
|
* @version 9.0.0 |
8
|
|
|
* @package League.csv |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace League\Csv; |
16
|
|
|
|
17
|
|
|
use BadMethodCallException; |
18
|
|
|
use CallbackFilterIterator; |
19
|
|
|
use Countable; |
20
|
|
|
use Iterator; |
21
|
|
|
use IteratorAggregate; |
22
|
|
|
use League\Csv\Exception\RuntimeException; |
23
|
|
|
use LimitIterator; |
24
|
|
|
use SplFileObject; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* A class to manage records selection from a CSV document |
28
|
|
|
* |
29
|
|
|
* @package League.csv |
30
|
|
|
* @since 3.0.0 |
31
|
|
|
* |
32
|
|
|
* @method array fetchAll() Returns a sequential array of all CSV records |
33
|
|
|
* @method array fetchOne(int $offset = 0) Returns a single record from the CSV |
34
|
|
|
* @method Generator fetchColumn(string|int $column_index) Returns the next value from a single CSV record field |
35
|
|
|
* @method Generator fetchPairs(string|int $offset_index, string|int $value_index) Fetches the next key-value pairs from the CSV document |
36
|
|
|
*/ |
37
|
|
|
class Reader extends AbstractCsv implements Countable, IteratorAggregate |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
protected $stream_filter_mode = STREAM_FILTER_READ; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The value to pad if the record is less than header size. |
46
|
|
|
* |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
protected $record_padding_value; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* CSV Document header offset |
53
|
|
|
* |
54
|
|
|
* @var int|null |
55
|
|
|
*/ |
56
|
|
|
protected $header_offset; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* CSV Document Header record |
60
|
|
|
* |
61
|
|
|
* @var string[] |
62
|
|
|
*/ |
63
|
|
|
protected $header = []; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Records count |
67
|
|
|
* |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
protected $nb_records = -1; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Detect Delimiters occurences in the CSV |
74
|
|
|
* |
75
|
|
|
* Returns a associative array where each key represents |
76
|
|
|
* a valid delimiter and each value the number of occurences |
77
|
|
|
* |
78
|
|
|
* @param string[] $delimiters the delimiters to consider |
79
|
|
|
* @param int $nb_records Detection is made using $nb_records of the CSV |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
6 |
|
public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
84
|
|
|
{ |
85
|
|
|
$filter = function ($value): bool { |
86
|
4 |
|
return 1 == strlen($value); |
87
|
3 |
|
}; |
88
|
|
|
|
89
|
6 |
|
$nb_records = $this->filterMinRange($nb_records, 1, __METHOD__.'() expects the number of records to consider to be a valid positive integer, %s given'); |
90
|
4 |
|
$delimiters = array_unique(array_filter($delimiters, $filter)); |
91
|
|
|
$reducer = function (array $res, string $delimiter) use ($nb_records): array { |
92
|
4 |
|
$res[$delimiter] = $this->getCellCount($delimiter, $nb_records); |
93
|
|
|
|
94
|
4 |
|
return $res; |
95
|
4 |
|
}; |
96
|
|
|
|
97
|
4 |
|
$res = array_reduce($delimiters, $reducer, []); |
98
|
4 |
|
arsort($res, SORT_NUMERIC); |
99
|
|
|
|
100
|
4 |
|
return $res; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Returns the cell count for a specified delimiter |
105
|
|
|
* and a specified number of records |
106
|
|
|
* |
107
|
|
|
* @param string $delimiter CSV delimiter |
108
|
|
|
* @param int $nb_records CSV records to consider |
109
|
|
|
* |
110
|
|
|
* @return int |
111
|
|
|
*/ |
112
|
2 |
|
protected function getCellCount(string $delimiter, int $nb_records): int |
113
|
|
|
{ |
114
|
|
|
$filter = function ($record): bool { |
115
|
2 |
|
return is_array($record) && count($record) > 1; |
116
|
1 |
|
}; |
117
|
|
|
|
118
|
2 |
|
$this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
119
|
2 |
|
$this->document->setCsvControl($delimiter, $this->enclosure, $this->escape); |
120
|
2 |
|
$iterator = new CallbackFilterIterator(new LimitIterator($this->document, 0, $nb_records), $filter); |
121
|
|
|
|
122
|
2 |
|
return count(iterator_to_array($iterator, false), COUNT_RECURSIVE); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the record padding value |
127
|
|
|
* |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
2 |
|
public function getRecordPaddingValue() |
131
|
|
|
{ |
132
|
2 |
|
return $this->record_padding_value; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the record offset used as header |
137
|
|
|
* |
138
|
|
|
* If no CSV record is used this method MUST return null |
139
|
|
|
* |
140
|
|
|
* @return int|null |
141
|
|
|
*/ |
142
|
2 |
|
public function getHeaderOffset() |
143
|
|
|
{ |
144
|
2 |
|
return $this->header_offset; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Returns wether the selected header can be combine to each record |
149
|
|
|
* |
150
|
|
|
* A valid header must be empty or contains unique string field names |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
2 |
|
public function supportsHeaderAsRecordKeys(): bool |
155
|
|
|
{ |
156
|
2 |
|
$header = $this->getHeader(); |
157
|
|
|
|
158
|
2 |
|
return empty($header) || $header === array_unique(array_filter($header, 'is_string')); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Returns the CSV record header |
163
|
|
|
* |
164
|
|
|
* The returned header is represented as an array of string values |
165
|
|
|
* |
166
|
|
|
* @return string[] |
167
|
|
|
*/ |
168
|
4 |
|
public function getHeader(): array |
169
|
|
|
{ |
170
|
4 |
|
if (null === $this->header_offset) { |
171
|
2 |
|
return $this->header; |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
if (empty($this->header)) { |
175
|
4 |
|
$this->header = $this->setHeader($this->header_offset); |
176
|
|
|
} |
177
|
|
|
|
178
|
4 |
|
return $this->header; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Determine the CSV record header |
183
|
|
|
* |
184
|
|
|
* @param int $offset |
185
|
|
|
* |
186
|
|
|
* @throws RuntimeException If the header offset is an integer |
187
|
|
|
* and the corresponding record is missing |
188
|
|
|
* or is an empty array |
189
|
|
|
* |
190
|
|
|
* @return string[] |
191
|
|
|
*/ |
192
|
6 |
|
protected function setHeader(int $offset): array |
193
|
|
|
{ |
194
|
6 |
|
$this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
195
|
6 |
|
$this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
196
|
6 |
|
$this->document->seek($offset); |
197
|
6 |
|
$header = $this->document->current(); |
198
|
6 |
|
if (empty($header)) { |
199
|
2 |
|
throw new RuntimeException(sprintf('The header record does not exist or is empty at offset: `%s`', $offset)); |
200
|
|
|
} |
201
|
|
|
|
202
|
4 |
|
if (0 === $offset) { |
203
|
2 |
|
return $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure); |
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
return $header; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Strip the BOM sequence from a record |
211
|
|
|
* |
212
|
|
|
* @param string[] $record |
213
|
|
|
* @param int $bom_length |
214
|
|
|
* @param string $enclosure |
215
|
|
|
* |
216
|
|
|
* @return string[] |
217
|
|
|
*/ |
218
|
8 |
|
protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
219
|
|
|
{ |
220
|
8 |
|
if (0 == $bom_length) { |
221
|
2 |
|
return $record; |
222
|
|
|
} |
223
|
|
|
|
224
|
6 |
|
$record[0] = mb_substr($record[0], $bom_length); |
225
|
6 |
|
if ($enclosure == mb_substr($record[0], 0, 1) && $enclosure == mb_substr($record[0], -1, 1)) { |
226
|
2 |
|
$record[0] = mb_substr($record[0], 1, -1); |
227
|
|
|
} |
228
|
|
|
|
229
|
6 |
|
return $record; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @inheritdoc |
234
|
|
|
*/ |
235
|
6 |
|
public function __call($method, array $arguments) |
236
|
|
|
{ |
237
|
6 |
|
$whitelisted = ['fetchColumn' => 1, 'fetchPairs' => 1, 'fetchOne' => 1, 'fetchAll' => 1]; |
238
|
6 |
|
if (isset($whitelisted[$method])) { |
239
|
2 |
|
return (new ResultSet($this->getRecords(), $this->getHeader()))->$method(...$arguments); |
240
|
|
|
} |
241
|
|
|
|
242
|
4 |
|
throw new BadMethodCallException(sprintf('%s::%s() method does not exist', __CLASS__, $method)); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @inheritdoc |
247
|
|
|
*/ |
248
|
2 |
|
public function count(): int |
249
|
|
|
{ |
250
|
2 |
|
if (-1 === $this->nb_records) { |
251
|
2 |
|
$this->nb_records = iterator_count($this->getRecords()); |
252
|
|
|
} |
253
|
|
|
|
254
|
2 |
|
return $this->nb_records; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @inheritdoc |
259
|
|
|
*/ |
260
|
2 |
|
public function getIterator(): Iterator |
261
|
|
|
{ |
262
|
2 |
|
return $this->getRecords(); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the CSV records in an iterator object. |
267
|
|
|
* |
268
|
|
|
* Each CSV record is represented as a simple array of string or null values. |
269
|
|
|
* |
270
|
|
|
* If the CSV document has a header record then each record is combined |
271
|
|
|
* to each header record and the header record is removed from the iterator. |
272
|
|
|
* |
273
|
|
|
* If the CSV document is inconsistent. Missing record fields are |
274
|
|
|
* filled with null values while extra record fields are strip from |
275
|
|
|
* the returned object. |
276
|
|
|
* |
277
|
|
|
* @param array $header |
278
|
|
|
* |
279
|
|
|
* @return Iterator |
280
|
|
|
*/ |
281
|
6 |
|
public function getRecords(array $header = []): Iterator |
282
|
|
|
{ |
283
|
6 |
|
$header = $this->computeHeader($header); |
284
|
|
|
$normalized = function ($record): bool { |
285
|
4 |
|
return is_array($record) && $record != [null]; |
286
|
2 |
|
}; |
287
|
4 |
|
$bom = $this->getInputBOM(); |
288
|
4 |
|
$this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
289
|
4 |
|
$this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
290
|
|
|
|
291
|
4 |
|
$records = $this->stripBOM(new CallbackFilterIterator($this->document, $normalized), $bom); |
292
|
4 |
|
if (null !== $this->header_offset) { |
293
|
|
|
$records = new CallbackFilterIterator($records, function (array $record, int $offset): bool { |
294
|
4 |
|
return $offset !== $this->header_offset; |
295
|
4 |
|
}); |
296
|
|
|
} |
297
|
|
|
|
298
|
4 |
|
return $this->combineHeader($records, $header); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Returns the header to be used for iteration |
303
|
|
|
* |
304
|
|
|
* @param string[] $header |
305
|
|
|
* |
306
|
|
|
* @throws RuntimeException If the header contains non unique column name |
307
|
|
|
* |
308
|
|
|
* @return string[] |
309
|
|
|
*/ |
310
|
10 |
|
protected function computeHeader(array $header) |
311
|
|
|
{ |
312
|
10 |
|
if (!empty($header) && $header === array_unique(array_filter($header, 'is_string'))) { |
313
|
2 |
|
return $header; |
314
|
|
|
} |
315
|
|
|
|
316
|
8 |
|
if ($this->supportsHeaderAsRecordKeys()) { |
317
|
6 |
|
return $this->getHeader(); |
318
|
|
|
} |
319
|
|
|
|
320
|
2 |
|
throw new RuntimeException('The header record must be empty or a flat array with unique string values'); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Add the CSV header if present and valid |
325
|
|
|
* |
326
|
|
|
* @param Iterator $iterator |
327
|
|
|
* @param array $header |
328
|
|
|
* |
329
|
|
|
* @return Iterator |
330
|
|
|
*/ |
331
|
14 |
|
protected function combineHeader(Iterator $iterator, array $header): Iterator |
332
|
|
|
{ |
333
|
14 |
|
if (empty($header)) { |
334
|
8 |
|
return $iterator; |
335
|
|
|
} |
336
|
|
|
|
337
|
8 |
|
$field_count = count($header); |
338
|
|
|
$mapper = function (array $record) use ($header, $field_count): array { |
339
|
8 |
|
if (count($record) != $field_count) { |
340
|
4 |
|
$record = array_slice(array_pad($record, $field_count, $this->record_padding_value), 0, $field_count); |
341
|
|
|
} |
342
|
|
|
|
343
|
8 |
|
return array_combine($header, $record); |
344
|
8 |
|
}; |
345
|
|
|
|
346
|
8 |
|
return new MapIterator($iterator, $mapper); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Strip the BOM sequence if present |
351
|
|
|
* |
352
|
|
|
* @param Iterator $iterator |
353
|
|
|
* @param string $bom |
354
|
|
|
* |
355
|
|
|
* @return Iterator |
356
|
|
|
*/ |
357
|
10 |
|
protected function stripBOM(Iterator $iterator, string $bom): Iterator |
358
|
|
|
{ |
359
|
10 |
|
if ('' === $bom) { |
360
|
4 |
|
return $iterator; |
361
|
|
|
} |
362
|
|
|
|
363
|
6 |
|
$bom_length = mb_strlen($bom); |
364
|
6 |
|
$mapper = function (array $record, int $index) use ($bom_length): array { |
365
|
6 |
|
if (0 != $index) { |
366
|
2 |
|
return $record; |
367
|
|
|
} |
368
|
|
|
|
369
|
6 |
|
return $this->removeBOM($record, $bom_length, $this->enclosure); |
370
|
6 |
|
}; |
371
|
|
|
|
372
|
6 |
|
return new MapIterator($iterator, $mapper); |
373
|
|
|
} |
374
|
|
|
|
375
|
|
|
/** |
376
|
|
|
* Set the record padding value |
377
|
|
|
* |
378
|
|
|
* @param mixed $record_padding_value |
379
|
|
|
* |
380
|
|
|
* @return static |
381
|
|
|
*/ |
382
|
2 |
|
public function setRecordPaddingValue($record_padding_value): self |
383
|
|
|
{ |
384
|
2 |
|
$this->record_padding_value = $record_padding_value; |
385
|
|
|
|
386
|
2 |
|
return $this; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* Selects the record to be used as the CSV header |
391
|
|
|
* |
392
|
|
|
* Because of the header is represented as an array, to be valid |
393
|
|
|
* a header MUST contain only unique string value. |
394
|
|
|
* |
395
|
|
|
* @param int|null $offset the header record offset |
396
|
|
|
* |
397
|
|
|
* @return static |
398
|
|
|
*/ |
399
|
2 |
|
public function setHeaderOffset($offset): self |
400
|
|
|
{ |
401
|
2 |
|
if (null !== $offset) { |
402
|
2 |
|
$offset = $this->filterMinRange($offset, 0, __METHOD__.'() expects the header offset index to be a positive integer or 0, %s given'); |
403
|
|
|
} |
404
|
|
|
|
405
|
2 |
|
if ($offset !== $this->header_offset) { |
406
|
2 |
|
$this->header_offset = $offset; |
407
|
2 |
|
$this->resetProperties(); |
408
|
|
|
} |
409
|
|
|
|
410
|
2 |
|
return $this; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* @inheritdoc |
415
|
|
|
*/ |
416
|
2 |
|
protected function resetProperties() |
417
|
|
|
{ |
418
|
2 |
|
$this->nb_records = -1; |
419
|
2 |
|
$this->header = []; |
420
|
2 |
|
} |
421
|
|
|
} |
422
|
|
|
|