1 | <?php |
||
36 | class Reader extends AbstractCsv implements IteratorAggregate |
||
37 | { |
||
38 | /** |
||
39 | * @inheritdoc |
||
40 | */ |
||
41 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
42 | |||
43 | /** |
||
44 | * CSV Document header offset |
||
45 | * |
||
46 | * @var int|null |
||
47 | */ |
||
48 | protected $header_offset; |
||
49 | |||
50 | /** |
||
51 | * CSV Document Header record |
||
52 | * |
||
53 | * @var string[] |
||
54 | */ |
||
55 | protected $header = []; |
||
56 | |||
57 | /** |
||
58 | * Tell whether the header needs to be re-generated |
||
59 | * |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $is_header_loaded = false; |
||
63 | |||
64 | /** |
||
65 | * The value to pad if the record is less than header size. |
||
66 | * |
||
67 | * @var mixed |
||
68 | */ |
||
69 | protected $record_padding_value; |
||
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 | 2 | public function getHeaderOffset() |
|
82 | |||
83 | /** |
||
84 | * Returns wether the selected header can be combine to each record |
||
85 | * |
||
86 | * A valid header must be empty or contains unique string field names |
||
87 | * |
||
88 | * @return bool |
||
89 | */ |
||
90 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
96 | |||
97 | /** |
||
98 | * Returns the CSV record header |
||
99 | * |
||
100 | * The returned header is represented as an array of string values |
||
101 | * |
||
102 | * @return string[] |
||
103 | */ |
||
104 | 4 | public function getHeader(): array |
|
118 | |||
119 | /** |
||
120 | * Determine the CSV record header |
||
121 | * |
||
122 | * @param int $offset |
||
123 | * |
||
124 | * @throws RuntimeException If the header offset is an integer |
||
125 | * and the corresponding record is missing |
||
126 | * or is an empty array |
||
127 | * |
||
128 | * @return string[] |
||
129 | */ |
||
130 | 6 | protected function setHeader(int $offset): array |
|
146 | |||
147 | /** |
||
148 | * Strip the BOM sequence from a record |
||
149 | * |
||
150 | * @param string[] $record |
||
151 | * @param int $bom_length |
||
152 | * @param string $enclosure |
||
153 | * |
||
154 | * @return string[] |
||
155 | */ |
||
156 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
169 | |||
170 | /** |
||
171 | * Returns the record padding value |
||
172 | * |
||
173 | * @return mixed |
||
174 | */ |
||
175 | 2 | public function getRecordPaddingValue() |
|
179 | |||
180 | /** |
||
181 | * @inheritdoc |
||
182 | */ |
||
183 | 6 | public function __call($method, array $arguments) |
|
194 | |||
195 | /** |
||
196 | * Detect Delimiters occurences in the CSV |
||
197 | * |
||
198 | * Returns a associative array where each key represents |
||
199 | * a valid delimiter and each value the number of occurences |
||
200 | * |
||
201 | * @param string[] $delimiters the delimiters to consider |
||
202 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
203 | * |
||
204 | * @return array |
||
205 | */ |
||
206 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
225 | |||
226 | /** |
||
227 | * Returns the cell count for a specified delimiter |
||
228 | * and a specified number of records |
||
229 | * |
||
230 | * @param string $delimiter CSV delimiter |
||
231 | * @param int $nb_records CSV records to consider |
||
232 | * |
||
233 | * @return int |
||
234 | */ |
||
235 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
247 | |||
248 | /** |
||
249 | * @inheritdoc |
||
250 | */ |
||
251 | 18 | public function getIterator(): Iterator |
|
255 | |||
256 | /** |
||
257 | * Returns the CSV records in an iterator object. |
||
258 | * |
||
259 | * Each CSV record is represented as a simple array of string or null values. |
||
260 | * |
||
261 | * If the CSV document has a header record then each record is combined |
||
262 | * to each header record and the header record is removed from the iterator. |
||
263 | * |
||
264 | * If the CSV document is inconsistent. Missing record fields are |
||
265 | * filled with null values while extra record fields are strip from |
||
266 | * the returned object. |
||
267 | * |
||
268 | * @throws RuntimeException If the header contains non unique column name |
||
269 | * |
||
270 | * @return Iterator |
||
271 | */ |
||
272 | 4 | public function getRecords(): Iterator |
|
287 | |||
288 | /** |
||
289 | * Add the CSV header if present and valid |
||
290 | * |
||
291 | * @param Iterator $iterator |
||
292 | * |
||
293 | * @return Iterator |
||
294 | */ |
||
295 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
317 | |||
318 | /** |
||
319 | * Strip the BOM sequence if present |
||
320 | * |
||
321 | * @param Iterator $iterator |
||
322 | * @param string $bom |
||
323 | * |
||
324 | * @return Iterator |
||
325 | */ |
||
326 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
343 | |||
344 | |||
345 | /** |
||
346 | * Selects the record to be used as the CSV header |
||
347 | * |
||
348 | * Because of the header is represented as an array, to be valid |
||
349 | * a header MUST contain only unique string value. |
||
350 | * |
||
351 | * @param int|null $offset the header record offset |
||
352 | * |
||
353 | * @return static |
||
354 | */ |
||
355 | 2 | public function setHeaderOffset($offset): self |
|
368 | |||
369 | /** |
||
370 | * Set the record padding value |
||
371 | * |
||
372 | * @param mixed $record_padding_value |
||
373 | * |
||
374 | * @return static |
||
375 | */ |
||
376 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
382 | |||
383 | /** |
||
384 | * @inheritdoc |
||
385 | */ |
||
386 | 2 | protected function resetProperties() |
|
390 | } |
||
391 |