Completed
Push — master ( 60d8b9...2438b2 )
by ignace nyamagana
06:00
created

RecordSet::getIterator()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 3
nop 0
crap 3
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 CallbackFilterIterator;
18
use Countable;
19
use DOMDocument;
20
use DOMElement;
21
use Generator;
22
use Iterator;
23
use IteratorAggregate;
24
use JsonSerializable;
25
use League\Csv\Exception\InvalidArgumentException;
26
use League\Csv\Exception\RuntimeException;
27
use LimitIterator;
28
29
/**
30
 * A class to manage extracting and filtering a CSV
31
 *
32
 * @package League.csv
33
 * @since   9.0.0
34
 * @author  Ignace Nyamagana Butera <[email protected]>
35
 *
36
 */
37
class RecordSet implements JsonSerializable, IteratorAggregate, Countable
38
{
39
    use ValidatorTrait;
40
41
    /**
42
     * The CSV iterator result
43
     *
44
     * @var Iterator
45
     */
46
    protected $iterator;
47
48
    /**
49
     * The CSV header
50
     *
51
     * @var array
52
     */
53
    protected $column_names = [];
54
55
    /**
56
     * Charset Encoding for the CSV
57
     *
58
     * This information is used when converting the CSV to XML or JSON
59
     *
60
     * @var string
61
     */
62
    protected $conversion_input_encoding = 'UTF-8';
63
64
    /**
65
     * Tell whether the CSV document offset
66
     * must be kept on output
67
     *
68
     * @var bool
69
     */
70
    protected $preserve_offset = false;
71
72
    /**
73
     * New instance
74
     *
75
     * @param Iterator $iterator     a CSV iterator
76
     * @param array    $column_names the CSV header
77
     */
78 112
    public function __construct(Iterator $iterator, array $column_names = [])
79
    {
80 112
        $this->iterator = $iterator;
81 112
        $this->column_names = $column_names;
82 112
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 112
    public function __destruct()
88
    {
89 112
        $this->iterator = null;
90 112
    }
91
92
    /**
93
     * Returns the field names associate with the RecordSet
94
     *
95
     * @return string[]
96
     */
97 6
    public function getColumnNames(): array
98
    {
99 6
        return $this->column_names;
100
    }
101
102
    /**
103
     * Tell whether the CSV document offset
104
     * must be kept on output
105
     *
106
     * @return bool
107
     */
108 2
    public function isOffsetPreserved(): bool
109
    {
110 2
        return $this->preserve_offset;
111
    }
112
113
    /**
114
     * Returns the conversion input encoding
115
     *
116
     * @return string
117
     */
118 2
    public function getConversionInputEncoding(): string
119
    {
120 2
        return $this->conversion_input_encoding;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     */
126 2
    public function getIterator(): Iterator
127
    {
128 2
        foreach ($this->iterator as $key => $value) {
129 2
            $this->preserve_offset ? yield $key => $value : yield $value;
130
        }
131 2
    }
132
133
    /**
134
     * @inheritdoc
135
     */
136 2
    public function count(): int
137
    {
138 2
        return iterator_count($this->iterator);
139
    }
140
141
    /**
142
     * @inheritdoc
143
     */
144 2
    public function jsonSerialize()
145
    {
146 2
        return iterator_to_array($this->convertToUtf8($this->iterator), $this->preserve_offset);
147
    }
148
149
    /**
150
     * Convert Csv file into UTF-8
151
     *
152
     * @param Iterator $iterator
153
     *
154
     * @return Iterator
155
     */
156 8
    protected function convertToUtf8(Iterator $iterator): Iterator
157
    {
158 8
        if (stripos($this->conversion_input_encoding, 'UTF-8') !== false) {
159 6
            return $iterator;
160
        }
161
162
        $convert_cell = function ($value) {
163 2
            return mb_convert_encoding((string) $value, 'UTF-8', $this->conversion_input_encoding);
164 1
        };
165
166
        $convert_row = function (array $row) use ($convert_cell) {
167 2
            $res = [];
168 2
            foreach ($row as $key => $value) {
169 2
                $res[$convert_cell($key)] = $convert_cell($value);
170
            }
171
172 2
            return $res;
173 2
        };
174
175 2
        return new MapIterator($iterator, $convert_row);
176
    }
177
178
    /**
179
     * Returns a HTML table representation of the CSV Table
180
     *
181
     * @param string $class_attr  optional classname
182
     * @param string $column_attr column attribute name
183
     * @param string $offset_attr offset attribute name
184
     *
185
     * @return string
186
     */
187 4
    public function toHTML(
188
        string $class_attr = 'table-csv-data',
189
        string $column_attr = 'title',
190
        string $offset_attr = 'data-record-offset'
191
    ): string {
192 4
        $doc = $this->toXML('table', 'tr', 'td', $column_attr, $offset_attr);
193 4
        $doc->documentElement->setAttribute('class', $class_attr);
194
195 4
        return $doc->saveHTML($doc->documentElement);
196
    }
197
198
    /**
199
     * Transforms a CSV into a XML
200
     *
201
     * @param string $root_name   XML root node name
202
     * @param string $row_name    XML row node name
203
     * @param string $cell_name   XML cell node name
204
     * @param string $column_attr XML column attribute name
205
     * @param string $offset_attr XML offset attribute name
206
     *
207
     * @return DOMDocument
208
     */
209 6
    public function toXML(
210
        string $root_name = 'csv',
211
        string $row_name = 'row',
212
        string $cell_name = 'cell',
213
        string $column_attr = 'name',
214
        string $offset_attr = 'offset'
215
    ): DOMDocument {
216 6
        $doc = new DOMDocument('1.0', 'UTF-8');
217 6
        $root = $doc->createElement($root_name);
218 6
        foreach ($this->convertToUtf8($this->iterator) as $offset => $row) {
219 6
            $root->appendChild($this->toDOMNode(
220
                $doc,
221
                $row,
222
                $offset,
223
                $row_name,
224
                $cell_name,
225
                $column_attr,
226
                $offset_attr
227
            ));
228
        }
229 6
        $doc->appendChild($root);
230
231 6
        return $doc;
232
    }
233
234
    /**
235
     * convert a Record into a DOMNode
236
     *
237
     * @param DOMDocument $doc         The DOMDocument
238
     * @param array       $row         The CSV record
239
     * @param int         $offset      The CSV record offset
240
     * @param string      $row_name    XML row node name
241
     * @param string      $cell_name   XML cell node name
242
     * @param string      $column_attr XML header attribute name
243
     * @param string      $offset_attr XML offset attribute name
244
     *
245
     * @return DOMElement
246
     */
247 6
    protected function toDOMNode(
248
        DOMDocument $doc,
249
        array $row,
250
        int $offset,
251
        string $row_name,
252
        string $cell_name,
253
        string $column_attr,
254
        string $offset_attr
255
    ): DOMElement {
256 6
        $rowElement = $doc->createElement($row_name);
257 6
        if ($this->preserve_offset) {
258 2
            $rowElement->setAttribute($offset_attr, (string) $offset);
259
        }
260 6
        foreach ($row as $name => $value) {
261 6
            $content = $doc->createTextNode($value);
262 6
            $cell = $doc->createElement($cell_name);
263 6
            if (!empty($this->column_names)) {
264 4
                $cell->setAttribute($column_attr, $name);
265
            }
266 6
            $cell->appendChild($content);
267 6
            $rowElement->appendChild($cell);
268
        }
269
270 6
        return $rowElement;
271
    }
272
273
    /**
274
     * Returns a sequential array of all CSV lines
275
     *
276
     * @return array
277
     */
278 64
    public function fetchAll(): array
279
    {
280 64
        return iterator_to_array($this->iterator, $this->preserve_offset);
281
    }
282
283
    /**
284
     * Returns a single row from the CSV
285
     *
286
     * By default if no offset is provided the first row of the CSV is selected
287
     *
288
     * @param int $offset the CSV row offset
289
     *
290
     * @return array
291
     */
292 6
    public function fetchOne(int $offset = 0): array
293
    {
294 6
        $offset = $this->filterInteger($offset, 0, __METHOD__.': the submitted offset is invalid');
295 4
        $it = new LimitIterator($this->iterator, $offset, 1);
296 4
        $it->rewind();
297
298 4
        return (array) $it->current();
299
    }
300
301
    /**
302
     * Returns the next value from a single CSV column
303
     *
304
     * By default if no column index is provided the first column of the CSV is selected
305
     *
306
     * @param string|int $index CSV column index
307
     *
308
     * @return Generator
309
     */
310 18
    public function fetchColumn($index = 0): Generator
311
    {
312 18
        $offset = $this->getColumnIndex($index, __METHOD__.': the column index `%s` value is invalid');
313
        $filter = function (array $row) use ($offset) {
314 12
            return isset($row[$offset]);
315 12
        };
316
317
        $select = function (array $row) use ($offset) {
318 10
            return $row[$offset];
319 12
        };
320
321 12
        $iterator = new MapIterator(new CallbackFilterIterator($this->iterator, $filter), $select);
322 12
        foreach ($iterator as $key => $value) {
323 10
            $this->preserve_offset ? yield $key => $value : yield $value;
324
        }
325 8
    }
326
327
    /**
328
     * Filter a column name against the CSV header if any
329
     *
330
     * @param string|int $field         the field name or the field index
331
     * @param string     $error_message the associated error message
332
     *
333
     * @throws InvalidArgumentException if the field is invalid
334
     * @throws RuntimeException         if the column is not found
335
     *
336
     * @return string|int
337
     */
338 26
    protected function getColumnIndex($field, string $error_message)
339
    {
340 26
        if (false !== array_search($field, $this->column_names, true)) {
341 2
            return $field;
342
        }
343
344 24
        if (is_string($field)) {
345 2
            throw new InvalidArgumentException(sprintf($error_message, $field));
346
        }
347
348 22
        $index = $this->filterInteger($field, 0, $error_message);
349 20
        if (empty($this->column_names)) {
350 16
            return $index;
351
        }
352
353 4
        $index = array_search($index, array_flip($this->column_names), true);
354 4
        if (false !== $index) {
355 2
            return $index;
356
        }
357
358 2
        throw new RuntimeException(sprintf($error_message, $field));
359
    }
360
361
    /**
362
     * Fetches the next key-value pairs from a result set (first
363
     * column is the key, second column is the value).
364
     *
365
     * By default if no column index is provided:
366
     * - the first CSV column is used to provide the keys
367
     * - the second CSV column is used to provide the value
368
     *
369
     * @param string|int $offset_index The column index to serve as offset
370
     * @param string|int $value_index  The column index to serve as value
371
     *
372
     * @return Generator
373
     */
374 8
    public function fetchPairs($offset_index = 0, $value_index = 1): Generator
375
    {
376 8
        $offset = $this->getColumnIndex($offset_index, __METHOD__.': the offset index value is invalid');
377 8
        $value = $this->getColumnIndex($value_index, __METHOD__.': the value index value is invalid');
378
379
        $filter = function (array $record) use ($offset) {
380 8
            return isset($record[$offset]);
381 8
        };
382
383 8
        $select = function (array $record) use ($offset, $value) {
384 6
            return [$record[$offset], $record[$value] ?? null];
385 8
        };
386
387 8
        $iterator = new MapIterator(new CallbackFilterIterator($this->iterator, $filter), $select);
388
389 8
        foreach ($iterator as $pair) {
390 6
            yield $pair[0] => $pair[1];
391
        }
392 8
    }
393
394
    /**
395
     * Sets the CSV encoding charset
396
     *
397
     * @param string $str
398
     *
399
     * @throws InvalidArgumentException if the charset is empty
400
     *
401
     * @return static
402
     */
403 4
    public function setConversionInputEncoding(string $str): self
404
    {
405 4
        $str = str_replace('_', '-', $str);
406 4
        $str = filter_var($str, FILTER_SANITIZE_STRING, ['flags' => FILTER_FLAG_STRIP_LOW | FILTER_FLAG_STRIP_HIGH]);
407 4
        $str = trim($str);
408 4
        if ('' === $str) {
409 2
            throw new InvalidArgumentException('you should use a valid charset');
410
        }
411 2
        $this->conversion_input_encoding = strtoupper($str);
412
413 2
        return $this;
414
    }
415
416
    /**
417
     * Whether we should preserve the CSV document record offset.
418
     *
419
     * If set to true CSV document record offset will added to
420
     * method output where it makes sense.
421
     *
422
     * @param bool $status
423
     *
424
     * @return static
425
     */
426 4
    public function preserveOffset(bool $status)
427
    {
428 4
        $this->preserve_offset = $status;
429
430 4
        return $this;
431
    }
432
}
433