1 | <?php |
||
37 | class Reader extends AbstractCsv implements Countable, IteratorAggregate, JsonSerializable |
||
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 | * Returns the record padding value |
||
74 | * |
||
75 | * @return mixed |
||
76 | */ |
||
77 | 2 | public function getRecordPaddingValue() |
|
78 | { |
||
79 | 2 | return $this->record_padding_value; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * Returns the record offset used as header |
||
84 | * |
||
85 | * If no CSV record is used this method MUST return null |
||
86 | * |
||
87 | * @return int|null |
||
88 | */ |
||
89 | 10 | public function getHeaderOffset() |
|
90 | { |
||
91 | 10 | return $this->header_offset; |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns the CSV record header |
||
96 | * |
||
97 | * The returned header is represented as an array of string values |
||
98 | * |
||
99 | * @return string[] |
||
100 | */ |
||
101 | 10 | public function getHeader(): array |
|
102 | { |
||
103 | 10 | if (null === $this->header_offset) { |
|
104 | 8 | return $this->header; |
|
105 | } |
||
106 | |||
107 | 4 | if (empty($this->header)) { |
|
108 | 4 | $this->header = $this->setHeader($this->header_offset); |
|
109 | } |
||
110 | |||
111 | 4 | return $this->header; |
|
112 | } |
||
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 | * @return string[] |
||
124 | */ |
||
125 | 6 | protected function setHeader(int $offset): array |
|
141 | |||
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 | 8 | protected function removeBOM(array $record, int $bom_length, string $enclosure): array |
|
164 | |||
165 | /** |
||
166 | * @inheritdoc |
||
167 | */ |
||
168 | 6 | public function __call($method, array $arguments) |
|
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | 2 | public function count(): int |
|
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 2 | public function getIterator(): Iterator |
|
197 | |||
198 | /** |
||
199 | * @inheritdoc |
||
200 | */ |
||
201 | 2 | public function jsonSerialize(): array |
|
205 | |||
206 | /** |
||
207 | * Returns the CSV records in an iterator object. |
||
208 | * |
||
209 | * Each CSV record is represented as a simple array of string or null values. |
||
210 | * |
||
211 | * If the CSV document has a header record then each record is combined |
||
212 | * to each header record and the header record is removed from the iterator. |
||
213 | * |
||
214 | * If the CSV document is inconsistent. Missing record fields are |
||
215 | * filled with null values while extra record fields are strip from |
||
216 | * the returned object. |
||
217 | * |
||
218 | * @param string[] $header an optional header to use instead of the CSV document header |
||
219 | * |
||
220 | * @return Iterator |
||
221 | */ |
||
222 | 12 | public function getRecords(array $header = []): Iterator |
|
241 | |||
242 | /** |
||
243 | * Returns the header to be used for iteration |
||
244 | * |
||
245 | * @param string[] $header |
||
246 | * |
||
247 | * @throws RuntimeException If the header contains non unique column name |
||
248 | * |
||
249 | * @return string[] |
||
250 | */ |
||
251 | 16 | protected function computeHeader(array $header) |
|
263 | |||
264 | /** |
||
265 | * Add the CSV header if present and valid |
||
266 | * |
||
267 | * @param Iterator $iterator |
||
268 | * @param string[] $header |
||
269 | * |
||
270 | * @return Iterator |
||
271 | */ |
||
272 | 20 | protected function combineHeader(Iterator $iterator, array $header): Iterator |
|
289 | |||
290 | /** |
||
291 | * Strip the BOM sequence if present |
||
292 | * |
||
293 | * @param Iterator $iterator |
||
294 | * @param string $bom |
||
295 | * |
||
296 | * @return Iterator |
||
297 | */ |
||
298 | 16 | protected function stripBOM(Iterator $iterator, string $bom): Iterator |
|
315 | |||
316 | /** |
||
317 | * Set the record padding value |
||
318 | * |
||
319 | * @param mixed $record_padding_value |
||
320 | * |
||
321 | * @return static |
||
322 | */ |
||
323 | 2 | public function setRecordPaddingValue($record_padding_value): self |
|
329 | |||
330 | /** |
||
331 | * Selects the record to be used as the CSV header |
||
332 | * |
||
333 | * Because of the header is represented as an array, to be valid |
||
334 | * a header MUST contain only unique string value. |
||
335 | * |
||
336 | * @param int|null $offset the header record offset |
||
337 | * |
||
338 | * @return static |
||
339 | */ |
||
340 | 10 | public function setHeaderOffset($offset): self |
|
353 | |||
354 | /** |
||
355 | * @inheritdoc |
||
356 | */ |
||
357 | 8 | protected function resetProperties() |
|
362 | } |
||
363 |