1 | <?php |
||
39 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
40 | { |
||
41 | /** |
||
42 | * CSV Document header offset |
||
43 | * |
||
44 | * @var int|null |
||
45 | */ |
||
46 | protected $header_offset; |
||
47 | |||
48 | /** |
||
49 | * CSV Document Header record |
||
50 | * |
||
51 | * @var string[] |
||
52 | */ |
||
53 | protected $header = []; |
||
54 | |||
55 | /** |
||
56 | * Records count |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $nb_records = -1; |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
66 | |||
67 | /** |
||
68 | * Returns the record offset used as header |
||
69 | * |
||
70 | * If no CSV record is used this method MUST return null |
||
71 | * |
||
72 | * @return int|null |
||
73 | */ |
||
74 | 10 | public function getHeaderOffset() |
|
78 | |||
79 | /** |
||
80 | * Returns the CSV record header |
||
81 | * |
||
82 | * The returned header is represented as an array of string values |
||
83 | * |
||
84 | * @return string[] |
||
85 | */ |
||
86 | 10 | public function getHeader(): array |
|
100 | |||
101 | /** |
||
102 | * Determine the CSV record header |
||
103 | * |
||
104 | * @param int $offset |
||
105 | * |
||
106 | * @throws RuntimeException If the header offset is an integer |
||
107 | * and the corresponding record is missing |
||
108 | * or is an empty array |
||
109 | * |
||
110 | * @return string[] |
||
111 | */ |
||
112 | 6 | protected function setHeader(int $offset): array |
|
127 | |||
128 | /** |
||
129 | * Strip the BOM sequence from a record |
||
130 | * |
||
131 | * @param string[] $record |
||
132 | * @param int $bom_length |
||
133 | * @param string $enclosure |
||
134 | * |
||
135 | * @return string[] |
||
136 | */ |
||
137 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
150 | |||
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 6 | public function __call($method, array $arguments) |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 2 | public function count(): int |
|
175 | |||
176 | /** |
||
177 | * @inheritdoc |
||
178 | */ |
||
179 | 2 | public function getIterator(): Iterator |
|
183 | |||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | 2 | public function jsonSerialize(): array |
|
191 | |||
192 | /** |
||
193 | * Returns the CSV records in an iterator object. |
||
194 | * |
||
195 | * Each CSV record is represented as a simple array of string or null values. |
||
196 | * |
||
197 | * If the CSV document has a header record then each record is combined |
||
198 | * to each header record and the header record is removed from the iterator. |
||
199 | * |
||
200 | * If the CSV document is inconsistent. Missing record fields are |
||
201 | * filled with null values while extra record fields are strip from |
||
202 | * the returned object. |
||
203 | * |
||
204 | * @param string[] $header an optional header to use instead of the CSV document header |
||
205 | * |
||
206 | * @return Iterator |
||
207 | */ |
||
208 | 12 | public function getRecords(array $header = []): Iterator |
|
227 | |||
228 | /** |
||
229 | * Returns the header to be used for iteration |
||
230 | * |
||
231 | * @param string[] $header |
||
232 | * |
||
233 | * @throws RuntimeException If the header contains non unique column name |
||
234 | * |
||
235 | * @return string[] |
||
236 | */ |
||
237 | 16 | protected function computeHeader(array $header) |
|
249 | |||
250 | /** |
||
251 | * Add the CSV header if present and valid |
||
252 | * |
||
253 | * @param Iterator $iterator |
||
254 | * @param string[] $header |
||
255 | * |
||
256 | * @return Iterator |
||
257 | */ |
||
258 | 20 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
275 | |||
276 | /** |
||
277 | * Strip the BOM sequence if present |
||
278 | * |
||
279 | * @param Iterator $iterator |
||
280 | * @param string $bom |
||
281 | * |
||
282 | * @return Iterator |
||
283 | */ |
||
284 | 16 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
301 | |||
302 | /** |
||
303 | * Selects the record to be used as the CSV header |
||
304 | * |
||
305 | * Because of the header is represented as an array, to be valid |
||
306 | * a header MUST contain only unique string value. |
||
307 | * |
||
308 | * @param int|null $offset the header record offset |
||
309 | * |
||
310 | * @return static |
||
311 | */ |
||
312 | 10 | public function setHeaderOffset($offset): self |
|
322 | |||
323 | /** |
||
324 | * Filter Header Offset |
||
325 | * |
||
326 | * @param int|null $offset |
||
327 | * |
||
328 | * @throws OutOfRangeException if header offset is not a positive integer or null |
||
329 | */ |
||
330 | 12 | protected function filterHeaderOffset($offset) |
|
340 | |||
341 | /** |
||
342 | * @inheritdoc |
||
343 | */ |
||
344 | 2 | protected function resetProperties() |
|
349 | } |
||
350 |