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 | * Returns the record offset used as header |
||
61 | * |
||
62 | * If no CSV record is used this method MUST return null |
||
63 | * |
||
64 | * @return int|null |
||
65 | */ |
||
66 | 2 | public function getHeaderOffset() |
|
70 | |||
71 | /** |
||
72 | * Returns a CSV records collection |
||
73 | * |
||
74 | * @param Statement $stmt |
||
75 | * |
||
76 | * @return ResultSet |
||
77 | */ |
||
78 | 2 | public function select(Statement $stmt): ResultSet |
|
82 | |||
83 | /** |
||
84 | * Detect Delimiters occurences in the CSV |
||
85 | * |
||
86 | * Returns a associative array where each key represents |
||
87 | * a valid delimiter and each value the number of occurences |
||
88 | * |
||
89 | * @param string[] $delimiters the delimiters to consider |
||
90 | * @param int $nb_records Detection is made using $nb_records of the CSV |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 6 | public function fetchDelimitersOccurrence(array $delimiters, int $nb_records = 1): array |
|
113 | |||
114 | /** |
||
115 | * Returns the cell count for a specified delimiter |
||
116 | * and a specified number of records |
||
117 | * |
||
118 | * @param string $delimiter CSV delimiter |
||
119 | * @param int $nb_records CSV records to consider |
||
120 | * |
||
121 | * @return int |
||
122 | */ |
||
123 | 2 | protected function getCellCount(string $delimiter, int $nb_records): int |
|
135 | |||
136 | /** |
||
137 | * @inheritdoc |
||
138 | */ |
||
139 | 24 | public function getIterator(): Iterator |
|
140 | { |
||
141 | 24 | return $this->getRecords(); |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Returns the CSV records in an iterator object. |
||
146 | * |
||
147 | * Each CSV record is represented as a simple array of string or null values. |
||
148 | * |
||
149 | * If the CSV document has a header record then each record is combined |
||
150 | * to each header record and the header record is removed from the iterator. |
||
151 | * |
||
152 | * If the CSV document is inconsistent. Missing record fields are |
||
153 | * filled with null values while extra record fields are strip from |
||
154 | * the returned object. |
||
155 | * |
||
156 | * @throws RuntimeException If the header contains non unique column name |
||
157 | * |
||
158 | * @return Iterator |
||
159 | */ |
||
160 | 4 | public function getRecords(): Iterator |
|
161 | { |
||
162 | 4 | if (!$this->supportsHeaderAsRecordKeys()) { |
|
163 | 2 | throw new RuntimeException('The header record must be empty or a flat array with unique string values'); |
|
164 | } |
||
165 | |||
166 | $normalized = function ($record): bool { |
||
167 | 2 | return is_array($record) && $record != [null]; |
|
168 | 1 | }; |
|
169 | |||
170 | 2 | $bom = $this->getInputBOM(); |
|
171 | 2 | $this->document->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY); |
|
172 | 2 | $this->document->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
|
173 | |||
174 | 2 | return $this->combineHeader( |
|
175 | 2 | $this->stripBOM(new CallbackFilterIterator($this->document, $normalized), $bom) |
|
176 | ); |
||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Returns wether the selected header can be combine to each record |
||
181 | * |
||
182 | * A valid header must be empty or contains unique string field names |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 2 | public function supportsHeaderAsRecordKeys(): bool |
|
192 | |||
193 | /** |
||
194 | * Returns the CSV record header |
||
195 | * |
||
196 | * The returned header is represented as an array of string values |
||
197 | * |
||
198 | * @return string[] |
||
199 | */ |
||
200 | 4 | public function getHeader(): array |
|
214 | |||
215 | /** |
||
216 | * Determine the CSV record header |
||
217 | * |
||
218 | * @param int $offset |
||
219 | * |
||
220 | * @throws RuntimeException If the header offset is an integer |
||
221 | * and the corresponding record is missing |
||
222 | * or is an empty array |
||
223 | * |
||
224 | * @return string[] |
||
225 | */ |
||
226 | 6 | protected function setHeader(int $offset): array |
|
242 | |||
243 | /** |
||
244 | * Add the CSV header if present and valid |
||
245 | * |
||
246 | * @param Iterator $iterator |
||
247 | * |
||
248 | * @return Iterator |
||
249 | */ |
||
250 | 4 | protected function combineHeader(Iterator $iterator): Iterator |
|
272 | |||
273 | /** |
||
274 | * Strip the BOM sequence if present |
||
275 | * |
||
276 | * @param Iterator $iterator |
||
277 | * @param string $bom |
||
278 | * |
||
279 | * @return Iterator |
||
280 | */ |
||
281 | 4 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
298 | |||
299 | /** |
||
300 | * Strip the BOM sequence from a record |
||
301 | * |
||
302 | * @param string[] $record |
||
303 | * @param int $bom_length |
||
304 | * @param string $enclosure |
||
305 | * |
||
306 | * @return string[] |
||
307 | */ |
||
308 | 4 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
321 | |||
322 | /** |
||
323 | * Selects the record to be used as the CSV header |
||
324 | * |
||
325 | * Because of the header is represented as an array, to be valid |
||
326 | * a header MUST contain only unique string value. |
||
327 | * |
||
328 | * @param int|null $offset the header record offset |
||
329 | * |
||
330 | * @return static |
||
331 | */ |
||
332 | 2 | public function setHeaderOffset($offset): self |
|
345 | |||
346 | /** |
||
347 | * @inheritdoc |
||
348 | */ |
||
349 | 2 | protected function resetProperties() |
|
353 | } |
||
354 |