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() |
|
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() |
|
146 | |||
147 | /** |
||
148 | * Returns wether the selected header can be combine to each record |
||
149 | * |
||
150 | * A valid header must be empty or contains unique string field names |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
160 | |||
161 | /** |
||
162 | * Returns the CSV record header |
||
163 | * |
||
164 | * The returned header is represented as an array of string values |
||
165 | * |
||
166 | * @return string[] |
||
167 | */ |
||
168 | 4 | public function getHeader(): array |
|
180 | |||
181 | /** |
||
182 | * Determine the CSV record header |
||
183 | * |
||
184 | * @param int $offset |
||
185 | * |
||
186 | * @throws RuntimeException If the header offset is an integer |
||
187 | * and the corresponding record is missing |
||
188 | * or is an empty array |
||
189 | * |
||
190 | * @return string[] |
||
191 | */ |
||
192 | 6 | protected function setHeader(int $offset): array |
|
208 | |||
209 | /** |
||
210 | * Strip the BOM sequence from a record |
||
211 | * |
||
212 | * @param string[] $record |
||
213 | * @param int $bom_length |
||
214 | * @param string $enclosure |
||
215 | * |
||
216 | * @return string[] |
||
217 | */ |
||
218 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
231 | |||
232 | /** |
||
233 | * @inheritdoc |
||
234 | */ |
||
235 | 6 | public function __call($method, array $arguments) |
|
244 | |||
245 | /** |
||
246 | * @inheritdoc |
||
247 | */ |
||
248 | 2 | public function count(): int |
|
256 | |||
257 | /** |
||
258 | * @inheritdoc |
||
259 | */ |
||
260 | 2 | public function getIterator(): Iterator |
|
264 | |||
265 | /** |
||
266 | * Returns the CSV records in an iterator object. |
||
267 | * |
||
268 | * Each CSV record is represented as a simple array of string or null values. |
||
269 | * |
||
270 | * If the CSV document has a header record then each record is combined |
||
271 | * to each header record and the header record is removed from the iterator. |
||
272 | * |
||
273 | * If the CSV document is inconsistent. Missing record fields are |
||
274 | * filled with null values while extra record fields are strip from |
||
275 | * the returned object. |
||
276 | * |
||
277 | * @throws RuntimeException If the header contains non unique column name |
||
278 | * |
||
279 | * @return Iterator |
||
280 | */ |
||
281 | 6 | public function getRecords(): Iterator |
|
282 | { |
||
283 | 6 | if (!$this->supportsHeaderAsRecordKeys()) { |
|
284 | 2 | throw new RuntimeException('The header record must be empty or a flat array with unique string values'); |
|
285 | } |
||
286 | |||
287 | $normalized = function ($record): bool { |
||
288 | 4 | return is_array($record) && $record != [null]; |
|
289 | 2 | }; |
|
290 | 4 | $bom = $this->getInputBOM(); |
|
291 | 4 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
292 | 4 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
293 | |||
294 | 4 | return $this->combineHeader($this->stripBOM(new CallbackFilterIterator($this->document, $normalized), $bom)); |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Add the CSV header if present and valid |
||
299 | * |
||
300 | * @param Iterator $iterator |
||
301 | * |
||
302 | * @return Iterator |
||
303 | */ |
||
304 | 14 | protected function combineHeader(Iterator $iterator): Iterator |
|
326 | |||
327 | /** |
||
328 | * Strip the BOM sequence if present |
||
329 | * |
||
330 | * @param Iterator $iterator |
||
331 | * @param string $bom |
||
332 | * |
||
333 | * @return Iterator |
||
334 | */ |
||
335 | 10 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
352 | |||
353 | /** |
||
354 | * Set the record padding value |
||
355 | * |
||
356 | * @param mixed $record_padding_value |
||
357 | * |
||
358 | * @return static |
||
359 | */ |
||
360 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
366 | |||
367 | /** |
||
368 | * Selects the record to be used as the CSV header |
||
369 | * |
||
370 | * Because of the header is represented as an array, to be valid |
||
371 | * a header MUST contain only unique string value. |
||
372 | * |
||
373 | * @param int|null $offset the header record offset |
||
374 | * |
||
375 | * @return static |
||
376 | */ |
||
377 | 2 | public function setHeaderOffset($offset): self |
|
390 | |||
391 | /** |
||
392 | * @inheritdoc |
||
393 | */ |
||
394 | 2 | protected function resetProperties() |
|
399 | } |
||
400 |