1 | <?php |
||
30 | abstract class AbstractCsv implements ByteSequence |
||
31 | { |
||
32 | use ValidatorTrait; |
||
33 | |||
34 | /** |
||
35 | * The CSV document |
||
36 | * |
||
37 | * @var StreamIterator|SplFileObject |
||
38 | */ |
||
39 | protected $document; |
||
40 | |||
41 | /** |
||
42 | * the field delimiter (one character only) |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $delimiter = ','; |
||
47 | |||
48 | /** |
||
49 | * the field enclosure character (one character only) |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $enclosure = '"'; |
||
54 | |||
55 | /** |
||
56 | * the field escape character (one character only) |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $escape = '\\'; |
||
61 | |||
62 | /** |
||
63 | * The CSV document BOM sequence |
||
64 | * |
||
65 | * @var string|null |
||
66 | */ |
||
67 | protected $input_bom = null; |
||
68 | |||
69 | /** |
||
70 | * The Output file BOM character |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $output_bom = ''; |
||
75 | |||
76 | /** |
||
77 | * collection of stream filters |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $stream_filters = []; |
||
82 | |||
83 | /** |
||
84 | * The stream filter mode (read or write) |
||
85 | * |
||
86 | * @var int |
||
87 | */ |
||
88 | protected $stream_filter_mode; |
||
89 | |||
90 | /** |
||
91 | * New instance |
||
92 | * |
||
93 | * @param SplFileObject|StreamIterator $document The CSV Object instance |
||
94 | */ |
||
95 | 10 | protected function __construct($document) |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | 2 | public function __clone() |
|
107 | |||
108 | /** |
||
109 | * Return a new instance from a SplFileObject |
||
110 | * |
||
111 | * @param SplFileObject $file |
||
112 | * |
||
113 | * @return static |
||
114 | */ |
||
115 | 12 | public static function createFromFileObject(SplFileObject $file): self |
|
127 | |||
128 | /** |
||
129 | * Return a new instance from a PHP resource stream |
||
130 | * |
||
131 | * @param resource $stream |
||
132 | * |
||
133 | * @return static |
||
134 | */ |
||
135 | 4 | public static function createFromStream($stream): self |
|
139 | |||
140 | /** |
||
141 | * Return a new instance from a string |
||
142 | * |
||
143 | * @param string $content the CSV document as a string |
||
144 | * |
||
145 | * @return static |
||
146 | */ |
||
147 | 4 | public static function createFromString(string $content): self |
|
151 | |||
152 | /** |
||
153 | * Return a new instance from a file path |
||
154 | * |
||
155 | * @param string $path file path |
||
156 | * @param string $open_mode the file open mode flag |
||
157 | * @param resource|null $context the resource context |
||
158 | * |
||
159 | * @return static |
||
160 | */ |
||
161 | 4 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null): self |
|
165 | |||
166 | /** |
||
167 | * Returns the class filter mode |
||
168 | * |
||
169 | * @return int |
||
170 | */ |
||
171 | 2 | public function getStreamFilterMode(): int |
|
175 | |||
176 | /** |
||
177 | * Returns the current field delimiter |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 2 | public function getDelimiter(): string |
|
185 | |||
186 | /** |
||
187 | * Returns the current field enclosure |
||
188 | * |
||
189 | * @return string |
||
190 | */ |
||
191 | 2 | public function getEnclosure(): string |
|
195 | |||
196 | /** |
||
197 | * Returns the current field escape character |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | 2 | public function getEscape(): string |
|
205 | |||
206 | /** |
||
207 | * Returns the BOM sequence in use on Output methods |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 2 | public function getOutputBOM(): string |
|
215 | |||
216 | /** |
||
217 | * Returns the BOM sequence of the given CSV |
||
218 | * |
||
219 | * @return string |
||
220 | */ |
||
221 | 6 | public function getInputBOM(): string |
|
232 | |||
233 | /** |
||
234 | * Tells whether the stream filter capabilities can be used |
||
235 | * |
||
236 | * @return bool |
||
237 | */ |
||
238 | 4 | public function supportsStreamFilter(): bool |
|
242 | |||
243 | /** |
||
244 | * Tell whether the specify stream filter is attach to the current stream |
||
245 | * |
||
246 | * @param string $filtername |
||
247 | * @return bool |
||
248 | */ |
||
249 | 2 | public function hasStreamFilter(string $filtername): bool |
|
253 | |||
254 | /** |
||
255 | * Retrieves the CSV content |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | 12 | public function __toString(): string |
|
268 | |||
269 | /** |
||
270 | * Retuns the CSV document as a Generator of string chunk |
||
271 | * |
||
272 | * @param int $length number of bytes read |
||
273 | * |
||
274 | * @return Generator |
||
275 | */ |
||
276 | 10 | public function chunk(int $length): Generator |
|
296 | |||
297 | /** |
||
298 | * Outputs all data on the CSV file |
||
299 | * |
||
300 | * @param string $filename CSV downloaded name if present adds extra headers |
||
301 | * |
||
302 | * @return int Returns the number of characters read from the handle |
||
303 | * and passed through to the output. |
||
304 | */ |
||
305 | 4 | public function output(string $filename = null): int |
|
325 | |||
326 | /** |
||
327 | * Sets the field delimiter |
||
328 | * |
||
329 | * @param string $delimiter |
||
330 | * |
||
331 | * @return static |
||
332 | */ |
||
333 | 2 | public function setDelimiter(string $delimiter): self |
|
343 | |||
344 | /** |
||
345 | * Reset dynamic object properties to improve performance |
||
346 | */ |
||
347 | 2 | protected function resetProperties() |
|
350 | |||
351 | /** |
||
352 | * Sets the field enclosure |
||
353 | * |
||
354 | * @param string $enclosure |
||
355 | * |
||
356 | * @return static |
||
357 | */ |
||
358 | 2 | public function setEnclosure(string $enclosure): self |
|
368 | |||
369 | /** |
||
370 | * Sets the field escape character |
||
371 | * |
||
372 | * @param string $escape |
||
373 | * |
||
374 | * @return static |
||
375 | */ |
||
376 | 2 | public function setEscape(string $escape): self |
|
387 | |||
388 | /** |
||
389 | * Sets the BOM sequence to prepend the CSV on output |
||
390 | * |
||
391 | * @param string $str The BOM sequence |
||
392 | * |
||
393 | * @return static |
||
394 | */ |
||
395 | 6 | public function setOutputBOM(string $str): self |
|
401 | |||
402 | /** |
||
403 | * append a stream filter |
||
404 | * |
||
405 | * @param string $filtername a string or an object that implements the '__toString' method |
||
406 | * @param mixed $params additional parameters for the filter |
||
407 | * |
||
408 | * @throws LogicException If the stream filter API can not be used |
||
409 | * |
||
410 | * @return static |
||
411 | */ |
||
412 | 10 | public function addStreamFilter(string $filtername, $params = null): self |
|
424 | |||
425 | /** |
||
426 | * The destructor |
||
427 | */ |
||
428 | 12 | public function __destruct() |
|
440 | } |
||
441 |