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 / AbstractCsv.php

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

324 lines 8.1 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 InvalidArgumentException;
16 use Iterator;
17 use IteratorAggregate;
18 use JsonSerializable;
19 use League\Csv\Config\Controls;
20 use League\Csv\Config\Output;
21 use League\Csv\Modifier\QueryFilter;
22 use League\Csv\Modifier\StreamFilter;
23 use SplFileInfo;
24 use SplFileObject;
25 use SplTempFileObject;
26
27 /**
28 * An abstract class to enable basic CSV manipulation
29 *
30 * @package League.csv
31 * @since 4.0.0
32 *
33 */
34 abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
35 {
36 use Controls;
37
38 use Output;
39
40 use QueryFilter;
41
42 use StreamFilter;
43
44 /**
45 * UTF-8 BOM sequence
46 */
47 const BOM_UTF8 = "\xEF\xBB\xBF";
48
49 /**
50 * UTF-16 BE BOM sequence
51 */
52 const BOM_UTF16_BE = "\xFE\xFF";
53
54 /**
55 * UTF-16 LE BOM sequence
56 */
57 const BOM_UTF16_LE = "\xFF\xFE";
58
59 /**
60 * UTF-32 BE BOM sequence
61 */
62 const BOM_UTF32_BE = "\x00\x00\xFE\xFF";
63
64 /**
65 * UTF-32 LE BOM sequence
66 */
67 const BOM_UTF32_LE = "\x00\x00\xFF\xFE";
68
69 /**
70 * The constructor path
71 *
72 * can be a SplFileInfo object or the string path to a file
73 *
74 * @var SplFileObject|string
75 */
76 protected $path;
77
78 /**
79 * The file open mode flag
80 *
81 * @var string
82 */
83 protected $open_mode;
84
85 /**
86 * Default SplFileObject flags settings
87 *
88 * @var int
89 */
90 protected $defaultFlags;
91
92 /**
93 * Creates a new instance
94 *
95 * The path must be an SplFileInfo object
96 * an object that implements the `__toString` method
97 * a path to a file
98 *
99 * @param SplFileObject|string $path The file path
100 * @param string $open_mode the file open mode flag
101 */
102 protected function __construct($path, $open_mode = 'r+')
103 {
104 $this->defaultFlags = SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY;
105 $this->flags = $this->defaultFlags;
106 $this->open_mode = strtolower($open_mode);
107 $this->path = $path;
108 $this->initStreamFilter($this->path);
109 }
110
111 /**
112 * The destructor
113 */
114 public function __destruct()
115 {
116 $this->path = null;
117 }
118
119 /**
120 * Returns the CSV Iterator
121 *
122 * @return SplFileObject
123 */
124 #[\ReturnTypeWillChange]
125 public function getIterator()
126 {
127 $iterator = $this->path;
128 if (!$iterator instanceof SplFileObject) {
129 $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
130 }
131 $iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
132 $iterator->setFlags($this->flags);
133
134 return $iterator;
135 }
136
137 /**
138 * Returns the CSV Iterator for conversion
139 *
140 * @return Iterator
141 */
142 protected function getConversionIterator()
143 {
144 $iterator = $this->getIterator();
145 $iterator->setFlags($this->defaultFlags);
146 $iterator = $this->applyBomStripping($iterator);
147 $iterator = $this->applyIteratorFilter($iterator);
148 $iterator = $this->applyIteratorSortBy($iterator);
149
150 return $this->applyIteratorInterval($iterator);
151 }
152
153 /**
154 * Creates a {@link AbstractCsv} from a string
155 *
156 * The path can be:
157 * - an SplFileInfo,
158 * - a SplFileObject,
159 * - an object that implements the `__toString` method,
160 * - a string
161 *
162 * BUT NOT a SplTempFileObject
163 *
164 * <code>
165 *<?php
166 * $csv = new Reader::createFromPath('/path/to/file.csv', 'a+');
167 * $csv = new Reader::createFromPath(new SplFileInfo('/path/to/file.csv'));
168 * $csv = new Reader::createFromPath(new SplFileObject('/path/to/file.csv'), 'rb');
169 *
170 * ?>
171 * </code>
172 *
173 * @param mixed $path file path
174 * @param string $open_mode the file open mode flag
175 *
176 * @throws InvalidArgumentException If $path is a \SplTempFileObject object
177 *
178 * @return static
179 */
180 public static function createFromPath($path, $open_mode = 'r+')
181 {
182 if ($path instanceof SplTempFileObject) {
183 throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
184 }
185
186 if ($path instanceof SplFileInfo) {
187 $path = $path->getPath().'/'.$path->getBasename();
188 }
189
190 return new static(static::validateString($path), $open_mode);
191 }
192
193 /**
194 * validate a string
195 *
196 * @param mixed $str the value to evaluate as a string
197 *
198 * @throws InvalidArgumentException if the submitted data can not be converted to string
199 *
200 * @return string
201 */
202 protected static function validateString($str)
203 {
204 if (is_string($str) || (is_object($str) && method_exists($str, '__toString'))) {
205 return (string) $str;
206 }
207
208 throw new InvalidArgumentException('Expected data must be a string or stringable');
209 }
210
211 /**
212 * Creates a {@link AbstractCsv} from a SplFileObject
213 *
214 * The path can be:
215 * - a SplFileObject,
216 * - a SplTempFileObject
217 *
218 * <code>
219 *<?php
220 * $csv = new Writer::createFromFileObject(new SplFileInfo('/path/to/file.csv'));
221 * $csv = new Writer::createFromFileObject(new SplTempFileObject);
222 *
223 * ?>
224 * </code>
225 *
226 * @param SplFileObject $file
227 *
228 * @return static
229 */
230 public static function createFromFileObject(SplFileObject $file)
231 {
232 return new static($file);
233 }
234
235 /**
236 * Creates a {@link AbstractCsv} from a string
237 *
238 * The string must be an object that implements the `__toString` method,
239 * or a string
240 *
241 * @param string $str the string
242 * @param string $newline the newline character
243 *
244 * @return static
245 */
246 public static function createFromString($str, $newline = "\n")
247 {
248 $file = new SplTempFileObject();
249 $file->fwrite(static::validateString($str));
250
251 $csv = static::createFromFileObject($file);
252 $csv->setNewline($newline);
253
254 return $csv;
255 }
256
257 /**
258 * Creates a {@link AbstractCsv} instance from another {@link AbstractCsv} object
259 *
260 * @param string $class_name the class to be instantiated
261 * @param string $open_mode the file open mode flag
262 *
263 * @return static
264 */
265 protected function newInstance($class_name, $open_mode)
266 {
267 $csv = new $class_name($this->path, $open_mode);
268 $csv->delimiter = $this->delimiter;
269 $csv->enclosure = $this->enclosure;
270 $csv->escape = $this->escape;
271 $csv->encodingFrom = $this->encodingFrom;
272 $csv->flags = $this->flags;
273 $csv->input_bom = $this->input_bom;
274 $csv->output_bom = $this->output_bom;
275 $csv->newline = $this->newline;
276
277 return $csv;
278 }
279
280 /**
281 * Creates a {@link Writer} instance from a {@link AbstractCsv} object
282 *
283 * @param string $open_mode the file open mode flag
284 *
285 * @return Writer
286 */
287 public function newWriter($open_mode = 'r+')
288 {
289 return $this->newInstance('\League\Csv\Writer', $open_mode);
290 }
291
292 /**
293 * Creates a {@link Reader} instance from a {@link AbstractCsv} object
294 *
295 * @param string $open_mode the file open mode flag
296 *
297 * @return Reader
298 */
299 public function newReader($open_mode = 'r+')
300 {
301 return $this->newInstance('\League\Csv\Reader', $open_mode);
302 }
303
304 /**
305 * Validate the submitted integer
306 *
307 * @param int $int
308 * @param int $minValue
309 * @param string $errorMessage
310 *
311 * @throws InvalidArgumentException If the value is invalid
312 *
313 * @return int
314 */
315 protected function filterInteger($int, $minValue, $errorMessage)
316 {
317 if (false === ($int = filter_var($int, FILTER_VALIDATE_INT, ['options' => ['min_range' => $minValue]]))) {
318 throw new InvalidArgumentException($errorMessage);
319 }
320
321 return $int;
322 }
323 }
324