Completed
Pull Request — master (#210)
by ignace nyamagana
04:28
created

RecordSet::preserveOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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