1 | <?php |
||
32 | class Document implements SeekableIterator |
||
33 | { |
||
34 | use ValidatorTrait; |
||
35 | |||
36 | /** |
||
37 | * Attached filters |
||
38 | * |
||
39 | * @var resource[] |
||
40 | */ |
||
41 | protected $filters = []; |
||
42 | |||
43 | /** |
||
44 | * stream resource |
||
45 | * |
||
46 | * @var resource |
||
47 | */ |
||
48 | protected $stream; |
||
49 | |||
50 | /** |
||
51 | * Tell whether the stream should be closed on object destruction |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $should_close_stream = false; |
||
56 | |||
57 | /** |
||
58 | * Current iterator value |
||
59 | * |
||
60 | * @var mixed |
||
61 | */ |
||
62 | protected $value; |
||
63 | |||
64 | /** |
||
65 | * Current iterator key |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $offset; |
||
70 | |||
71 | /** |
||
72 | * Flags for the Document |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | protected $flags = 0; |
||
77 | |||
78 | /** |
||
79 | * the field delimiter (one character only) |
||
80 | * |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $delimiter = ','; |
||
84 | |||
85 | /** |
||
86 | * the field enclosure character (one character only) |
||
87 | * |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $enclosure = '"'; |
||
91 | |||
92 | /** |
||
93 | * the field escape character (one character only) |
||
94 | * |
||
95 | * @var string |
||
96 | */ |
||
97 | protected $escape = '\\'; |
||
98 | |||
99 | /** |
||
100 | * New instance |
||
101 | * |
||
102 | * @param resource $resource stream type resource |
||
103 | * |
||
104 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
105 | */ |
||
106 | 34 | public function __construct($resource) |
|
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function __destruct() |
||
140 | |||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | 2 | public function __clone() |
|
148 | |||
149 | /** |
||
150 | * Return a new instance from a file path |
||
151 | * |
||
152 | * @param string $path file path |
||
153 | * @param string $open_mode the file open mode flag |
||
154 | * @param resource|null $context the resource context |
||
155 | * |
||
156 | * @throws RuntimeException if the stream resource can not be created |
||
157 | * |
||
158 | * @return static |
||
159 | */ |
||
160 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
177 | |||
178 | /** |
||
179 | * Return a new instance from a string |
||
180 | * |
||
181 | * @param string $content the CSV document as a string |
||
182 | * |
||
183 | * @return static |
||
184 | */ |
||
185 | 10 | public static function createFromString(string $content): self |
|
195 | |||
196 | /** |
||
197 | * append a filter |
||
198 | * |
||
199 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
200 | * |
||
201 | * @param string $filtername |
||
202 | * @param int $read_write |
||
203 | * @param mixed $params |
||
204 | * |
||
205 | * @throws RuntimeException if the filter can not be appended |
||
206 | */ |
||
207 | 10 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
217 | |||
218 | /** |
||
219 | * Set CSV control |
||
220 | * |
||
221 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
222 | * |
||
223 | * @param string $delimiter |
||
224 | * @param string $enclosure |
||
225 | * @param string $escape |
||
226 | */ |
||
227 | 22 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
233 | |||
234 | /** |
||
235 | * Set CSV control |
||
236 | * |
||
237 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
238 | * |
||
239 | * @return string[] |
||
240 | */ |
||
241 | 28 | public function getCsvControl() |
|
245 | |||
246 | /** |
||
247 | * Set Document Flags |
||
248 | * |
||
249 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
250 | * |
||
251 | * @param int $flags |
||
252 | */ |
||
253 | 22 | public function setFlags(int $flags) |
|
257 | |||
258 | /** |
||
259 | * Write a field array as a CSV line |
||
260 | * |
||
261 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
262 | * |
||
263 | * @param array $fields |
||
264 | * @param string $delimiter |
||
265 | * @param string $enclosure |
||
266 | * @param string $escape |
||
267 | * |
||
268 | * @return int|bool |
||
269 | */ |
||
270 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
280 | |||
281 | /** |
||
282 | * Get line number |
||
283 | * |
||
284 | * @see http://php.net/manual/en/splfileobject.key.php |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | 12 | public function key() |
|
292 | |||
293 | /** |
||
294 | * Read next line |
||
295 | * |
||
296 | * @see http://php.net/manual/en/splfileobject.next.php |
||
297 | * |
||
298 | */ |
||
299 | 12 | public function next() |
|
304 | |||
305 | /** |
||
306 | * Rewind the file to the first line |
||
307 | * |
||
308 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
309 | * |
||
310 | */ |
||
311 | 22 | public function rewind() |
|
320 | |||
321 | /** |
||
322 | * Not at EOF |
||
323 | * |
||
324 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | 20 | public function valid() |
|
336 | |||
337 | /** |
||
338 | * Retrieves the current line of the file. |
||
339 | * |
||
340 | * @see http://php.net/manual/en/splfileobject.current.php |
||
341 | * |
||
342 | * @return mixed |
||
343 | */ |
||
344 | 24 | public function current() |
|
354 | |||
355 | /** |
||
356 | * Retrieves the current line as a CSV Record |
||
357 | * |
||
358 | * @return array|bool |
||
359 | */ |
||
360 | 22 | protected function getCurrentRecord() |
|
368 | |||
369 | /** |
||
370 | * Seek to specified line |
||
371 | * |
||
372 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
373 | * |
||
374 | * @param int $position |
||
375 | */ |
||
376 | 4 | public function seek($position) |
|
388 | |||
389 | /** |
||
390 | * Output all remaining data on a file pointer |
||
391 | * |
||
392 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
393 | * |
||
394 | * @return int |
||
395 | */ |
||
396 | 2 | public function fpassthru() |
|
400 | |||
401 | /** |
||
402 | * Read from file |
||
403 | * |
||
404 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
405 | * |
||
406 | * @param int $length The number of bytes to read |
||
407 | * |
||
408 | * @return string|false |
||
409 | */ |
||
410 | 8 | public function fread($length) |
|
414 | |||
415 | /** |
||
416 | * Seek to a position |
||
417 | * |
||
418 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
419 | * |
||
420 | * @param int $offset |
||
421 | * @param int $whence |
||
422 | * |
||
423 | * @return int |
||
424 | */ |
||
425 | 4 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
429 | |||
430 | /** |
||
431 | * Write to stream |
||
432 | * |
||
433 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
434 | * |
||
435 | * @param string $str |
||
436 | * @param int $length |
||
437 | * |
||
438 | * @return int|bool |
||
439 | */ |
||
440 | 2 | public function fwrite(string $str, int $length = 0) |
|
444 | |||
445 | /** |
||
446 | * Flushes the output to a file |
||
447 | * |
||
448 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
449 | * |
||
450 | * @return bool |
||
451 | */ |
||
452 | 8 | public function fflush() |
|
456 | |||
457 | /** |
||
458 | * @inheritdoc |
||
459 | */ |
||
460 | 2 | public function __debugInfo() |
|
469 | } |
||
470 |