Completed
Push — master ( a3df6b...bee6cf )
by ignace nyamagana
23s queued 10s
created

Reader::fetchColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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