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 | 100 | public function __construct(Iterator $iterator, array $column_names) |
|
70 | |||
71 | /** |
||
72 | * @inheritdoc |
||
73 | */ |
||
74 | 100 | public function __destruct() |
|
78 | |||
79 | /** |
||
80 | * Returns the column names associated with the ResultSet |
||
81 | * |
||
82 | * @return string[] |
||
83 | */ |
||
84 | 4 | 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 | 10 | 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 | 18 | protected function iteratorToGenerator(Iterator $iterator): Generator |
|
127 | |||
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 2 | public function count(): int |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 2 | public function jsonSerialize(): array |
|
143 | |||
144 | /** |
||
145 | * Returns a sequential array of all CSV records found |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | 58 | public function fetchAll(): 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 | 6 | public function fetchOne(int $offset = 0): array |
|
171 | |||
172 | /** |
||
173 | * Returns the next value from a single CSV record field |
||
174 | * |
||
175 | * By default if no column index is provided the first column of the CSV is selected |
||
176 | * |
||
177 | * @param string|int $index CSV column index |
||
178 | * |
||
179 | * @return Generator |
||
180 | */ |
||
181 | 14 | public function fetchColumn($index = 0): Generator |
|
196 | |||
197 | /** |
||
198 | * Filter a column name against the CSV header if any |
||
199 | * |
||
200 | * @param string|int $field the field name or the field index |
||
201 | * @param string $error_message the associated error message |
||
202 | * |
||
203 | * @throws RuntimeException if the field is invalid |
||
204 | * @throws RuntimeException if the column is not found |
||
205 | * |
||
206 | * @return string|int |
||
207 | */ |
||
208 | 22 | protected function getColumnIndex($field, string $error_message) |
|
230 | |||
231 | /** |
||
232 | * Fetches the next key-value pairs from a result set (first |
||
233 | * column is the key, second column is the value). |
||
234 | * |
||
235 | * By default if no column index is provided: |
||
236 | * - the first CSV column is used to provide the keys |
||
237 | * - the second CSV column is used to provide the value |
||
238 | * |
||
239 | * @param string|int $offset_index The column index to serve as offset |
||
240 | * @param string|int $value_index The column index to serve as value |
||
241 | * |
||
242 | * @return Generator |
||
243 | */ |
||
244 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
262 | |||
263 | /** |
||
264 | * Whether we should preserve the CSV document record offset. |
||
265 | * |
||
266 | * If set to true CSV document record offset will be added to |
||
267 | * method output where it makes sense. |
||
268 | * |
||
269 | * @param bool $status |
||
270 | * |
||
271 | * @return self |
||
272 | */ |
||
273 | 4 | public function preserveRecordOffset(bool $status): self |
|
279 | } |
||
280 |