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 | 2 | /** |
|
67 | * Returns the record offset used as header |
||
68 | 2 | * |
|
69 | * If no CSV record is used this method MUST return null |
||
70 | * |
||
71 | * @return int|null |
||
72 | */ |
||
73 | public function getHeaderOffset() |
||
77 | |||
78 | 2 | /** |
|
79 | * Returns wether the selected header can be combine to each record |
||
80 | 2 | * |
|
81 | * A valid header must be empty or contains unique string field names |
||
82 | * |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function supportsHeaderAsRecordKeys(): bool |
||
91 | |||
92 | /** |
||
93 | * Returns the CSV record header |
||
94 | 6 | * |
|
95 | * The returned header is represented as an array of string values |
||
96 | * |
||
97 | 4 | * @return string[] |
|
98 | 3 | */ |
|
99 | 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 | 2 | * @return string[] |
|
124 | */ |
||
125 | protected function setHeader(int $offset): array |
||
141 | 18 | ||
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 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
||
164 | |||
165 | /** |
||
166 | * Returns the record padding value |
||
167 | 2 | * |
|
168 | 1 | * @return mixed |
|
169 | */ |
||
170 | 2 | public function getRecordPaddingValue() |
|
174 | 2 | ||
175 | 2 | /** |
|
176 | * Returns a CSV records collection |
||
177 | * |
||
178 | * @param Statement $stmt |
||
179 | * |
||
180 | * @return ResultSet |
||
181 | */ |
||
182 | public function select(Statement $stmt): ResultSet |
||
186 | 2 | ||
187 | /** |
||
188 | 2 | * Detect Delimiters occurences in the CSV |
|
189 | * |
||
190 | 2 | * 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 | 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 | 6 | */ |
|
227 | protected function getCellCount(string $delimiter, int $nb_records): int |
||
239 | |||
240 | 4 | /** |
|
241 | * @inheritdoc |
||
242 | */ |
||
243 | public function getIterator(): Iterator |
||
247 | |||
248 | /** |
||
249 | * Returns the CSV records in an iterator object. |
||
250 | 4 | * |
|
251 | * Each CSV record is represented as a simple array of string or null values. |
||
252 | 4 | * |
|
253 | 2 | * 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 | 4 | * filled with null values while extra record fields are strip from |
|
258 | 4 | * the returned object. |
|
259 | * |
||
260 | 4 | * @throws RuntimeException If the header contains non unique column name |
|
261 | 4 | * |
|
262 | * @return Iterator |
||
263 | 4 | */ |
|
264 | 4 | public function getRecords(): Iterator |
|
279 | |||
280 | /** |
||
281 | 10 | * Add the CSV header if present and valid |
|
282 | * |
||
283 | 10 | * @param Iterator $iterator |
|
284 | 4 | * |
|
285 | * @return Iterator |
||
286 | */ |
||
287 | 6 | protected function combineHeader(Iterator $iterator): Iterator |
|
309 | |||
310 | 8 | /** |
|
311 | 2 | * Strip the BOM sequence if present |
|
312 | * |
||
313 | * @param Iterator $iterator |
||
314 | 6 | * @param string $bom |
|
315 | 6 | * |
|
316 | 2 | * @return Iterator |
|
317 | */ |
||
318 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
||
335 | 2 | ||
336 | |||
337 | /** |
||
338 | 2 | * Selects the record to be used as the CSV header |
|
339 | 2 | * |
|
340 | 2 | * Because of the header is represented as an array, to be valid |
|
341 | * a header MUST contain only unique string value. |
||
342 | * |
||
343 | 2 | * @param int|null $offset the header record offset |
|
344 | * |
||
345 | * @return static |
||
346 | */ |
||
347 | 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 | public function setRecordPaddingValue($record_padding_value): self |
||
374 | |||
375 | /** |
||
376 | * @inheritdoc |
||
377 | */ |
||
378 | protected function resetProperties() |
||
382 | } |
||
383 |