-
-
Notifications
You must be signed in to change notification settings - Fork 348
Expand file tree
/
Copy pathReader.php
More file actions
435 lines (372 loc) · 11.3 KB
/
Copy pathReader.php
File metadata and controls
435 lines (372 loc) · 11.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php
/**
* League.Csv (https://csv.thephpleague.com)
*
* (c) Ignace Nyamagana Butera <nyamsprod@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace League\Csv;
use BadMethodCallException;
use CallbackFilterIterator;
use Countable;
use Generator;
use Iterator;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Polyfill\EmptyEscapeParser;
use SplFileObject;
use TypeError;
use function array_combine;
use function array_filter;
use function array_pad;
use function array_slice;
use function array_unique;
use function count;
use function gettype;
use function is_array;
use function iterator_count;
use function iterator_to_array;
use function mb_strlen;
use function mb_substr;
use function sprintf;
use function strlen;
use function substr;
use const PHP_VERSION_ID;
use const STREAM_FILTER_READ;
/**
* A class to parse and read records from a CSV document.
*
* @method array fetchOne(int $nth_record = 0) Returns a single record from the CSV
* @method Generator fetchColumn(string|int $column_index) Returns the next value from a single CSV record field
* @method Generator fetchPairs(string|int $offset_index = 0, string|int $value_index = 1) Fetches the next key-value pairs from the CSV document
*/
class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable
{
/**
* header offset.
*
* @var int|null
*/
protected $header_offset;
/**
* header record.
*
* @var string[]
*/
protected $header = [];
/**
* records count.
*
* @var int
*/
protected $nb_records = -1;
/**
* {@inheritdoc}
*/
protected $stream_filter_mode = STREAM_FILTER_READ;
/**
* @var bool
*/
protected $is_empty_records_included = false;
/**
* {@inheritdoc}
*/
public static function createFromPath(string $path, string $open_mode = 'r', $context = null)
{
return parent::createFromPath($path, $open_mode, $context);
}
/**
* {@inheritdoc}
*/
protected function resetProperties()
{
parent::resetProperties();
$this->nb_records = -1;
$this->header = [];
}
/**
* Returns the header offset.
*
* If no CSV header offset is set this method MUST return null
*
* @return int|null
*/
public function getHeaderOffset()
{
return $this->header_offset;
}
/**
* Returns the CSV record used as header.
*
* The returned header is represented as an array of string values
*
* @return string[]
*/
public function getHeader(): array
{
if (null === $this->header_offset) {
return $this->header;
}
if ([] !== $this->header) {
return $this->header;
}
$this->header = $this->setHeader($this->header_offset);
return $this->header;
}
/**
* Determine the CSV record header.
*
* @throws Exception If the header offset is set and no record is found or is the empty array
*
* @return string[]
*/
protected function setHeader(int $offset): array
{
$header = $this->seekRow($offset);
if (false === $header || [] === $header || [null] === $header) {
throw new SyntaxError(sprintf('The header record does not exist or is empty at offset: `%s`', $offset));
}
if (0 === $offset) {
return $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure);
}
return $header;
}
/**
* Returns the row at a given offset.
*
* @return array|false
*/
protected function seekRow(int $offset)
{
foreach ($this->getDocument() as $index => $record) {
if ($offset === $index) {
return $record;
}
}
return false;
}
/**
* Returns the document as an Iterator.
*/
protected function getDocument(): Iterator
{
if (70400 > PHP_VERSION_ID && '' === $this->escape) {
$this->document->setCsvControl($this->delimiter, $this->enclosure);
return EmptyEscapeParser::parse($this->document);
}
$this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD);
$this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$this->document->rewind();
return $this->document;
}
/**
* Strip the BOM sequence from a record.
*
* @param string[] $record
*
* @return string[]
*/
protected function removeBOM(array $record, int $bom_length, string $enclosure): array
{
if (0 === $bom_length) {
return $record;
}
$record[0] = mb_substr($record[0], $bom_length);
if ($enclosure.$enclosure != substr($record[0].$record[0], strlen($record[0]) - 1, 2)) {
return $record;
}
$record[0] = substr($record[0], 1, -1);
return $record;
}
/**
* {@inheritdoc}
*/
public function __call($method, array $arguments)
{
static $whitelisted = ['fetchColumn' => 1, 'fetchOne' => 1, 'fetchPairs' => 1];
if (isset($whitelisted[$method])) {
return (new ResultSet($this->getRecords(), $this->getHeader()))->$method(...$arguments);
}
throw new BadMethodCallException(sprintf('%s::%s() method does not exist', static::class, $method));
}
/**
* {@inheritdoc}
*/
public function count(): int
{
if (-1 === $this->nb_records) {
$this->nb_records = iterator_count($this->getRecords());
}
return $this->nb_records;
}
/**
* {@inheritdoc}
*/
public function getIterator(): Iterator
{
return $this->getRecords();
}
/**
* {@inheritdoc}
*/
public function jsonSerialize(): array
{
return iterator_to_array($this->getRecords(), false);
}
/**
* Returns the CSV records as an iterator object.
*
* Each CSV record is represented as a simple array containing strings or null values.
*
* If the CSV document has a header record then each record is combined
* to the header record and the header record is removed from the iterator.
*
* If the CSV document is inconsistent. Missing record fields are
* filled with null values while extra record fields are strip from
* the returned object.
*
* @param string[] $header an optional header to use instead of the CSV document header
*/
public function getRecords(array $header = []): Iterator
{
$header = $this->computeHeader($header);
$normalized = function ($record): bool {
return is_array($record) && ($this->is_empty_records_included || $record != [null]);
};
$bom = '';
if (!$this->is_input_bom_included) {
$bom = $this->getInputBOM();
}
$document = $this->getDocument();
$records = $this->stripBOM(new CallbackFilterIterator($document, $normalized), $bom);
if (null !== $this->header_offset) {
$records = new CallbackFilterIterator($records, function (array $record, int $offset): bool {
return $offset !== $this->header_offset;
});
}
if ($this->is_empty_records_included) {
$normalized_empty_records = static function (array $record): array {
if ([null] === $record) {
return [];
}
return $record;
};
return $this->combineHeader(new MapIterator($records, $normalized_empty_records), $header);
}
return $this->combineHeader($records, $header);
}
/**
* Returns the header to be used for iteration.
*
* @param string[] $header
*
* @throws Exception If the header contains non unique column name
*
* @return string[]
*/
protected function computeHeader(array $header)
{
if ([] === $header) {
$header = $this->getHeader();
}
if ($header === array_unique(array_filter($header, 'is_string'))) {
return $header;
}
throw new SyntaxError('The header record must be empty or a flat array with unique string values');
}
/**
* Combine the CSV header to each record if present.
*
* @param string[] $header
*/
protected function combineHeader(Iterator $iterator, array $header): Iterator
{
if ([] === $header) {
return $iterator;
}
$field_count = count($header);
$mapper = static function (array $record) use ($header, $field_count): array {
if (count($record) != $field_count) {
$record = array_slice(array_pad($record, $field_count, null), 0, $field_count);
}
return array_combine($header, $record);
};
return new MapIterator($iterator, $mapper);
}
/**
* Strip the BOM sequence from the returned records if necessary.
*/
protected function stripBOM(Iterator $iterator, string $bom): Iterator
{
if ('' === $bom) {
return $iterator;
}
$bom_length = mb_strlen($bom);
$mapper = function (array $record, int $index) use ($bom_length): array {
if (0 !== $index) {
return $record;
}
return $this->removeBOM($record, $bom_length, $this->enclosure);
};
return new MapIterator($iterator, $mapper);
}
/**
* Selects the record to be used as the CSV header.
*
* Because the header is represented as an array, to be valid
* a header MUST contain only unique string value.
*
* @param int|null $offset the header record offset
*
* @throws Exception if the offset is a negative integer
*
* @return static
*/
public function setHeaderOffset($offset): self
{
if ($offset === $this->header_offset) {
return $this;
}
if (!is_nullable_int($offset)) {
throw new TypeError(sprintf(__METHOD__.'() expects 1 Argument to be null or an integer %s given', gettype($offset)));
}
if (null !== $offset && 0 > $offset) {
throw new InvalidArgument(__METHOD__.'() expects 1 Argument to be greater or equal to 0');
}
$this->header_offset = $offset;
$this->resetProperties();
return $this;
}
/**
* Enable skipping empty records.
*/
public function skipEmptyRecords(): self
{
if ($this->is_empty_records_included) {
$this->is_empty_records_included = false;
$this->nb_records = -1;
}
return $this;
}
/**
* Disable skipping empty records.
*/
public function includeEmptyRecords(): self
{
if (!$this->is_empty_records_included) {
$this->is_empty_records_included = true;
$this->nb_records = -1;
}
return $this;
}
/**
* Tells whether empty records are skipped by the instance.
*/
public function isEmptyRecordsIncluded(): bool
{
return $this->is_empty_records_included;
}
}