1 | <?php |
||
31 | class Reader extends AbstractCsv implements IteratorAggregate |
||
32 | { |
||
33 | /** |
||
34 | * @inheritdoc |
||
35 | */ |
||
36 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
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 header needs to be re-generated |
||
54 | * |
||
55 | * @var bool |
||
56 | */ |
||
57 | protected $is_header_loaded = false; |
||
58 | |||
59 | /** |
||
60 | * The value to pad if the record is less than header size. |
||
61 | * |
||
62 | * @var mixed |
||
63 | */ |
||
64 | protected $record_padding_value; |
||
65 | |||
66 | /** |
||
67 | * Returns the record offset used as header |
||
68 | * |
||
69 | * If no CSV record is used this method MUST return null |
||
70 | * |
||
71 | * @return int|null |
||
72 | */ |
||
73 | 2 | public function getHeaderOffset() |
|
74 | { |
||
75 | 2 | return $this->header_offset; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Returns wether the selected header can be combine to each record |
||
80 | * |
||
81 | * A valid header must be empty or contains unique string field names |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
86 | { |
||
87 | 2 | $header = $this->getHeader(); |
|
88 | |||
89 | 2 | return empty($header) || $header === array_unique(array_filter($header, 'is_string')); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * Returns the CSV record header |
||
94 | * |
||
95 | * The returned header is represented as an array of string values |
||
96 | * |
||
97 | * @return string[] |
||
98 | */ |
||
99 | 4 | public function getHeader(): array |
|
113 | |||
114 | /** |
||
115 | * Determine the CSV record header |
||
116 | * |
||
117 | * @param int $offset |
||
118 | * |
||
119 | * @throws RuntimeException If the header offset is an integer |
||
120 | * and the corresponding record is missing |
||
121 | * or is an empty array |
||
122 | * |
||
123 | * @return string[] |
||
124 | */ |
||
125 | 6 | protected function setHeader(int $offset): array |
|
141 | |||
142 | /** |
||
143 | * Strip the BOM sequence from a record |
||
144 | * |
||
145 | * @param string[] $record |
||
146 | * @param int $bom_length |
||
147 | * @param string $enclosure |
||
148 | * |
||
149 | * @return string[] |
||
150 | */ |
||
151 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
164 | |||
165 | /** |
||
166 | * Returns the record padding value |
||
167 | * |
||
168 | * @return mixed |
||
169 | */ |
||
170 | 2 | public function getRecordPaddingValue() |
|
174 | |||
175 | /** |
||
176 | * Returns a CSV records collection |
||
177 | * |
||
178 | * @param Statement $stmt |
||
179 | * |
||
180 | * @return ResultSet |
||
181 | */ |
||
182 | 2 | public function select(Statement $stmt): ResultSet |
|
186 | |||
187 | /** |
||
188 | * Detect Delimiters occurences in the CSV |
||
189 | * |
||
190 | * Returns a associative array where each key represents |
||
191 | * a valid delimiter and each value the number of occurences |
||
192 | * |
||
193 | * @param string[] $delimiters the delimiters to consider |
||
194 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
217 | |||
218 | /** |
||
219 | * Returns the cell count for a specified delimiter |
||
220 | * and a specified number of records |
||
221 | * |
||
222 | * @param string $delimiter CSV delimiter |
||
223 | * @param int $nb_records CSV records to consider |
||
224 | * |
||
225 | * @return int |
||
226 | */ |
||
227 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | 18 | public function getIterator(): Iterator |
|
247 | |||
248 | /** |
||
249 | * Returns the CSV records in an iterator object. |
||
250 | * |
||
251 | * Each CSV record is represented as a simple array of string or null values. |
||
252 | * |
||
253 | * If the CSV document has a header record then each record is combined |
||
254 | * to each header record and the header record is removed from the iterator. |
||
255 | * |
||
256 | * If the CSV document is inconsistent. Missing record fields are |
||
257 | * filled with null values while extra record fields are strip from |
||
258 | * the returned object. |
||
259 | * |
||
260 | * @throws RuntimeException If the header contains non unique column name |
||
261 | * |
||
262 | * @return Iterator |
||
263 | */ |
||
264 | 4 | public function getRecords(): Iterator |
|
279 | |||
280 | /** |
||
281 | * Add the CSV header if present and valid |
||
282 | * |
||
283 | * @param Iterator $iterator |
||
284 | * |
||
285 | * @return Iterator |
||
286 | */ |
||
287 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
309 | |||
310 | /** |
||
311 | * Strip the BOM sequence if present |
||
312 | * |
||
313 | * @param Iterator $iterator |
||
314 | * @param string $bom |
||
315 | * |
||
316 | * @return Iterator |
||
317 | */ |
||
318 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
335 | |||
336 | |||
337 | /** |
||
338 | * Selects the record to be used as the CSV header |
||
339 | * |
||
340 | * Because of the header is represented as an array, to be valid |
||
341 | * a header MUST contain only unique string value. |
||
342 | * |
||
343 | * @param int|null $offset the header record offset |
||
344 | * |
||
345 | * @return static |
||
346 | */ |
||
347 | 2 | public function setHeaderOffset($offset): self |
|
360 | |||
361 | /** |
||
362 | * Set the record padding value |
||
363 | * |
||
364 | * @param mixed $record_padding_value |
||
365 | * |
||
366 | * @return static |
||
367 | */ |
||
368 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
374 | |||
375 | /** |
||
376 | * @inheritdoc |
||
377 | */ |
||
378 | 2 | protected function resetProperties() |
|
382 | } |
||
383 |