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) |
|
96 | { |
||
97 | 10 | $this->resetProperties(); |
|
98 | 10 | $this->document = $document; |
|
99 | 10 | } |
|
100 | |||
101 | /** |
||
102 | * @inheritdoc |
||
103 | */ |
||
104 | 2 | public function __clone() |
|
108 | |||
109 | /** |
||
110 | * Return a new instance from a SplFileObject |
||
111 | * |
||
112 | * @param SplFileObject $file |
||
113 | * |
||
114 | * @return static |
||
115 | */ |
||
116 | 12 | public static function createFromFileObject(SplFileObject $file): self |
|
128 | |||
129 | /** |
||
130 | * Return a new instance from a PHP resource stream |
||
131 | * |
||
132 | * @param resource $stream |
||
133 | * |
||
134 | * @return static |
||
135 | */ |
||
136 | 4 | public static function createFromStream($stream): self |
|
140 | |||
141 | /** |
||
142 | * Return a new instance from a string |
||
143 | * |
||
144 | * @param string $content the CSV document as a string |
||
145 | * |
||
146 | * @return static |
||
147 | */ |
||
148 | 4 | public static function createFromString(string $content): self |
|
152 | |||
153 | /** |
||
154 | * Return a new instance from a file path |
||
155 | * |
||
156 | * @param string $path file path |
||
157 | * @param string $open_mode the file open mode flag |
||
158 | * @param resource|null $context the resource context |
||
159 | * |
||
160 | * @return static |
||
161 | */ |
||
162 | 4 | public static function createFromPath(string $path, string $open_mode = 'r+', $context = null): self |
|
166 | |||
167 | /** |
||
168 | * Returns the class filter mode |
||
169 | * |
||
170 | * @return int |
||
171 | */ |
||
172 | 2 | public function getStreamFilterMode(): int |
|
176 | |||
177 | /** |
||
178 | * Returns the current field delimiter |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 2 | public function getDelimiter(): string |
|
186 | |||
187 | /** |
||
188 | * Returns the current field enclosure |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | 2 | public function getEnclosure(): string |
|
196 | |||
197 | /** |
||
198 | * Returns the current field escape character |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 2 | public function getEscape(): string |
|
206 | |||
207 | /** |
||
208 | * Returns the BOM sequence in use on Output methods |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | 2 | public function getOutputBOM(): string |
|
216 | |||
217 | /** |
||
218 | * Returns the BOM sequence of the given CSV |
||
219 | * |
||
220 | * @return string |
||
221 | */ |
||
222 | 6 | public function getInputBOM(): string |
|
233 | |||
234 | /** |
||
235 | * Tells whether the stream filter capabilities can be used |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 4 | public function supportsStreamFilter(): bool |
|
243 | |||
244 | /** |
||
245 | * Tell whether the specify stream filter is attach to the current stream |
||
246 | * |
||
247 | * @param string $filtername |
||
248 | * @return bool |
||
249 | */ |
||
250 | 2 | public function hasStreamFilter(string $filtername): bool |
|
254 | |||
255 | /** |
||
256 | * Retrieves the CSV content |
||
257 | * |
||
258 | * @return string |
||
259 | */ |
||
260 | 12 | public function __toString(): string |
|
269 | |||
270 | /** |
||
271 | * Retuns the CSV document as a Generator of string chunk |
||
272 | * |
||
273 | * @param int $length number of bytes read |
||
274 | * |
||
275 | * @return Generator |
||
276 | */ |
||
277 | 10 | public function chunk(int $length): Generator |
|
297 | |||
298 | /** |
||
299 | * Outputs all data on the CSV file |
||
300 | * |
||
301 | * @param string $filename CSV downloaded name if present adds extra headers |
||
302 | * |
||
303 | * @return int Returns the number of characters read from the handle |
||
304 | * and passed through to the output. |
||
305 | */ |
||
306 | 4 | public function output(string $filename = null): int |
|
326 | |||
327 | /** |
||
328 | * Sets the field delimiter |
||
329 | * |
||
330 | * @param string $delimiter |
||
331 | * |
||
332 | * @return static |
||
333 | */ |
||
334 | 2 | public function setDelimiter(string $delimiter): self |
|
344 | |||
345 | /** |
||
346 | * Reset dynamic object properties to improve performance |
||
347 | */ |
||
348 | 10 | protected function resetProperties() |
|
351 | |||
352 | /** |
||
353 | * Sets the field enclosure |
||
354 | * |
||
355 | * @param string $enclosure |
||
356 | * |
||
357 | * @return static |
||
358 | */ |
||
359 | 2 | public function setEnclosure(string $enclosure): self |
|
369 | |||
370 | /** |
||
371 | * Sets the field escape character |
||
372 | * |
||
373 | * @param string $escape |
||
374 | * |
||
375 | * @return static |
||
376 | */ |
||
377 | 2 | public function setEscape(string $escape): self |
|
388 | |||
389 | /** |
||
390 | * Sets the BOM sequence to prepend the CSV on output |
||
391 | * |
||
392 | * @param string $str The BOM sequence |
||
393 | * |
||
394 | * @return static |
||
395 | */ |
||
396 | 6 | public function setOutputBOM(string $str): self |
|
402 | |||
403 | /** |
||
404 | * append a stream filter |
||
405 | * |
||
406 | * @param string $filtername a string or an object that implements the '__toString' method |
||
407 | * @param mixed $params additional parameters for the filter |
||
408 | * |
||
409 | * @throws LogicException If the stream filter API can not be used |
||
410 | * |
||
411 | * @return static |
||
412 | */ |
||
413 | 10 | public function addStreamFilter(string $filtername, $params = null): self |
|
425 | |||
426 | /** |
||
427 | * The destructor |
||
428 | */ |
||
429 | 14 | public function __destruct() |
|
441 | } |
||
442 |