Completed
Pull Request — master (#210)
by ignace nyamagana
02:36
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
    protected static $method_list;
34
35
    /**
36
     * @inheritdoc
37
     */
38
    protected $stream_filter_mode = STREAM_FILTER_READ;
39
40
    /**
41
     * Returns a collection of selected records
42
     *
43 159
     * @param Statement|null $stmt
44
     *
45 159
     * @return RecordSet
46
     */
47
    public function select(Statement $stmt = null): RecordSet
48
    {
49
        $stmt = $stmt ?: new Statement();
50
51
        return $stmt->process($this);
52
    }
53
54
    /**
55
     * @inheritdoc
56 159
     */
57
    public function count(): int
58 159
    {
59 75
        return $this->select()->count();
60
    }
61
62 144
    /**
63
     * @inheritdoc
64
     */
65
    public function getIterator(): Iterator
66
    {
67
        return $this->select()->getIterator();
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function jsonSerialize()
74 63
    {
75
        return $this->select()->jsonSerialize();
76 63
    }
77
78
    /**
79
     * Returns a HTML table representation of the CSV Table
80
     *
81
     * @param string $class_attr optional classname
82
     *
83
     * @return string
84
     */
85
    public function toHTML(string $class_attr = 'table-csv-data'): string
86
    {
87
        return $this->select()->toHTML($class_attr);
88
    }
89 9
90
    /**
91 9
     * Transforms a CSV into a XML
92 9
     *
93 9
     * @param string $root_name XML root node name
94 9
     * @param string $row_name  XML row node name
95 6
     * @param string $cell_name XML cell node name
96 9
     *
97 9
     * @return DomDocument
98
     */
99 6
    public function toXML(string $root_name = 'csv', string $row_name = 'row', string $cell_name = 'cell'): DomDocument
100 9
    {
101 9
        return $this->select()->toXML($root_name, $row_name, $cell_name);
102 6
    }
103
104 9
    /**
105
     * Returns a sequential array of all CSV lines
106
     *
107
     * The callable function will be applied to each Iterator item
108
     *
109
     * @return array
110
     */
111
    public function fetchAll(): array
112
    {
113
        return $this->select()->fetchAll();
114
    }
115
116 12
    /**
117
     * Returns a single row from the CSV
118 12
     *
119 12
     * By default if no offset is provided the first row of the CSV is selected
120 12
     *
121 12
     * @param int $offset the CSV row offset
122
     *
123 12
     * @return array
124
     */
125
    public function fetchOne(int $offset = 0): array
126
    {
127
        return $this->select()->fetchOne($offset);
128
    }
129
130
    /**
131
     * Returns the next value from a single CSV column
132
     *
133
     * The callable function will be applied to each value to be return
134
     *
135
     * By default if no column index is provided the first column of the CSV is selected
136
     *
137
     * @param string|int $column_index CSV column index
138 21
     *
139
     * @return Iterator
140 21
     */
141
    public function fetchColumn($column_index = 0): Iterator
142
    {
143 18
        return $this->select()->fetchColumn($column_index);
144 18
    }
145
146
    /**
147 15
     * Fetches the next key-value pairs from a result set (first
148 18
     * column is the key, second column is the value).
149
     *
150 18
     * By default if no column index is provided:
151 18
     * - the first CSV column is used to provide the keys
152 18
     * - the second CSV column is used to provide the value
153 18
     *
154
     * @param string|int $offset_index The column index to serve as offset
155 18
     * @param string|int $value_index  The column index to serve as value
156
     *
157
     * @return Generator
158
     */
159
    public function fetchPairs($offset_index = 0, $value_index = 1)
160
    {
161
        return $this->select()->fetchPairs($offset_index, $value_index);
162
    }
163
}
164