1 | <?php |
||
33 | class ResultSet implements Countable, IteratorAggregate, JsonSerializable |
||
34 | { |
||
35 | /** |
||
36 | * The CSV records collection. |
||
37 | * |
||
38 | * @var Iterator |
||
39 | */ |
||
40 | protected $records; |
||
41 | |||
42 | /** |
||
43 | * The CSV records collection header. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | protected $header = []; |
||
48 | |||
49 | /** |
||
50 | * New instance. |
||
51 | */ |
||
52 | 15 | public function __construct(Iterator $records, array $header) |
|
58 | |||
59 | /** |
||
60 | * @throws SyntaxError if the header syntax is invalid |
||
61 | */ |
||
62 | 15 | protected function validateHeader(array $header): void |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 18 | public function __destruct() |
|
76 | |||
77 | /** |
||
78 | * Returns the header associated with the result set. |
||
79 | * |
||
80 | * @return string[] |
||
81 | */ |
||
82 | 3 | public function getHeader(): array |
|
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | 21 | public function getIterator(): Generator |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 18 | public function getRecords(array $header = []): Generator |
|
106 | |||
107 | /** |
||
108 | * Combine the header to each record if present. |
||
109 | */ |
||
110 | 15 | protected function combineHeader(array $header): Iterator |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 3 | public function count(): int |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 3 | public function jsonSerialize(): array |
|
146 | |||
147 | /** |
||
148 | * Returns the nth record from the result set. |
||
149 | * |
||
150 | * By default if no index is provided the first record of the resultet is returned |
||
151 | * |
||
152 | * @param int $nth_record the CSV record offset |
||
153 | * |
||
154 | * @throws Exception if argument is lesser than 0 |
||
155 | */ |
||
156 | 6 | public function fetchOne(int $nth_record = 0): array |
|
167 | |||
168 | /** |
||
169 | * Returns a single column from the next record of the result set. |
||
170 | * |
||
171 | * By default if no value is supplied the first column is fetch |
||
172 | * |
||
173 | * @param string|int $index CSV column index |
||
174 | */ |
||
175 | 21 | public function fetchColumn($index = 0): Generator |
|
191 | |||
192 | /** |
||
193 | * Filter a column name against the header if any. |
||
194 | * |
||
195 | * @param string|int $field the field name or the field index |
||
196 | * @param string $error_message the associated error message |
||
197 | * |
||
198 | * @return string|int |
||
199 | */ |
||
200 | 30 | protected function getColumnIndex($field, string $error_message) |
|
208 | |||
209 | /** |
||
210 | * Returns the selected column name. |
||
211 | * |
||
212 | * @throws Exception if the column is not found |
||
213 | */ |
||
214 | 6 | protected function getColumnIndexByValue(string $value, string $error_message): string |
|
222 | |||
223 | /** |
||
224 | * Returns the selected column name according to its offset. |
||
225 | * |
||
226 | * @throws Exception if the field is invalid or not found |
||
227 | * |
||
228 | * @return int|string |
||
229 | */ |
||
230 | 18 | protected function getColumnIndexByKey(int $index, string $error_message) |
|
247 | |||
248 | /** |
||
249 | * Returns the next key-value pairs from a result set (first |
||
250 | * column is the key, second column is the value). |
||
251 | * |
||
252 | * By default if no column index is provided: |
||
253 | * - the first column is used to provide the keys |
||
254 | * - the second column is used to provide the value |
||
255 | * |
||
256 | * @param string|int $offset_index The column index to serve as offset |
||
257 | * @param string|int $value_index The column index to serve as value |
||
258 | */ |
||
259 | 12 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
277 | } |
||
278 |