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 Json serialization |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $json_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 |
|
84 | { |
||
85 | 2 | return $this->header; |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | */ |
||
91 | 8 | public function getRecords(): Generator |
|
92 | { |
||
93 | 8 | foreach ($this->records as $value) { |
|
94 | 4 | yield $value; |
|
95 | } |
||
96 | 8 | } |
|
97 | |||
98 | /** |
||
99 | * @inheritdoc |
||
100 | */ |
||
101 | 12 | public function getIterator(): Generator |
|
105 | |||
106 | /** |
||
107 | * @inheritdoc |
||
108 | */ |
||
109 | 2 | public function count(): int |
|
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | 2 | public function jsonSerialize(): array |
|
121 | |||
122 | /** |
||
123 | * Returns a the nth record from the resultset |
||
124 | * |
||
125 | * By default if no index is provided the first record of the resultet is returned |
||
126 | * |
||
127 | * @param int $nth_record the CSV record offset |
||
128 | * |
||
129 | * @throws OutOfRangeException if argument is lesser than 0 |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | 4 | public function fetchOne(int $nth_record = 0): array |
|
144 | |||
145 | /** |
||
146 | * Returns the next value from a single CSV record field |
||
147 | * |
||
148 | * By default if no column index is provided the first column of the CSV is selected |
||
149 | * |
||
150 | * @param string|int $index CSV column index |
||
151 | * |
||
152 | * @return Generator |
||
153 | */ |
||
154 | 14 | public function fetchColumn($index = 0): Generator |
|
170 | |||
171 | /** |
||
172 | * Filter a column name against the CSV header if any |
||
173 | * |
||
174 | * @param string|int $field the field name or the field index |
||
175 | * @param string $error_message the associated error message |
||
176 | * |
||
177 | * @return string|int |
||
178 | */ |
||
179 | 20 | protected function getColumnIndex($field, string $error_message) |
|
188 | |||
189 | /** |
||
190 | * Returns the selected column name |
||
191 | * |
||
192 | * @param string $value |
||
193 | * @param string $error_message |
||
194 | * |
||
195 | * @throws RuntimeException if the column is not found |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | 4 | protected function getColumnIndexByValue(string $value, string $error_message): string |
|
207 | |||
208 | /** |
||
209 | * Returns the selected column name according to its offset |
||
210 | * |
||
211 | * @param int $index |
||
212 | * @param string $error_message |
||
213 | * |
||
214 | * @throws OutOfRangeException if the field index is invalid |
||
215 | * @throws RuntimeException if the field is invalid or not found |
||
216 | * |
||
217 | * @return int|string |
||
218 | */ |
||
219 | 12 | protected function getColumnIndexByKey(int $index, string $error_message) |
|
236 | |||
237 | /** |
||
238 | * Fetches the next key-value pairs from a result set (first |
||
239 | * column is the key, second column is the value). |
||
240 | * |
||
241 | * By default if no column index is provided: |
||
242 | * - the first CSV column is used to provide the keys |
||
243 | * - the second CSV column is used to provide the value |
||
244 | * |
||
245 | * @param string|int $offset_index The column index to serve as offset |
||
246 | * @param string|int $value_index The column index to serve as value |
||
247 | * |
||
248 | * @return Generator |
||
249 | */ |
||
250 | 8 | public function fetchPairs($offset_index = 0, $value_index = 1): Generator |
|
268 | |||
269 | /** |
||
270 | * Whether we should preserve the CSV document record offset on json serialization. |
||
271 | * |
||
272 | * If set to true CSV document record offset will be preserve when calling jsonSerialize |
||
273 | * |
||
274 | * @param bool $status |
||
275 | * |
||
276 | * @return self |
||
277 | */ |
||
278 | 2 | public function jsonPreserveOffset(bool $status): self |
|
284 | } |
||
285 |