1 | <?php |
||
36 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
37 | { |
||
38 | /** |
||
39 | * CSV Document header offset |
||
40 | * |
||
41 | * @var int|null |
||
42 | */ |
||
43 | protected $header_offset; |
||
44 | |||
45 | /** |
||
46 | * CSV Document Header record |
||
47 | * |
||
48 | * @var string[] |
||
49 | */ |
||
50 | protected $header = []; |
||
51 | |||
52 | /** |
||
53 | * Tell whether the CSV records offset must be kept on Json serialization |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $json_preserve_offset = false; |
||
58 | |||
59 | /** |
||
60 | * Records count |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | protected $nb_records = -1; |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
70 | |||
71 | /** |
||
72 | * Returns the record offset used as header |
||
73 | * |
||
74 | * If no CSV record is used this method MUST return null |
||
75 | * |
||
76 | * @return int|null |
||
77 | */ |
||
78 | 10 | public function getHeaderOffset() |
|
82 | |||
83 | /** |
||
84 | * Returns the CSV record header |
||
85 | * |
||
86 | * The returned header is represented as an array of string values |
||
87 | * |
||
88 | * @return string[] |
||
89 | */ |
||
90 | 10 | public function getHeader(): array |
|
104 | |||
105 | /** |
||
106 | * Determine the CSV record header |
||
107 | * |
||
108 | * @param int $offset |
||
109 | * |
||
110 | * @throws RuntimeException If the header offset is an integer |
||
111 | * and the corresponding record is missing |
||
112 | * or is an empty array |
||
113 | * |
||
114 | * @return string[] |
||
115 | */ |
||
116 | 6 | protected function setHeader(int $offset): array |
|
131 | |||
132 | /** |
||
133 | * Strip the BOM sequence from a record |
||
134 | * |
||
135 | * @param string[] $record |
||
136 | * @param int $bom_length |
||
137 | * @param string $enclosure |
||
138 | * |
||
139 | * @return string[] |
||
140 | */ |
||
141 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
154 | |||
155 | /** |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | 6 | public function __call($method, array $arguments) |
|
167 | |||
168 | /** |
||
169 | * @inheritdoc |
||
170 | */ |
||
171 | 2 | public function count(): int |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 2 | public function getIterator(): Iterator |
|
187 | |||
188 | /** |
||
189 | * @inheritdoc |
||
190 | */ |
||
191 | 2 | public function jsonSerialize(): array |
|
195 | |||
196 | /** |
||
197 | * Returns the CSV records in an iterator object. |
||
198 | * |
||
199 | * Each CSV record is represented as a simple array of string or null values. |
||
200 | * |
||
201 | * If the CSV document has a header record then each record is combined |
||
202 | * to each header record and the header record is removed from the iterator. |
||
203 | * |
||
204 | * If the CSV document is inconsistent. Missing record fields are |
||
205 | * filled with null values while extra record fields are strip from |
||
206 | * the returned object. |
||
207 | * |
||
208 | * @param string[] $header an optional header to use instead of the CSV document header |
||
209 | * |
||
210 | * @return Iterator |
||
211 | */ |
||
212 | 12 | public function getRecords(array $header = []): Iterator |
|
231 | |||
232 | /** |
||
233 | * Returns the header to be used for iteration |
||
234 | * |
||
235 | * @param string[] $header |
||
236 | * |
||
237 | * @throws RuntimeException If the header contains non unique column name |
||
238 | * |
||
239 | * @return string[] |
||
240 | */ |
||
241 | 16 | protected function computeHeader(array $header) |
|
253 | |||
254 | /** |
||
255 | * Add the CSV header if present and valid |
||
256 | * |
||
257 | * @param Iterator $iterator |
||
258 | * @param string[] $header |
||
259 | * |
||
260 | * @return Iterator |
||
261 | */ |
||
262 | 20 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
279 | |||
280 | /** |
||
281 | * Strip the BOM sequence if present |
||
282 | * |
||
283 | * @param Iterator $iterator |
||
284 | * @param string $bom |
||
285 | * |
||
286 | * @return Iterator |
||
287 | */ |
||
288 | 16 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
305 | |||
306 | /** |
||
307 | * Selects the record to be used as the CSV header |
||
308 | * |
||
309 | * Because of the header is represented as an array, to be valid |
||
310 | * a header MUST contain only unique string value. |
||
311 | * |
||
312 | * @param int|null $offset the header record offset |
||
313 | * |
||
314 | * @return static |
||
315 | */ |
||
316 | 10 | public function setHeaderOffset($offset): self |
|
326 | |||
327 | /** |
||
328 | * Whether we should preserve the CSV document record offset on json serialization. |
||
329 | * |
||
330 | * If set to true CSV document record offset will be preserve when calling jsonSerialize |
||
331 | * |
||
332 | * @param bool $status |
||
333 | * |
||
334 | * @return self |
||
335 | */ |
||
336 | 2 | public function jsonPreserveOffset(bool $status): self |
|
342 | |||
343 | /** |
||
344 | * @inheritdoc |
||
345 | */ |
||
346 | 8 | protected function resetProperties() |
|
351 | } |
||
352 |