1 | <?php |
||
33 | class ResultSet implements Countable, IteratorAggregate |
||
34 | { |
||
35 | use ValidatorTrait; |
||
36 | |||
37 | /** |
||
38 | * The CSV records collection |
||
39 | * |
||
40 | * @var Iterator |
||
41 | */ |
||
42 | protected $iterator; |
||
43 | |||
44 | /** |
||
45 | * The CSV records collection column names |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $column_names = []; |
||
50 | |||
51 | /** |
||
52 | * Tell whether the CSV records offset must be kept on output |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $preserve_offset = false; |
||
57 | |||
58 | /** |
||
59 | * New instance |
||
60 | * |
||
61 | * @param Iterator $iterator a CSV records collection iterator |
||
62 | * @param array $column_names the associated collection column names |
||
63 | */ |
||
64 | 2 | public function __construct(Iterator $iterator, array $column_names) |
|
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 6 | public function __destruct() |
|
77 | |||
78 | /** |
||
79 | * Returns the column names associated with the ResultSet |
||
80 | * |
||
81 | * @return string[] |
||
82 | */ |
||
83 | 4 | public function getColumnNames(): array |
|
87 | |||
88 | /** |
||
89 | * Tell whether the CSV document record offset must be kept on output |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 2 | public function isRecordOffsetPreserved(): bool |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 2 | public function getIterator(): Generator |
|
105 | |||
106 | /** |
||
107 | * Return the generator depending on the preserveRecordOffset setting |
||
108 | * |
||
109 | * @param Iterator $iterator |
||
110 | * |
||
111 | * @return Generator |
||
112 | */ |
||
113 | 6 | protected function iteratorToGenerator(Iterator $iterator): Generator |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 2 | public function count(): int |
|
134 | |||
135 | /** |
||
136 | * Returns a sequential array of all CSV records found |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 2 | public function fetchAll(): array |
|
144 | |||
145 | /** |
||
146 | * Returns a single record from the CSV |
||
147 | * |
||
148 | * By default if no offset is provided the first row of the CSV is selected |
||
149 | * |
||
150 | * @param int $offset the CSV record offset |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | 4 | public function fetchOne(int $offset = 0): array |
|
162 | |||
163 | /** |
||
164 | * Returns the next value from a single CSV record field |
||
165 | * |
||
166 | * By default if no column index is provided the first column of the CSV is selected |
||
167 | * |
||
168 | * @param string|int $index CSV column index |
||
169 | * |
||
170 | * @return Generator |
||
171 | */ |
||
172 | 14 | public function fetchColumn($index = 0): Generator |
|
187 | |||
188 | /** |
||
189 | * Filter a column name against the CSV header if any |
||
190 | * |
||
191 | * @param string|int $field the field name or the field index |
||
192 | * @param string $error_message the associated error message |
||
193 | * |
||
194 | * @return string|int |
||
195 | */ |
||
196 | 20 | protected function getColumnIndex($field, string $error_message) |
|
205 | |||
206 | /** |
||
207 | * Returns the selected column name |
||
208 | * |
||
209 | * @param string $field |
||
210 | * @param string $error_message |
||
211 | * |
||
212 | * @throws RuntimeException if the column is not found |
||
213 | * |
||
214 | * @return string |
||
215 | */ |
||
216 | 4 | protected function getColumnIndexByValue(string $field, string $error_message) |
|
224 | |||
225 | /** |
||
226 | * Returns the selected column name according to its offset |
||
227 | * |
||
228 | * @param int $offset |
||
229 | * @param string $error_message |
||
230 | * |
||
231 | * @throws RuntimeException if the field is invalid or not found |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | 10 | protected function getColumnIndexByKey(int $offset, string $error_message) |
|
249 | |||
250 | /** |
||
251 | * Fetches the next key-value pairs from a result set (first |
||
252 | * column is the key, second column is the value). |
||
253 | * |
||
254 | * By default if no column index is provided: |
||
255 | * - the first CSV column is used to provide the keys |
||
256 | * - the second CSV column is used to provide the value |
||
257 | * |
||
258 | * @param string|int $offset_index The column index to serve as offset |
||
259 | * @param string|int $value_index The column index to serve as value |
||
260 | * |
||
261 | * @return Generator |
||
262 | */ |
||
263 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
281 | |||
282 | /** |
||
283 | * Whether we should preserve the CSV document record offset. |
||
284 | * |
||
285 | * If set to true CSV document record offset will be added to |
||
286 | * method output where it makes sense. |
||
287 | * |
||
288 | * @param bool $status |
||
289 | * |
||
290 | * @return self |
||
291 | */ |
||
292 | 2 | public function preserveRecordOffset(bool $status): self |
|
298 | } |
||
299 |