1 | <?php |
||
34 | class ResultSet implements Countable, IteratorAggregate, JsonSerializable |
||
35 | { |
||
36 | use ValidatorTrait; |
||
37 | |||
38 | /** |
||
39 | * The CSV records collection |
||
40 | * |
||
41 | * @var Iterator |
||
42 | */ |
||
43 | protected $iterator; |
||
44 | |||
45 | /** |
||
46 | * The CSV records collection column names |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $column_names = []; |
||
51 | |||
52 | /** |
||
53 | * Tell whether the CSV records offset must be kept on output |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $preserve_offset = false; |
||
58 | |||
59 | /** |
||
60 | * New instance |
||
61 | * |
||
62 | * @param Iterator $iterator a CSV records collection iterator |
||
63 | * @param array $column_names the associated collection column names |
||
64 | */ |
||
65 | 8 | public function __construct(Iterator $iterator, array $column_names) |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 10 | public function __destruct() |
|
78 | |||
79 | /** |
||
80 | * Returns the column names associated with the ResultSet |
||
81 | * |
||
82 | * @return string[] |
||
83 | */ |
||
84 | 2 | public function getColumnNames(): array |
|
88 | |||
89 | /** |
||
90 | * Tell whether the CSV document record offset must be kept on output |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | 2 | public function isRecordOffsetPreserved(): bool |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | 2 | public function getIterator(): Generator |
|
106 | |||
107 | /** |
||
108 | * Return the generator depending on the preserveRecordOffset setting |
||
109 | * |
||
110 | * @param Iterator $iterator |
||
111 | * |
||
112 | * @return Generator |
||
113 | */ |
||
114 | 6 | protected function iteratorToGenerator(Iterator $iterator): Generator |
|
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 2 | public function count(): int |
|
135 | |||
136 | /** |
||
137 | * Returns a sequential array of all CSV records found |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | 8 | public function fetchAll(): array |
|
145 | |||
146 | /** |
||
147 | * @inheritdoc |
||
148 | */ |
||
149 | 2 | public function jsonSerialize(): array |
|
153 | |||
154 | /** |
||
155 | * Returns a single record from the CSV |
||
156 | * |
||
157 | * By default if no offset is provided the first row of the CSV is selected |
||
158 | * |
||
159 | * @param int $offset the CSV record offset |
||
160 | * |
||
161 | * @return array |
||
162 | */ |
||
163 | 4 | public function fetchOne(int $offset = 0): array |
|
175 | |||
176 | /** |
||
177 | * Returns the next value from a single CSV record field |
||
178 | * |
||
179 | * By default if no column index is provided the first column of the CSV is selected |
||
180 | * |
||
181 | * @param string|int $index CSV column index |
||
182 | * |
||
183 | * @return Generator |
||
184 | */ |
||
185 | 14 | public function fetchColumn($index = 0): Generator |
|
200 | |||
201 | /** |
||
202 | * Filter a column name against the CSV header if any |
||
203 | * |
||
204 | * @param string|int $field the field name or the field index |
||
205 | * @param string $error_message the associated error message |
||
206 | * |
||
207 | * @return string|int |
||
208 | */ |
||
209 | 20 | protected function getColumnIndex($field, string $error_message) |
|
218 | |||
219 | /** |
||
220 | * Returns the selected column name |
||
221 | * |
||
222 | * @param string $value |
||
223 | * @param string $error_message |
||
224 | * |
||
225 | * @throws RuntimeException if the column is not found |
||
226 | * |
||
227 | * @return string |
||
228 | */ |
||
229 | 4 | protected function getColumnIndexByValue(string $value, string $error_message): string |
|
237 | |||
238 | /** |
||
239 | * Returns the selected column name according to its offset |
||
240 | * |
||
241 | * @param int $index |
||
242 | * @param string $error_message |
||
243 | * |
||
244 | * @throws RuntimeException if the field is invalid or not found |
||
245 | * |
||
246 | * @return int|string |
||
247 | */ |
||
248 | 10 | protected function getColumnIndexByKey(int $index, string $error_message) |
|
262 | |||
263 | /** |
||
264 | * Fetches the next key-value pairs from a result set (first |
||
265 | * column is the key, second column is the value). |
||
266 | * |
||
267 | * By default if no column index is provided: |
||
268 | * - the first CSV column is used to provide the keys |
||
269 | * - the second CSV column is used to provide the value |
||
270 | * |
||
271 | * @param string|int $offset_index The column index to serve as offset |
||
272 | * @param string|int $value_index The column index to serve as value |
||
273 | * |
||
274 | * @return Generator |
||
275 | */ |
||
276 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
294 | |||
295 | /** |
||
296 | * Whether we should preserve the CSV document record offset. |
||
297 | * |
||
298 | * If set to true CSV document record offset will be added to |
||
299 | * method output where it makes sense. |
||
300 | * |
||
301 | * @param bool $status |
||
302 | * |
||
303 | * @return self |
||
304 | */ |
||
305 | 2 | public function preserveRecordOffset(bool $status): self |
|
311 | } |
||
312 |