1 | <?php |
||
39 | class Reader extends AbstractCsv implements IteratorAggregate |
||
40 | { |
||
41 | /** |
||
42 | * @inheritdoc |
||
43 | */ |
||
44 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
45 | |||
46 | /** |
||
47 | * CSV Document header offset |
||
48 | * |
||
49 | * @var int|null |
||
50 | */ |
||
51 | protected $header_offset; |
||
52 | |||
53 | /** |
||
54 | * CSV Document Header record |
||
55 | * |
||
56 | * @var string[] |
||
57 | */ |
||
58 | protected $header = []; |
||
59 | |||
60 | /** |
||
61 | * Tell whether the header needs to be re-generated |
||
62 | * |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $is_header_loaded = false; |
||
66 | |||
67 | /** |
||
68 | * The value to pad if the record is less than header size. |
||
69 | * |
||
70 | * @var mixed |
||
71 | */ |
||
72 | protected $record_padding_value; |
||
73 | |||
74 | /** |
||
75 | * Returns the record offset used as header |
||
76 | * |
||
77 | * If no CSV record is used this method MUST return null |
||
78 | * |
||
79 | * @return int|null |
||
80 | */ |
||
81 | 2 | public function getHeaderOffset() |
|
85 | |||
86 | /** |
||
87 | * Returns wether the selected header can be combine to each record |
||
88 | * |
||
89 | * A valid header must be empty or contains unique string field names |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
99 | |||
100 | /** |
||
101 | * Returns the CSV record header |
||
102 | * |
||
103 | * The returned header is represented as an array of string values |
||
104 | * |
||
105 | * @return string[] |
||
106 | */ |
||
107 | 4 | public function getHeader(): array |
|
121 | |||
122 | /** |
||
123 | * Determine the CSV record header |
||
124 | * |
||
125 | * @param int $offset |
||
126 | * |
||
127 | * @throws RuntimeException If the header offset is an integer |
||
128 | * and the corresponding record is missing |
||
129 | * or is an empty array |
||
130 | * |
||
131 | * @return string[] |
||
132 | */ |
||
133 | 6 | protected function setHeader(int $offset): array |
|
149 | |||
150 | /** |
||
151 | * Strip the BOM sequence from a record |
||
152 | * |
||
153 | * @param string[] $record |
||
154 | * @param int $bom_length |
||
155 | * @param string $enclosure |
||
156 | * |
||
157 | * @return string[] |
||
158 | */ |
||
159 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
172 | |||
173 | /** |
||
174 | * Returns the record padding value |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | 2 | public function getRecordPaddingValue() |
|
182 | |||
183 | /** |
||
184 | * Returns a CSV records collection |
||
185 | * |
||
186 | * @param Statement $stmt |
||
187 | * |
||
188 | * @return ResultSet |
||
189 | */ |
||
190 | 2 | public function select(Statement $stmt): ResultSet |
|
194 | |||
195 | /** |
||
196 | * @inheritdoc |
||
197 | */ |
||
198 | 2 | public function __call($method, array $arguments) |
|
202 | |||
203 | /** |
||
204 | * Detect Delimiters occurences in the CSV |
||
205 | * |
||
206 | * Returns a associative array where each key represents |
||
207 | * a valid delimiter and each value the number of occurences |
||
208 | * |
||
209 | * @param string[] $delimiters the delimiters to consider |
||
210 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
211 | * |
||
212 | * @return array |
||
213 | */ |
||
214 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
233 | |||
234 | /** |
||
235 | * Returns the cell count for a specified delimiter |
||
236 | * and a specified number of records |
||
237 | * |
||
238 | * @param string $delimiter CSV delimiter |
||
239 | * @param int $nb_records CSV records to consider |
||
240 | * |
||
241 | * @return int |
||
242 | */ |
||
243 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
255 | |||
256 | /** |
||
257 | * @inheritdoc |
||
258 | */ |
||
259 | 18 | public function getIterator(): Iterator |
|
263 | |||
264 | /** |
||
265 | * Returns the CSV records in an iterator object. |
||
266 | * |
||
267 | * Each CSV record is represented as a simple array of string or null values. |
||
268 | * |
||
269 | * If the CSV document has a header record then each record is combined |
||
270 | * to each header record and the header record is removed from the iterator. |
||
271 | * |
||
272 | * If the CSV document is inconsistent. Missing record fields are |
||
273 | * filled with null values while extra record fields are strip from |
||
274 | * the returned object. |
||
275 | * |
||
276 | * @throws RuntimeException If the header contains non unique column name |
||
277 | * |
||
278 | * @return Iterator |
||
279 | */ |
||
280 | 4 | public function getRecords(): Iterator |
|
295 | |||
296 | /** |
||
297 | * Add the CSV header if present and valid |
||
298 | * |
||
299 | * @param Iterator $iterator |
||
300 | * |
||
301 | * @return Iterator |
||
302 | */ |
||
303 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
325 | |||
326 | /** |
||
327 | * Strip the BOM sequence if present |
||
328 | * |
||
329 | * @param Iterator $iterator |
||
330 | * @param string $bom |
||
331 | * |
||
332 | * @return Iterator |
||
333 | */ |
||
334 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
351 | |||
352 | |||
353 | /** |
||
354 | * Selects the record to be used as the CSV header |
||
355 | * |
||
356 | * Because of the header is represented as an array, to be valid |
||
357 | * a header MUST contain only unique string value. |
||
358 | * |
||
359 | * @param int|null $offset the header record offset |
||
360 | * |
||
361 | * @return static |
||
362 | */ |
||
363 | 2 | public function setHeaderOffset($offset): self |
|
376 | |||
377 | /** |
||
378 | * Set the record padding value |
||
379 | * |
||
380 | * @param mixed $record_padding_value |
||
381 | * |
||
382 | * @return static |
||
383 | */ |
||
384 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
390 | |||
391 | /** |
||
392 | * @inheritdoc |
||
393 | */ |
||
394 | 2 | protected function resetProperties() |
|
398 | } |
||
399 |