PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.21
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.21
2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 1.45 All 41 releases
fluent-boards / app / Services / Libs / csv / src / Reader.php

Reader.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.21, at app/Services/Libs/csv/src/Reader.php

280 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file is part of the League.csv library
4 *
5 * @license http://opensource.org/licenses/MIT
6 * @link https://github.com/thephpleague/csv/
7 * @version 7.2.0
8 * @package League.csv
9 *
10 * For the full copyright and license information, please view the LICENSE
11 * file that was distributed with this source code.
12 */
13 namespace League\Csv;
14
15 use CallbackFilterIterator;
16 use InvalidArgumentException;
17 use Iterator;
18 use League\Csv\Modifier\MapIterator;
19 use LimitIterator;
20 use SplFileObject;
21
22 /**
23 * A class to manage extracting and filtering a CSV
24 *
25 * @package League.csv
26 * @since 3.0.0
27 *
28 */
29 class Reader extends AbstractCsv
30 {
31 /**
32 * @inheritdoc
33 */
34 protected $stream_filter_mode = STREAM_FILTER_READ;
35
36 /**
37 * Returns a Filtered Iterator
38 *
39 * DEPRECATION WARNING! This method will be removed in the next major point release
40 *
41 * @deprecated deprecated since version 7.2
42 *
43 * @return Iterator
44 */
45 public function query(callable $callable = null)
46 {
47 return $this->fetch($callable);
48 }
49
50 /**
51 * Return a Filtered Iterator
52 *
53 * @param callable $callable a callable function to be applied to each Iterator item
54 *
55 * @return Iterator
56 */
57 public function fetch(callable $callable = null)
58 {
59 $this->addFilter(function ($row) {
60 return is_array($row);
61 });
62 $iterator = $this->getIterator();
63 $iterator = $this->applyBomStripping($iterator);
64 $iterator = $this->applyIteratorFilter($iterator);
65 $iterator = $this->applyIteratorSortBy($iterator);
66 $iterator = $this->applyIteratorInterval($iterator);
67 if (!is_null($callable)) {
68 return new MapIterator($iterator, $callable);
69 }
70
71 return $iterator;
72 }
73
74 /**
75 * Applies a callback function on the CSV
76 *
77 * The callback function must return TRUE in order to continue
78 * iterating over the iterator.
79 *
80 * @param callable $callable The callback function
81 *
82 * @return int the iteration count
83 */
84 public function each(callable $callable)
85 {
86 $index = 0;
87 $iterator = $this->fetch();
88 $iterator->rewind();
89 while ($iterator->valid() && true === call_user_func(
90 $callable,
91 $iterator->current(),
92 $iterator->key(),
93 $iterator
94 )) {
95 ++$index;
96 $iterator->next();
97 }
98
99 return $index;
100 }
101
102 /**
103 * Returns a single row from the CSV
104 *
105 * @param int $offset
106 *
107 * @throws InvalidArgumentException If the $offset is not a valid Integer
108 *
109 * @return array
110 */
111 public function fetchOne($offset = 0)
112 {
113 $this->setOffset($offset);
114 $this->setLimit(1);
115 $iterator = $this->fetch();
116 $iterator->rewind();
117
118 return (array) $iterator->current();
119 }
120
121 /**
122 * Returns a sequential array of all CSV lines
123 *
124 * The callable function will be applied to each Iterator item
125 *
126 * @param callable $callable a callable function
127 *
128 * @return array
129 */
130 public function fetchAll(callable $callable = null)
131 {
132 return iterator_to_array($this->fetch($callable), false);
133 }
134
135 /**
136 * Returns a single column from the CSV data
137 *
138 * The callable function will be applied to each value to be return
139 *
140 * @param int $column_index field Index
141 * @param callable $callable a callable function
142 *
143 * @throws InvalidArgumentException If the column index is not a positive integer or 0
144 *
145 * @return array
146 */
147 public function fetchColumn($column_index = 0, callable $callable = null)
148 {
149 if (false === filter_var($column_index, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
150 throw new InvalidArgumentException(
151 'the column index must be a positive integer or 0'
152 );
153 }
154 $filterColumn = function ($row) use ($column_index) {
155 return array_key_exists($column_index, $row);
156 };
157 $selectColumn = function ($row) use ($column_index) {
158 return $row[$column_index];
159 };
160
161 $iterator = $this->fetch($callable);
162 $iterator = new CallbackFilterIterator($iterator, $filterColumn);
163
164 return iterator_to_array(new MapIterator($iterator, $selectColumn), false);
165 }
166
167 /**
168 * Returns a sequential array of all CSV lines;
169 *
170 * The rows are presented as associated arrays
171 * The callable function will be applied to each Iterator item
172 *
173 * @param int|array $offset_or_keys the name for each key member OR the row Index to be
174 * used as the associated named keys
175 *
176 * @param callable $callable a callable function
177 *
178 * @throws InvalidArgumentException If the submitted keys are invalid
179 *
180 * @return Iterator
181 */
182 public function fetchAssoc($offset_or_keys = 0, callable $callable = null)
183 {
184 $keys = $this->getAssocKeys($offset_or_keys);
185 $keys_count = count($keys);
186 $combineArray = function (array $row) use ($keys, $keys_count) {
187 if ($keys_count != count($row)) {
188 $row = array_slice(array_pad($row, $keys_count, null), 0, $keys_count);
189 }
190
191 return array_combine($keys, $row);
192 };
193
194 return iterator_to_array(new MapIterator($this->fetch($callable), $combineArray), false);
195 }
196
197 /**
198 * Selects the array to be used as key for the fetchAssoc method
199 *
200 * @param int|array $offset_or_keys the assoc key OR the row Index to be used
201 * as the key index
202 *
203 * @throws InvalidArgumentException If the row index and/or the resulting array is invalid
204 *
205 * @return array
206 */
207 protected function getAssocKeys($offset_or_keys)
208 {
209 if (is_array($offset_or_keys)) {
210 return $this->validateAssocKeys($offset_or_keys);
211 }
212
213 if (false === filter_var($offset_or_keys, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
214 throw new InvalidArgumentException('the row index must be a positive integer, 0 or a non empty array');
215 }
216
217 $keys = $this->getRow($offset_or_keys);
218 $keys = $this->validateAssocKeys($keys);
219 $filterOutRow = function ($row, $rowIndex) use ($offset_or_keys) {
220 return is_array($row) && $rowIndex != $offset_or_keys;
221 };
222 $this->addFilter($filterOutRow);
223
224 return $keys;
225 }
226
227 /**
228 * Validates the array to be used by the fetchAssoc method
229 *
230 * @param array $keys
231 *
232 * @throws InvalidArgumentException If the submitted array fails the assertion
233 */
234 protected function validateAssocKeys(array $keys)
235 {
236 if (empty($keys)) {
237 throw new InvalidArgumentException('The array can not be empty');
238 }
239
240 foreach ($keys as &$str) {
241 $str = $this->validateString($str);
242 }
243 unset($str);
244
245 if ($keys == array_unique($keys)) {
246 return $keys;
247 }
248
249 throw new InvalidArgumentException('The array must contain unique values');
250 }
251
252 /**
253 * Returns a single row from the CSV without filtering
254 *
255 * @param int $offset
256 *
257 * @throws InvalidArgumentException If the $offset is not valid or the row does not exist
258 *
259 * @return array
260 */
261 protected function getRow($offset)
262 {
263 $csv = $this->getIterator();
264 $csv->setFlags($this->getFlags() & ~SplFileObject::READ_CSV);
265 $iterator = new LimitIterator($csv, $offset, 1);
266 $iterator->rewind();
267 $res = $iterator->current();
268
269 if (empty($res)) {
270 throw new InvalidArgumentException('the specified row does not exist or is empty');
271 }
272
273 if (0 == $offset && $this->isBomStrippable()) {
274 $res = mb_substr($res, mb_strlen($this->getInputBom()));
275 }
276
277 return str_getcsv($res, $this->delimiter, $this->enclosure, $this->escape);
278 }
279 }
280