1 | <?php |
||
34 | class RecordSet implements IteratorAggregate, Countable |
||
35 | { |
||
36 | use ValidatorTrait; |
||
37 | |||
38 | /** |
||
39 | * The CSV iterator result |
||
40 | * |
||
41 | * @var Iterator |
||
42 | */ |
||
43 | protected $iterator; |
||
44 | |||
45 | /** |
||
46 | * The CSV header |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $column_names = []; |
||
51 | |||
52 | /** |
||
53 | * Tell whether the CSV document offset |
||
54 | * must be kept on output |
||
55 | * |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $preserve_offset = false; |
||
59 | |||
60 | /** |
||
61 | * New instance |
||
62 | * |
||
63 | * @param Iterator $iterator a CSV iterator |
||
64 | * @param array $column_names the CSV header |
||
65 | */ |
||
66 | 110 | public function __construct(Iterator $iterator, array $column_names = []) |
|
71 | |||
72 | /** |
||
73 | * @inheritdoc |
||
74 | */ |
||
75 | 110 | public function __destruct() |
|
79 | |||
80 | /** |
||
81 | * Returns the field names associate with the RecordSet |
||
82 | * |
||
83 | * @return string[] |
||
84 | */ |
||
85 | 6 | public function getColumnNames(): array |
|
89 | |||
90 | /** |
||
91 | * Tell whether the CSV document offset |
||
92 | * must be kept on output |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 2 | public function isRecordOffsetPreserved(): bool |
|
97 | { |
||
98 | 2 | return $this->preserve_offset; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | */ |
||
104 | 8 | public function getIterator(): Generator |
|
108 | |||
109 | /** |
||
110 | * Return the generator depending on the preserveRecordOffset setting |
||
111 | * |
||
112 | * @param Iterator $iterator |
||
113 | * |
||
114 | * @return Generator |
||
115 | */ |
||
116 | 20 | protected function iteratorToGenerator(Iterator $iterator, bool $preserve_offset): Generator |
|
129 | |||
130 | /** |
||
131 | * @inheritdoc |
||
132 | */ |
||
133 | 2 | public function count(): int |
|
137 | |||
138 | /** |
||
139 | * Returns a sequential array of all CSV lines |
||
140 | * |
||
141 | * @return array |
||
142 | */ |
||
143 | 66 | public function fetchAll(): array |
|
147 | |||
148 | /** |
||
149 | * Returns a single row from the CSV |
||
150 | * |
||
151 | * By default if no offset is provided the first row of the CSV is selected |
||
152 | * |
||
153 | * @param int $offset the CSV row offset |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 6 | public function fetchOne(int $offset = 0): array |
|
165 | |||
166 | /** |
||
167 | * Returns the next value from a single CSV column |
||
168 | * |
||
169 | * By default if no column index is provided the first column of the CSV is selected |
||
170 | * |
||
171 | * @param string|int $index CSV column index |
||
172 | * |
||
173 | * @return Generator |
||
174 | */ |
||
175 | 18 | public function fetchColumn($index = 0): Generator |
|
190 | |||
191 | /** |
||
192 | * Filter a column name against the CSV header if any |
||
193 | * |
||
194 | * @param string|int $field the field name or the field index |
||
195 | * @param string $error_message the associated error message |
||
196 | * |
||
197 | * @throws InvalidArgumentException if the field is invalid |
||
198 | * @throws RuntimeException if the column is not found |
||
199 | * |
||
200 | * @return string|int |
||
201 | */ |
||
202 | 26 | protected function getColumnIndex($field, string $error_message) |
|
224 | |||
225 | /** |
||
226 | * Fetches the next key-value pairs from a result set (first |
||
227 | * column is the key, second column is the value). |
||
228 | * |
||
229 | * By default if no column index is provided: |
||
230 | * - the first CSV column is used to provide the keys |
||
231 | * - the second CSV column is used to provide the value |
||
232 | * |
||
233 | * @param string|int $offset_index The column index to serve as offset |
||
234 | * @param string|int $value_index The column index to serve as value |
||
235 | * |
||
236 | * @return Generator |
||
237 | */ |
||
238 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
257 | |||
258 | /** |
||
259 | * Whether we should preserve the CSV document record offset. |
||
260 | * |
||
261 | * If set to true CSV document record offset will added to |
||
262 | * method output where it makes sense. |
||
263 | * |
||
264 | * @param bool $status |
||
265 | * |
||
266 | * @return static |
||
267 | */ |
||
268 | 6 | public function preserveRecordOffset(bool $status) |
|
274 | } |
||
275 |