1 | <?php |
||
35 | class ResultSet implements Countable, IteratorAggregate, JsonSerializable |
||
36 | { |
||
37 | /** |
||
38 | * The CSV records collection |
||
39 | * |
||
40 | * @var Iterator |
||
41 | */ |
||
42 | protected $records; |
||
43 | |||
44 | /** |
||
45 | * The CSV records collection column names |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $header = []; |
||
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 $records a CSV records collection iterator |
||
62 | * @param array $header the associated collection column names |
||
63 | */ |
||
64 | 8 | public function __construct(Iterator $records, array $header) |
|
69 | |||
70 | /** |
||
71 | * @inheritdoc |
||
72 | */ |
||
73 | 10 | public function __destruct() |
|
77 | |||
78 | /** |
||
79 | * Returns the column names associated with the ResultSet |
||
80 | * |
||
81 | * @return string[] |
||
82 | */ |
||
83 | 2 | public function getHeader(): array |
|
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | */ |
||
91 | 10 | public function getRecords(): Generator |
|
95 | |||
96 | /** |
||
97 | * Return the generator depending on the preserveRecordOffset setting |
||
98 | * |
||
99 | * @param Iterator $iterator |
||
100 | * |
||
101 | * @return Generator |
||
102 | */ |
||
103 | 10 | protected function iteratorToGenerator(Iterator $iterator): Generator |
|
116 | |||
117 | /** |
||
118 | * Tell whether the CSV document record offset must be kept on output |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | 2 | public function isRecordOffsetPreserved(): bool |
|
126 | |||
127 | /** |
||
128 | * @inheritdoc |
||
129 | */ |
||
130 | 12 | public function getIterator(): Generator |
|
134 | |||
135 | /** |
||
136 | * @inheritdoc |
||
137 | */ |
||
138 | 2 | public function count(): int |
|
142 | |||
143 | /** |
||
144 | * @inheritdoc |
||
145 | */ |
||
146 | 2 | public function jsonSerialize(): array |
|
150 | |||
151 | /** |
||
152 | * Returns a the nth record from the resultset |
||
153 | * |
||
154 | * By default if no index is provided the first record of the resultet is returned |
||
155 | * |
||
156 | * @param int $nth_record the CSV record offset |
||
157 | * |
||
158 | * @throws OutOfRangeException if argument is lesser than 0 |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | 4 | public function fetchOne(int $nth_record = 0): array |
|
173 | |||
174 | /** |
||
175 | * Returns the next value from a single CSV record field |
||
176 | * |
||
177 | * By default if no column index is provided the first column of the CSV is selected |
||
178 | * |
||
179 | * @param string|int $index CSV column index |
||
180 | * |
||
181 | * @return Generator |
||
182 | */ |
||
183 | 14 | public function fetchColumn($index = 0): Generator |
|
198 | |||
199 | /** |
||
200 | * Filter a column name against the CSV header if any |
||
201 | * |
||
202 | * @param string|int $field the field name or the field index |
||
203 | * @param string $error_message the associated error message |
||
204 | * |
||
205 | * @return string|int |
||
206 | */ |
||
207 | 20 | protected function getColumnIndex($field, string $error_message) |
|
216 | |||
217 | /** |
||
218 | * Returns the selected column name |
||
219 | * |
||
220 | * @param string $value |
||
221 | * @param string $error_message |
||
222 | * |
||
223 | * @throws RuntimeException if the column is not found |
||
224 | * |
||
225 | * @return string |
||
226 | */ |
||
227 | 4 | protected function getColumnIndexByValue(string $value, string $error_message): string |
|
235 | |||
236 | /** |
||
237 | * Returns the selected column name according to its offset |
||
238 | * |
||
239 | * @param int $index |
||
240 | * @param string $error_message |
||
241 | * |
||
242 | * @throws OutOfRangeException if the field index is invalid |
||
243 | * @throws RuntimeException if the field is invalid or not found |
||
244 | * |
||
245 | * @return int|string |
||
246 | */ |
||
247 | 12 | protected function getColumnIndexByKey(int $index, string $error_message) |
|
264 | |||
265 | /** |
||
266 | * Fetches the next key-value pairs from a result set (first |
||
267 | * column is the key, second column is the value). |
||
268 | * |
||
269 | * By default if no column index is provided: |
||
270 | * - the first CSV column is used to provide the keys |
||
271 | * - the second CSV column is used to provide the value |
||
272 | * |
||
273 | * @param string|int $offset_index The column index to serve as offset |
||
274 | * @param string|int $value_index The column index to serve as value |
||
275 | * |
||
276 | * @return Generator |
||
277 | */ |
||
278 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
296 | |||
297 | /** |
||
298 | * Whether we should preserve the CSV document record offset. |
||
299 | * |
||
300 | * If set to true CSV document record offset will be added to |
||
301 | * method output where it makes sense. |
||
302 | * |
||
303 | * @param bool $status |
||
304 | * |
||
305 | * @return self |
||
306 | */ |
||
307 | 2 | public function preserveRecordOffset(bool $status): self |
|
313 | } |
||
314 |