Completed
Pull Request — master (#210)
by ignace nyamagana
02:58 queued 01:13
created

Reader::getFieldIndex()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
ccs 8
cts 8
cp 1
cc 4
eloc 9
nc 4
nop 2
crap 4
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 Countable;
18
use DomDocument;
19
use Generator;
20
use Iterator;
21
use IteratorAggregate;
22
use JsonSerializable;
23
24
/**
25
 *  A class to manage extracting and filtering a CSV
26
 *
27
 * @package League.csv
28
 * @since  3.0.0
29
 *
30
 */
31
class Reader extends AbstractCsv implements JsonSerializable, Countable, IteratorAggregate
32
{
33
    /**
34
     * @inheritdoc
35
     */
36
    protected $stream_filter_mode = STREAM_FILTER_READ;
37
38
    /**
39
     * Returns a collection of selected records
40
     *
41
     * @param Statement|null $stmt
42
     *
43 159
     * @return RecordSet
44
     */
45 159
    public function select(Statement $stmt = null): RecordSet
46
    {
47
        $stmt = $stmt ?: new Statement();
48
49
        return $stmt->process($this);
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function count(): int
56 159
    {
57
        return $this->select()->count();
58 159
    }
59 75
60
    /**
61
     * @inheritdoc
62 144
     */
63
    public function getIterator(): Iterator
64
    {
65
        return $this->select()->getIterator();
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function jsonSerialize()
72
    {
73
        return $this->select()->jsonSerialize();
74 63
    }
75
76 63
    /**
77
     * Returns a HTML table representation of the CSV Table
78
     *
79
     * @param string $class_attr optional classname
80
     *
81
     * @return string
82
     */
83
    public function toHTML(string $class_attr = 'table-csv-data'): string
84
    {
85
        return $this->select()->toHTML($class_attr);
86
    }
87
88
    /**
89 9
     * Transforms a CSV into a XML
90
     *
91 9
     * @param string $root_name XML root node name
92 9
     * @param string $row_name  XML row node name
93 9
     * @param string $cell_name XML cell node name
94 9
     *
95 6
     * @return DomDocument
96 9
     */
97 9
    public function toXML(string $root_name = 'csv', string $row_name = 'row', string $cell_name = 'cell'): DomDocument
98
    {
99 6
        return $this->select()->toXML($root_name, $row_name, $cell_name);
100 9
    }
101 9
102 6
    /**
103
     * Returns a sequential array of all CSV lines
104 9
     *
105
     * @return array
106
     */
107
    public function fetchAll(): array
108
    {
109
        return $this->select()->fetchAll();
110
    }
111
112
    /**
113
     * Returns a single row from the CSV
114
     *
115
     * By default if no offset is provided the first row of the CSV is selected
116 12
     *
117
     * @param int $offset the CSV row offset
118 12
     *
119 12
     * @return array
120 12
     */
121 12
    public function fetchOne(int $offset = 0): array
122
    {
123 12
        return $this->select()->fetchOne($offset);
124
    }
125
126
    /**
127
     * Returns the next value from a single CSV column
128
     *
129
     * By default if no column index is provided the first column of the CSV is selected
130
     *
131
     * @param string|int $column_index CSV column index
132
     *
133
     * @return Iterator
134
     */
135
    public function fetchColumn($column_index = 0): Iterator
136
    {
137
        return $this->select()->fetchColumn($column_index);
138 21
    }
139
140 21
    /**
141
     * Fetches the next key-value pairs from a result set (first
142
     * column is the key, second column is the value).
143 18
     *
144 18
     * By default if no column index is provided:
145
     * - the first CSV column is used to provide the keys
146
     * - the second CSV column is used to provide the value
147 15
     *
148 18
     * @param string|int $offset_index The column index to serve as offset
149
     * @param string|int $value_index  The column index to serve as value
150 18
     *
151 18
     * @return Generator
152 18
     */
153 18
    public function fetchPairs($offset_index = 0, $value_index = 1)
154
    {
155 18
        return $this->select()->fetchPairs($offset_index, $value_index);
156
    }
157
}
158