1 | <?php |
||
37 | class Reader extends AbstractCsv implements Countable, IteratorAggregate |
||
38 | { |
||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
43 | |||
44 | /** |
||
45 | * The value to pad if the record is less than header size. |
||
46 | * |
||
47 | * @var mixed |
||
48 | */ |
||
49 | protected $record_padding_value; |
||
50 | |||
51 | /** |
||
52 | * CSV Document header offset |
||
53 | * |
||
54 | * @var int|null |
||
55 | */ |
||
56 | protected $header_offset; |
||
57 | |||
58 | /** |
||
59 | * CSV Document Header record |
||
60 | * |
||
61 | * @var string[] |
||
62 | */ |
||
63 | protected $header = []; |
||
64 | |||
65 | /** |
||
66 | * Records Iterator |
||
67 | * |
||
68 | * @var Iterator |
||
69 | */ |
||
70 | protected $records; |
||
71 | |||
72 | /** |
||
73 | * Records count |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $nb_records = -1; |
||
78 | |||
79 | /** |
||
80 | * Detect Delimiters occurences in the CSV |
||
81 | * |
||
82 | * Returns a associative array where each key represents |
||
83 | * a valid delimiter and each value the number of occurences |
||
84 | * |
||
85 | * @param string[] $delimiters the delimiters to consider |
||
86 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
87 | * |
||
88 | * @return array |
||
89 | */ |
||
90 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
109 | |||
110 | /** |
||
111 | * Returns the cell count for a specified delimiter |
||
112 | * and a specified number of records |
||
113 | * |
||
114 | * @param string $delimiter CSV delimiter |
||
115 | * @param int $nb_records CSV records to consider |
||
116 | * |
||
117 | * @return int |
||
118 | */ |
||
119 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
131 | |||
132 | /** |
||
133 | * Returns the record padding value |
||
134 | * |
||
135 | * @return mixed |
||
136 | */ |
||
137 | 2 | public function getRecordPaddingValue() |
|
141 | |||
142 | /** |
||
143 | * Returns the record offset used as header |
||
144 | * |
||
145 | * If no CSV record is used this method MUST return null |
||
146 | * |
||
147 | * @return int|null |
||
148 | */ |
||
149 | 2 | public function getHeaderOffset() |
|
153 | |||
154 | /** |
||
155 | * Returns wether the selected header can be combine to each record |
||
156 | * |
||
157 | * A valid header must be empty or contains unique string field names |
||
158 | * |
||
159 | * @return bool |
||
160 | */ |
||
161 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
167 | |||
168 | /** |
||
169 | * Returns the CSV record header |
||
170 | * |
||
171 | * The returned header is represented as an array of string values |
||
172 | * |
||
173 | * @return string[] |
||
174 | */ |
||
175 | 4 | public function getHeader(): array |
|
187 | |||
188 | /** |
||
189 | * Determine the CSV record header |
||
190 | * |
||
191 | * @param int $offset |
||
192 | * |
||
193 | * @throws RuntimeException If the header offset is an integer |
||
194 | * and the corresponding record is missing |
||
195 | * or is an empty array |
||
196 | * |
||
197 | * @return string[] |
||
198 | */ |
||
199 | 6 | protected function setHeader(int $offset): array |
|
215 | |||
216 | /** |
||
217 | * Strip the BOM sequence from a record |
||
218 | * |
||
219 | * @param string[] $record |
||
220 | * @param int $bom_length |
||
221 | * @param string $enclosure |
||
222 | * |
||
223 | * @return string[] |
||
224 | */ |
||
225 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
238 | |||
239 | /** |
||
240 | * @inheritdoc |
||
241 | */ |
||
242 | 6 | public function __call($method, array $arguments) |
|
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | 2 | public function count(): int |
|
263 | |||
264 | /** |
||
265 | * Returns the CSV records in an iterator object. |
||
266 | * |
||
267 | * @return Iterator |
||
268 | */ |
||
269 | 6 | public function getRecords(): Iterator |
|
275 | |||
276 | /** |
||
277 | * Returns the CSV records in an iterator object. |
||
278 | * |
||
279 | * Each CSV record is represented as a simple array of string or null values. |
||
280 | * |
||
281 | * If the CSV document has a header record then each record is combined |
||
282 | * to each header record and the header record is removed from the iterator. |
||
283 | * |
||
284 | * If the CSV document is inconsistent. Missing record fields are |
||
285 | * filled with null values while extra record fields are strip from |
||
286 | * the returned object. |
||
287 | * |
||
288 | * @throws RuntimeException If the header contains non unique column name |
||
289 | * |
||
290 | * @return Iterator |
||
291 | */ |
||
292 | 4 | protected function setRecords(): Iterator |
|
307 | |||
308 | /** |
||
309 | * Add the CSV header if present and valid |
||
310 | * |
||
311 | * @param Iterator $iterator |
||
312 | * |
||
313 | * @return Iterator |
||
314 | */ |
||
315 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
337 | |||
338 | /** |
||
339 | * Strip the BOM sequence if present |
||
340 | * |
||
341 | * @param Iterator $iterator |
||
342 | * @param string $bom |
||
343 | * |
||
344 | * @return Iterator |
||
345 | */ |
||
346 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
363 | |||
364 | /** |
||
365 | * @inheritdoc |
||
366 | */ |
||
367 | 2 | public function getIterator(): Iterator |
|
371 | |||
372 | /** |
||
373 | * Set the record padding value |
||
374 | * |
||
375 | * @param mixed $record_padding_value |
||
376 | * |
||
377 | * @return static |
||
378 | */ |
||
379 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
385 | |||
386 | /** |
||
387 | * Selects the record to be used as the CSV header |
||
388 | * |
||
389 | * Because of the header is represented as an array, to be valid |
||
390 | * a header MUST contain only unique string value. |
||
391 | * |
||
392 | * @param int|null $offset the header record offset |
||
393 | * |
||
394 | * @return static |
||
395 | */ |
||
396 | 2 | public function setHeaderOffset($offset): self |
|
409 | |||
410 | /** |
||
411 | * @inheritdoc |
||
412 | */ |
||
413 | 4 | protected function resetProperties() |
|
419 | } |
||
420 |