1 | <?php |
||
29 | class Reader extends AbstractCsv |
||
30 | { |
||
31 | /** |
||
32 | * @inheritdoc |
||
33 | */ |
||
34 | protected $stream_filter_mode = STREAM_FILTER_READ; |
||
35 | |||
36 | /** |
||
37 | * Return a Filtered Iterator |
||
38 | * |
||
39 | * @param callable|null $callable a callable function to be applied to each Iterator item |
||
40 | * |
||
41 | * @return Iterator |
||
42 | */ |
||
43 | 159 | public function fetch(callable $callable = null) |
|
47 | |||
48 | /** |
||
49 | * Apply The callable function |
||
50 | * |
||
51 | * @param Iterator $iterator |
||
52 | * @param callable|null $callable |
||
53 | * |
||
54 | * @return Iterator |
||
55 | */ |
||
56 | 159 | protected function applyCallable(Iterator $iterator, callable $callable = null) |
|
64 | |||
65 | /** |
||
66 | * Returns a sequential array of all CSV lines |
||
67 | * |
||
68 | * The callable function will be applied to each Iterator item |
||
69 | * |
||
70 | * @param callable|null $callable a callable function |
||
71 | * |
||
72 | * @return array |
||
73 | */ |
||
74 | 63 | public function fetchAll(callable $callable = null) |
|
78 | |||
79 | /** |
||
80 | * Applies a callback function on the CSV |
||
81 | * |
||
82 | * The callback function must return TRUE in order to continue |
||
83 | * iterating over the iterator. |
||
84 | * |
||
85 | * @param callable $callable a callable function to apply to each selected CSV rows |
||
86 | * |
||
87 | * @return int the iteration count |
||
88 | */ |
||
89 | 9 | public function each(callable $callable) |
|
106 | |||
107 | /** |
||
108 | * Returns a single row from the CSV |
||
109 | * |
||
110 | * By default if no offset is provided the first row of the CSV is selected |
||
111 | * |
||
112 | * @param int $offset the CSV row offset |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 12 | public function fetchOne($offset = 0) |
|
125 | |||
126 | /** |
||
127 | * Returns a single column from the CSV data |
||
128 | * |
||
129 | * The callable function will be applied to each value to be return |
||
130 | * |
||
131 | * By default if no column index is provided the first column of the CSV is selected |
||
132 | * |
||
133 | * @param int $columnIndex CSV column index |
||
134 | * @param callable|null $callable A callable to be applied to each of the value to be returned. |
||
135 | * |
||
136 | * @return Iterator |
||
137 | */ |
||
138 | 21 | public function fetchColumn($columnIndex = 0, callable $callable = null) |
|
156 | |||
157 | /** |
||
158 | * Retrive CSV data as pairs |
||
159 | * |
||
160 | * Fetches an associative array of all rows as key-value pairs (first |
||
161 | * column is the key, second column is the value). |
||
162 | * |
||
163 | * By default if no column index is provided: |
||
164 | * - the first CSV column is used to provide the keys |
||
165 | * - the second CSV column is used to provide the value |
||
166 | * |
||
167 | * @param int $offsetIndex The column index to serve as offset |
||
168 | * @param int $valueIndex The column index to serve as value |
||
169 | * @param callable|null $callable A callable to be applied to each of the rows to be returned. |
||
170 | * |
||
171 | * @return Generator |
||
172 | */ |
||
173 | public function fetchPairs($offsetIndex = 0, $valueIndex = 1, callable $callable = null) |
||
193 | 27 | ||
194 | /** |
||
195 | * Return the key/pairs as a PHP generator |
||
196 | * |
||
197 | * @param Iterator $iterator |
||
198 | * |
||
199 | * @return Generator |
||
200 | */ |
||
201 | protected function generatePairs(Iterator $iterator) |
||
207 | 18 | ||
208 | 27 | /** |
|
209 | * Retrive CSV data as pairs |
||
210 | * |
||
211 | * Fetches an associative array of all rows as key-value pairs (first |
||
212 | * column is the key, second column is the value). |
||
213 | * |
||
214 | * By default if no column index is provided: |
||
215 | * - the first CSV column is used to provide the keys |
||
216 | * - the second CSV column is used to provide the value |
||
217 | * |
||
218 | * @param int $offsetIndex The column index to serve as offset |
||
219 | * @param int $valueIndex The column index to serve as value |
||
220 | * @param callable|null $callable A callable to be applied to each of the rows to be returned. |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | public function fetchPairsWithoutDuplicates($offsetIndex = 0, $valueIndex = 1, callable $callable = null) |
||
228 | 27 | ||
229 | /** |
||
230 | 27 | * Returns a sequential array of all CSV lines; |
|
231 | 6 | * |
|
232 | 4 | * The rows are presented as associated arrays |
|
233 | * The callable function will be applied to each Iterator item |
||
234 | 27 | * |
|
235 | 27 | * @param int|array $offset_or_keys the name for each key member OR the row Index to be |
|
236 | * used as the associated named keys |
||
237 | 27 | * |
|
238 | 27 | * @param callable $callable A callable to be applied to each of the rows to be returned. |
|
239 | 27 | * |
|
240 | * @throws InvalidArgumentException If the submitted keys are invalid |
||
241 | 27 | * |
|
242 | * @return Iterator|array |
||
243 | */ |
||
244 | public function fetchAssoc($offset_or_keys = 0, callable $callable = null) |
||
261 | 12 | ||
262 | 18 | /** |
|
263 | 6 | * Selects the array to be used as key for the fetchAssoc method |
|
264 | 12 | * |
|
265 | 15 | * @param int|array $offset_or_keys the assoc key OR the row Index to be used |
|
266 | 12 | * as the key index |
|
267 | 12 | * |
|
268 | 12 | * @throws InvalidArgumentException If the row index and/or the resulting array is invalid |
|
269 | 12 | * |
|
270 | * @return array |
||
271 | 12 | */ |
|
272 | protected function getAssocKeys($offset_or_keys) |
||
291 | |||
292 | /** |
||
293 | * Validates the array to be used by the fetchAssoc method |
||
294 | * |
||
295 | * @param array $keys |
||
296 | * |
||
297 | * @throws InvalidArgumentException If the submitted array fails the assertion |
||
298 | * |
||
299 | 30 | * @return array |
|
300 | */ |
||
301 | 30 | protected function validateKeys(array $keys) |
|
309 | |||
310 | /** |
||
311 | * Returns whether the submitted value can be used as string |
||
312 | * |
||
313 | 15 | * @param mixed $value |
|
314 | * |
||
315 | 15 | * @return bool |
|
316 | 15 | */ |
|
317 | 15 | protected function isValidKey($value) |
|
321 | 15 | ||
322 | 3 | /** |
|
323 | * Returns a single row from the CSV without filtering |
||
324 | * |
||
325 | 12 | * @param int $offset |
|
326 | 6 | * |
|
327 | 4 | * @throws InvalidArgumentException If the $offset is not valid or the row does not exist |
|
328 | * |
||
329 | 12 | * @return array |
|
330 | */ |
||
331 | protected function getRow($offset) |
||
349 | } |
||
350 |