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 | * Returns a CSV records collection |
||
182 | * |
||
183 | * @param Statement $stmt |
||
184 | * |
||
185 | * @return ResultSet |
||
186 | */ |
||
187 | 2 | public function select(Statement $stmt): ResultSet |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | 6 | public function __call($method, array $arguments) |
|
206 | |||
207 | /** |
||
208 | * Detect Delimiters occurences in the CSV |
||
209 | * |
||
210 | * Returns a associative array where each key represents |
||
211 | * a valid delimiter and each value the number of occurences |
||
212 | * |
||
213 | * @param string[] $delimiters the delimiters to consider |
||
214 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
237 | |||
238 | /** |
||
239 | * Returns the cell count for a specified delimiter |
||
240 | * and a specified number of records |
||
241 | * |
||
242 | * @param string $delimiter CSV delimiter |
||
243 | * @param int $nb_records CSV records to consider |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
259 | |||
260 | /** |
||
261 | * @inheritdoc |
||
262 | */ |
||
263 | 18 | public function getIterator(): Iterator |
|
267 | |||
268 | /** |
||
269 | * Returns the CSV records in an iterator object. |
||
270 | * |
||
271 | * Each CSV record is represented as a simple array of string or null values. |
||
272 | * |
||
273 | * If the CSV document has a header record then each record is combined |
||
274 | * to each header record and the header record is removed from the iterator. |
||
275 | * |
||
276 | * If the CSV document is inconsistent. Missing record fields are |
||
277 | * filled with null values while extra record fields are strip from |
||
278 | * the returned object. |
||
279 | * |
||
280 | * @throws RuntimeException If the header contains non unique column name |
||
281 | * |
||
282 | * @return Iterator |
||
283 | */ |
||
284 | 4 | public function getRecords(): Iterator |
|
299 | |||
300 | /** |
||
301 | * Add the CSV header if present and valid |
||
302 | * |
||
303 | * @param Iterator $iterator |
||
304 | * |
||
305 | * @return Iterator |
||
306 | */ |
||
307 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
329 | |||
330 | /** |
||
331 | * Strip the BOM sequence if present |
||
332 | * |
||
333 | * @param Iterator $iterator |
||
334 | * @param string $bom |
||
335 | * |
||
336 | * @return Iterator |
||
337 | */ |
||
338 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
355 | |||
356 | |||
357 | /** |
||
358 | * Selects the record to be used as the CSV header |
||
359 | * |
||
360 | * Because of the header is represented as an array, to be valid |
||
361 | * a header MUST contain only unique string value. |
||
362 | * |
||
363 | * @param int|null $offset the header record offset |
||
364 | * |
||
365 | * @return static |
||
366 | */ |
||
367 | 2 | public function setHeaderOffset($offset): self |
|
380 | |||
381 | /** |
||
382 | * Set the record padding value |
||
383 | * |
||
384 | * @param mixed $record_padding_value |
||
385 | * |
||
386 | * @return static |
||
387 | */ |
||
388 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
394 | |||
395 | /** |
||
396 | * @inheritdoc |
||
397 | */ |
||
398 | 2 | protected function resetProperties() |
|
402 | } |
||
403 |