1 | <?php |
||
36 | class Reader extends AbstractCsv implements Countable, IteratorAggregate |
||
37 | { |
||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
42 | |||
43 | /** |
||
44 | * The value to pad if the record is less than header size. |
||
45 | * |
||
46 | * @var mixed |
||
47 | */ |
||
48 | protected $record_padding_value; |
||
49 | |||
50 | /** |
||
51 | * CSV Document header offset |
||
52 | * |
||
53 | * @var int|null |
||
54 | */ |
||
55 | protected $header_offset; |
||
56 | |||
57 | /** |
||
58 | * CSV Document Header record |
||
59 | * |
||
60 | * @var string[] |
||
61 | */ |
||
62 | protected $header = []; |
||
63 | |||
64 | /** |
||
65 | * Records count |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $nb_records = -1; |
||
70 | |||
71 | /** |
||
72 | * Returns the record padding value |
||
73 | * |
||
74 | * @return mixed |
||
75 | */ |
||
76 | 2 | public function getRecordPaddingValue() |
|
80 | |||
81 | /** |
||
82 | * Returns the record offset used as header |
||
83 | * |
||
84 | * If no CSV record is used this method MUST return null |
||
85 | * |
||
86 | * @return int|null |
||
87 | */ |
||
88 | 2 | public function getHeaderOffset() |
|
92 | |||
93 | /** |
||
94 | * Returns the CSV record header |
||
95 | * |
||
96 | * The returned header is represented as an array of string values |
||
97 | * |
||
98 | * @return string[] |
||
99 | */ |
||
100 | 4 | public function getHeader(): array |
|
112 | |||
113 | /** |
||
114 | * Determine the CSV record header |
||
115 | * |
||
116 | * @param int $offset |
||
117 | * |
||
118 | * @throws RuntimeException If the header offset is an integer |
||
119 | * and the corresponding record is missing |
||
120 | * or is an empty array |
||
121 | * |
||
122 | * @return string[] |
||
123 | */ |
||
124 | 6 | protected function setHeader(int $offset): array |
|
140 | |||
141 | /** |
||
142 | * Strip the BOM sequence from a record |
||
143 | * |
||
144 | * @param string[] $record |
||
145 | * @param int $bom_length |
||
146 | * @param string $enclosure |
||
147 | * |
||
148 | * @return string[] |
||
149 | */ |
||
150 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 6 | public function __call($method, array $arguments) |
|
176 | |||
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 2 | public function count(): int |
|
188 | |||
189 | /** |
||
190 | * @inheritdoc |
||
191 | */ |
||
192 | 2 | public function getIterator(): Iterator |
|
196 | |||
197 | /** |
||
198 | * Returns the CSV records in an iterator object. |
||
199 | * |
||
200 | * Each CSV record is represented as a simple array of string or null values. |
||
201 | * |
||
202 | * If the CSV document has a header record then each record is combined |
||
203 | * to each header record and the header record is removed from the iterator. |
||
204 | * |
||
205 | * If the CSV document is inconsistent. Missing record fields are |
||
206 | * filled with null values while extra record fields are strip from |
||
207 | * the returned object. |
||
208 | * |
||
209 | * @param array $header |
||
210 | * |
||
211 | * @return Iterator |
||
212 | */ |
||
213 | 6 | public function getRecords(array $header = []): Iterator |
|
232 | |||
233 | /** |
||
234 | * Returns the header to be used for iteration |
||
235 | * |
||
236 | * @param string[] $header |
||
237 | * |
||
238 | * @throws RuntimeException If the header contains non unique column name |
||
239 | * |
||
240 | * @return string[] |
||
241 | */ |
||
242 | 10 | protected function computeHeader(array $header) |
|
254 | |||
255 | /** |
||
256 | * Add the CSV header if present and valid |
||
257 | * |
||
258 | * @param Iterator $iterator |
||
259 | * @param array $header |
||
260 | * |
||
261 | * @return Iterator |
||
262 | */ |
||
263 | 14 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
280 | |||
281 | /** |
||
282 | * Strip the BOM sequence if present |
||
283 | * |
||
284 | * @param Iterator $iterator |
||
285 | * @param string $bom |
||
286 | * |
||
287 | * @return Iterator |
||
288 | */ |
||
289 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
306 | |||
307 | /** |
||
308 | * Set the record padding value |
||
309 | * |
||
310 | * @param mixed $record_padding_value |
||
311 | * |
||
312 | * @return static |
||
313 | */ |
||
314 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
320 | |||
321 | /** |
||
322 | * Selects the record to be used as the CSV header |
||
323 | * |
||
324 | * Because of the header is represented as an array, to be valid |
||
325 | * a header MUST contain only unique string value. |
||
326 | * |
||
327 | * @param int|null $offset the header record offset |
||
328 | * |
||
329 | * @return static |
||
330 | */ |
||
331 | 2 | public function setHeaderOffset($offset): self |
|
344 | |||
345 | /** |
||
346 | * @inheritdoc |
||
347 | */ |
||
348 | 2 | protected function resetProperties() |
|
353 | } |
||
354 |