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 | 18 | public function __construct(Iterator $records, array $header) |
|
57 | |||
58 | /** |
||
59 | * {@inheritdoc} |
||
60 | */ |
||
61 | 21 | public function __destruct() |
|
65 | |||
66 | /** |
||
67 | * Returns the header associated with the result set. |
||
68 | * |
||
69 | * @return string[] |
||
70 | */ |
||
71 | 3 | public function getHeader(): array |
|
75 | |||
76 | /** |
||
77 | * {@inheritdoc} |
||
78 | */ |
||
79 | 24 | public function getIterator(): Generator |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 18 | public function getRecords(array $header = []): Generator |
|
94 | |||
95 | /** |
||
96 | * Combine the header to each record if present. |
||
97 | */ |
||
98 | 15 | protected function combineHeader(array $header): Iterator |
|
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | 6 | public function count(): int |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 3 | public function jsonSerialize(): array |
|
134 | |||
135 | /** |
||
136 | * Returns the nth record from the result set. |
||
137 | * |
||
138 | * By default if no index is provided the first record of the resultet is returned |
||
139 | * |
||
140 | * @param int $nth_record the CSV record offset |
||
141 | * |
||
142 | * @throws Exception if argument is lesser than 0 |
||
143 | */ |
||
144 | 6 | public function fetchOne(int $nth_record = 0): array |
|
155 | |||
156 | /** |
||
157 | * Returns a single column from the next record of the result set. |
||
158 | * |
||
159 | * By default if no value is supplied the first column is fetch |
||
160 | * |
||
161 | * @param string|int $index CSV column index |
||
162 | */ |
||
163 | 21 | public function fetchColumn($index = 0): Generator |
|
179 | |||
180 | /** |
||
181 | * Filter a column name against the header if any. |
||
182 | * |
||
183 | * @param string|int $field the field name or the field index |
||
184 | * @param string $error_message the associated error message |
||
185 | * |
||
186 | * @return string|int |
||
187 | */ |
||
188 | 30 | protected function getColumnIndex($field, string $error_message) |
|
196 | |||
197 | /** |
||
198 | * Returns the selected column name. |
||
199 | * |
||
200 | * @throws Exception if the column is not found |
||
201 | */ |
||
202 | 6 | protected function getColumnIndexByValue(string $value, string $error_message): string |
|
210 | |||
211 | /** |
||
212 | * Returns the selected column name according to its offset. |
||
213 | * |
||
214 | * @throws Exception if the field is invalid or not found |
||
215 | * |
||
216 | * @return int|string |
||
217 | */ |
||
218 | 18 | protected function getColumnIndexByKey(int $index, string $error_message) |
|
235 | |||
236 | /** |
||
237 | * Returns the next key-value pairs from a result set (first |
||
238 | * column is the key, second column is the value). |
||
239 | * |
||
240 | * By default if no column index is provided: |
||
241 | * - the first column is used to provide the keys |
||
242 | * - the second column is used to provide the value |
||
243 | * |
||
244 | * @param string|int $offset_index The column index to serve as offset |
||
245 | * @param string|int $value_index The column index to serve as value |
||
246 | */ |
||
247 | 12 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
265 | } |
||
266 |