1 | <?php |
||
37 | class Reader extends AbstractCsv implements Countable, IteratorAggregate |
||
38 | { |
||
39 | /** |
||
40 | * @inheritdoc |
||
41 | */ |
||
42 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
43 | |||
44 | /** |
||
45 | * The value to pad if the record is less than header size. |
||
46 | * |
||
47 | * @var mixed |
||
48 | */ |
||
49 | protected $record_padding_value; |
||
50 | |||
51 | /** |
||
52 | * CSV Document header offset |
||
53 | * |
||
54 | * @var int|null |
||
55 | */ |
||
56 | protected $header_offset; |
||
57 | |||
58 | /** |
||
59 | * CSV Document Header record |
||
60 | * |
||
61 | * @var string[] |
||
62 | */ |
||
63 | protected $header = []; |
||
64 | |||
65 | /** |
||
66 | * Records count |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $nb_records = -1; |
||
71 | |||
72 | /** |
||
73 | * Detect Delimiters occurences in the CSV |
||
74 | * |
||
75 | * Returns a associative array where each key represents |
||
76 | * a valid delimiter and each value the number of occurences |
||
77 | * |
||
78 | * @param string[] $delimiters the delimiters to consider |
||
79 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
84 | { |
||
85 | $filter = function ($value): bool { |
||
86 | 4 | return 1 == strlen($value); |
|
87 | 3 | }; |
|
88 | |||
89 | 6 | $nb_records = $this->filterMinRange($nb_records, 1, __METHOD__.'() expects the number of records to consider to be a valid positive integer, %s given'); |
|
90 | 4 | $delimiters = array_unique(array_filter($delimiters, $filter)); |
|
91 | $reducer = function (array $res, string $delimiter) use ($nb_records): array { |
||
92 | 4 | $res[$delimiter] = $this->getCellCount($delimiter, $nb_records); |
|
93 | |||
94 | 4 | return $res; |
|
95 | 4 | }; |
|
96 | |||
97 | 4 | $res = array_reduce($delimiters, $reducer, []); |
|
98 | 4 | arsort($res, SORT_NUMERIC); |
|
99 | |||
100 | 4 | return $res; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the cell count for a specified delimiter |
||
105 | * and a specified number of records |
||
106 | * |
||
107 | * @param string $delimiter CSV delimiter |
||
108 | * @param int $nb_records CSV records to consider |
||
109 | * |
||
110 | * @return int |
||
111 | */ |
||
112 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
113 | { |
||
114 | $filter = function ($record): bool { |
||
115 | 2 | return is_array($record) && count($record) > 1; |
|
116 | 1 | }; |
|
117 | |||
118 | 2 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
119 | 2 | $this->document->setCsvControl($delimiter, $this->enclosure, $this->escape); |
|
120 | 2 | $iterator = new CallbackFilterIterator(new LimitIterator($this->document, 0, $nb_records), $filter); |
|
121 | |||
122 | 2 | return count(iterator_to_array($iterator, false), COUNT_RECURSIVE); |
|
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns the record padding value |
||
127 | * |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 2 | public function getRecordPaddingValue() |
|
131 | { |
||
132 | 2 | return $this->record_padding_value; |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Returns the record offset used as header |
||
137 | * |
||
138 | * If no CSV record is used this method MUST return null |
||
139 | * |
||
140 | * @return int|null |
||
141 | */ |
||
142 | 2 | public function getHeaderOffset() |
|
143 | { |
||
144 | 2 | return $this->header_offset; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Returns the CSV record header |
||
149 | * |
||
150 | * The returned header is represented as an array of string values |
||
151 | * |
||
152 | * @return string[] |
||
153 | */ |
||
154 | 4 | public function getHeader(): array |
|
155 | { |
||
156 | 4 | if (null === $this->header_offset) { |
|
157 | 2 | return $this->header; |
|
158 | } |
||
159 | |||
160 | 4 | if (empty($this->header)) { |
|
161 | 4 | $this->header = $this->setHeader($this->header_offset); |
|
162 | } |
||
163 | |||
164 | 4 | return $this->header; |
|
165 | } |
||
166 | |||
167 | /** |
||
168 | * Determine the CSV record header |
||
169 | * |
||
170 | * @param int $offset |
||
171 | * |
||
172 | * @throws RuntimeException If the header offset is an integer |
||
173 | * and the corresponding record is missing |
||
174 | * or is an empty array |
||
175 | * |
||
176 | * @return string[] |
||
177 | */ |
||
178 | 6 | protected function setHeader(int $offset): array |
|
179 | { |
||
180 | 6 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
181 | 6 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
182 | 6 | $this->document->seek($offset); |
|
183 | 6 | $header = $this->document->current(); |
|
184 | 6 | if (empty($header)) { |
|
185 | 2 | throw new RuntimeException(sprintf('The header record does not exist or is empty at offset: `%s`', $offset)); |
|
186 | } |
||
187 | |||
188 | 4 | if (0 === $offset) { |
|
189 | 2 | return $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure); |
|
190 | } |
||
191 | |||
192 | 2 | return $header; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Strip the BOM sequence from a record |
||
197 | * |
||
198 | * @param string[] $record |
||
199 | * @param int $bom_length |
||
200 | * @param string $enclosure |
||
201 | * |
||
202 | * @return string[] |
||
203 | */ |
||
204 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
205 | { |
||
206 | 8 | if (0 == $bom_length) { |
|
207 | 2 | return $record; |
|
208 | } |
||
209 | |||
210 | 6 | $record[0] = mb_substr($record[0], $bom_length); |
|
211 | 6 | if ($enclosure == mb_substr($record[0], 0, 1) && $enclosure == mb_substr($record[0], -1, 1)) { |
|
212 | 2 | $record[0] = mb_substr($record[0], 1, -1); |
|
213 | } |
||
214 | |||
215 | 6 | return $record; |
|
216 | } |
||
217 | |||
218 | /** |
||
219 | * @inheritdoc |
||
220 | */ |
||
221 | 6 | public function __call($method, array $arguments) |
|
222 | { |
||
223 | 6 | $whitelisted = ['fetchColumn' => 1, 'fetchPairs' => 1, 'fetchOne' => 1, 'fetchAll' => 1]; |
|
224 | 6 | if (isset($whitelisted[$method])) { |
|
225 | 2 | return (new ResultSet($this->getRecords(), $this->getHeader()))->$method(...$arguments); |
|
226 | } |
||
227 | |||
228 | 4 | throw new BadMethodCallException(sprintf('%s::%s() method does not exist', __CLASS__, $method)); |
|
229 | } |
||
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | 2 | public function count(): int |
|
235 | { |
||
236 | 2 | if (-1 === $this->nb_records) { |
|
237 | 2 | $this->nb_records = iterator_count($this->getRecords()); |
|
238 | } |
||
239 | |||
240 | 2 | return $this->nb_records; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * @inheritdoc |
||
245 | */ |
||
246 | 2 | public function getIterator(): Iterator |
|
247 | { |
||
248 | 2 | return $this->getRecords(); |
|
249 | } |
||
250 | |||
251 | /** |
||
252 | * Returns the CSV records in an iterator object. |
||
253 | * |
||
254 | * Each CSV record is represented as a simple array of string or null values. |
||
255 | * |
||
256 | * If the CSV document has a header record then each record is combined |
||
257 | * to each header record and the header record is removed from the iterator. |
||
258 | * |
||
259 | * If the CSV document is inconsistent. Missing record fields are |
||
260 | * filled with null values while extra record fields are strip from |
||
261 | * the returned object. |
||
262 | * |
||
263 | * @param array $header |
||
264 | * |
||
265 | * @return Iterator |
||
266 | */ |
||
267 | 6 | public function getRecords(array $header = []): Iterator |
|
268 | { |
||
269 | 6 | $header = $this->computeHeader($header); |
|
270 | $normalized = function ($record): bool { |
||
271 | 4 | return is_array($record) && $record != [null]; |
|
272 | 2 | }; |
|
273 | 4 | $bom = $this->getInputBOM(); |
|
274 | 4 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
275 | 4 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
276 | |||
277 | 4 | $records = $this->stripBOM(new CallbackFilterIterator($this->document, $normalized), $bom); |
|
278 | 4 | if (null !== $this->header_offset) { |
|
279 | $records = new CallbackFilterIterator($records, function (array $record, int $offset): bool { |
||
280 | 4 | return $offset !== $this->header_offset; |
|
281 | 4 | }); |
|
282 | } |
||
283 | |||
284 | 4 | return $this->combineHeader($records, $header); |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * Returns the header to be used for iteration |
||
289 | * |
||
290 | * @param string[] $header |
||
291 | * |
||
292 | * @throws RuntimeException If the header contains non unique column name |
||
293 | * |
||
294 | * @return string[] |
||
295 | */ |
||
296 | 10 | protected function computeHeader(array $header) |
|
297 | { |
||
298 | 10 | if (empty($header)) { |
|
299 | 8 | $header = $this->getHeader(); |
|
300 | } |
||
301 | |||
302 | 10 | if ($header === array_unique(array_filter($header, 'is_string'))) { |
|
303 | 8 | return $header; |
|
304 | } |
||
305 | |||
306 | 2 | throw new RuntimeException('The header record must be empty or a flat array with unique string values'); |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * Add the CSV header if present and valid |
||
311 | * |
||
312 | * @param Iterator $iterator |
||
313 | * @param array $header |
||
314 | * |
||
315 | * @return Iterator |
||
316 | */ |
||
317 | 14 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
318 | { |
||
319 | 14 | if (empty($header)) { |
|
320 | 8 | return $iterator; |
|
321 | } |
||
322 | |||
323 | 8 | $field_count = count($header); |
|
324 | $mapper = function (array $record) use ($header, $field_count): array { |
||
325 | 8 | if (count($record) != $field_count) { |
|
326 | 4 | $record = array_slice(array_pad($record, $field_count, $this->record_padding_value), 0, $field_count); |
|
327 | } |
||
328 | |||
329 | 8 | return array_combine($header, $record); |
|
330 | 8 | }; |
|
331 | |||
332 | 8 | return new MapIterator($iterator, $mapper); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Strip the BOM sequence if present |
||
337 | * |
||
338 | * @param Iterator $iterator |
||
339 | * @param string $bom |
||
340 | * |
||
341 | * @return Iterator |
||
342 | */ |
||
343 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
344 | { |
||
345 | 10 | if ('' === $bom) { |
|
346 | 4 | return $iterator; |
|
347 | } |
||
348 | |||
349 | 6 | $bom_length = mb_strlen($bom); |
|
350 | 6 | $mapper = function (array $record, int $index) use ($bom_length): array { |
|
351 | 6 | if (0 != $index) { |
|
352 | 2 | return $record; |
|
353 | } |
||
354 | |||
355 | 6 | return $this->removeBOM($record, $bom_length, $this->enclosure); |
|
356 | 6 | }; |
|
357 | |||
358 | 6 | return new MapIterator($iterator, $mapper); |
|
359 | } |
||
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 | * Selects the record to be used as the CSV header |
||
377 | * |
||
378 | * Because of the header is represented as an array, to be valid |
||
379 | * a header MUST contain only unique string value. |
||
380 | * |
||
381 | * @param int|null $offset the header record offset |
||
382 | * |
||
383 | * @return static |
||
384 | */ |
||
385 | 2 | public function setHeaderOffset($offset): self |
|
386 | { |
||
387 | 2 | if (null !== $offset) { |
|
388 | 2 | $offset = $this->filterMinRange($offset, 0, __METHOD__.'() expects the header offset index to be a positive integer or 0, %s given'); |
|
398 | |||
399 | /** |
||
400 | * @inheritdoc |
||
401 | */ |
||
402 | 2 | protected function resetProperties() |
|
407 | } |
||
408 |