1 | <?php |
||
33 | class Stream implements SeekableIterator |
||
34 | { |
||
35 | use ValidatorTrait; |
||
36 | |||
37 | /** |
||
38 | * Attached filters |
||
39 | * |
||
40 | * @var resource[] |
||
41 | */ |
||
42 | protected $filters = []; |
||
43 | |||
44 | /** |
||
45 | * stream resource |
||
46 | * |
||
47 | * @var resource |
||
48 | */ |
||
49 | protected $stream; |
||
50 | |||
51 | /** |
||
52 | * Tell whether the stream should be closed on object destruction |
||
53 | * |
||
54 | * @var bool |
||
55 | */ |
||
56 | protected $should_close_stream = false; |
||
57 | |||
58 | /** |
||
59 | * Current iterator value |
||
60 | * |
||
61 | * @var mixed |
||
62 | */ |
||
63 | protected $value; |
||
64 | |||
65 | /** |
||
66 | * Current iterator key |
||
67 | * |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $offset; |
||
71 | |||
72 | /** |
||
73 | * Flags for the Document |
||
74 | * |
||
75 | * @var int |
||
76 | */ |
||
77 | protected $flags = 0; |
||
78 | |||
79 | /** |
||
80 | * the field delimiter (one character only) |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $delimiter = ','; |
||
85 | |||
86 | /** |
||
87 | * the field enclosure character (one character only) |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $enclosure = '"'; |
||
92 | |||
93 | /** |
||
94 | * the field escape character (one character only) |
||
95 | * |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $escape = '\\'; |
||
99 | |||
100 | /** |
||
101 | * New instance |
||
102 | * |
||
103 | * @param resource $resource stream type resource |
||
104 | * |
||
105 | * @throws RuntimeException if the argument passed is not a seeakable stream resource |
||
106 | */ |
||
107 | 34 | public function __construct($resource) |
|
123 | |||
124 | /** |
||
125 | * @inheritdoc |
||
126 | */ |
||
127 | public function __destruct() |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | 2 | public function __clone() |
|
149 | |||
150 | /** |
||
151 | * @inheritdoc |
||
152 | */ |
||
153 | 2 | public function __debugInfo() |
|
162 | |||
163 | /** |
||
164 | * Return a new instance from a file path |
||
165 | * |
||
166 | * @param string $path file path |
||
167 | * @param string $open_mode the file open mode flag |
||
168 | * @param resource|null $context the resource context |
||
169 | * |
||
170 | * @throws RuntimeException if the stream resource can not be created |
||
171 | * |
||
172 | * @return static |
||
173 | */ |
||
174 | 12 | public static function createFromPath(string $path, string $open_mode = 'r', $context = null): self |
|
191 | |||
192 | /** |
||
193 | * Return a new instance from a string |
||
194 | * |
||
195 | * @param string $content the CSV document as a string |
||
196 | * |
||
197 | * @return static |
||
198 | */ |
||
199 | 10 | public static function createFromString(string $content): self |
|
209 | |||
210 | /** |
||
211 | * append a filter |
||
212 | * |
||
213 | * @see http://php.net/manual/en/function.stream-filter-append.php |
||
214 | * |
||
215 | * @param string $filtername |
||
216 | * @param int $read_write |
||
217 | * @param mixed $params |
||
218 | * |
||
219 | * @throws RuntimeException if the filter can not be appended |
||
220 | */ |
||
221 | 10 | public function appendFilter(string $filtername, int $read_write, $params = null) |
|
231 | |||
232 | /** |
||
233 | * Set CSV control |
||
234 | * |
||
235 | * @see http://php.net/manual/en/splfileobject.setcsvcontrol.php |
||
236 | * |
||
237 | * @param string $delimiter |
||
238 | * @param string $enclosure |
||
239 | * @param string $escape |
||
240 | */ |
||
241 | 22 | public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
247 | |||
248 | /** |
||
249 | * Set CSV control |
||
250 | * |
||
251 | * @see http://php.net/manual/en/splfileobject.getcsvcontrol.php |
||
252 | * |
||
253 | * @return string[] |
||
254 | */ |
||
255 | 28 | public function getCsvControl() |
|
259 | |||
260 | /** |
||
261 | * Set CSV stream flags |
||
262 | * |
||
263 | * @see http://php.net/manual/en/splfileobject.setflags.php |
||
264 | * |
||
265 | * @param int $flags |
||
266 | */ |
||
267 | 22 | public function setFlags(int $flags) |
|
271 | |||
272 | /** |
||
273 | * Write a field array as a CSV line |
||
274 | * |
||
275 | * @see http://php.net/manual/en/splfileobject.fputcsv.php |
||
276 | * |
||
277 | * @param array $fields |
||
278 | * @param string $delimiter |
||
279 | * @param string $enclosure |
||
280 | * @param string $escape |
||
281 | * |
||
282 | * @return int|bool |
||
283 | */ |
||
284 | 14 | public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\') |
|
294 | |||
295 | /** |
||
296 | * Get line number |
||
297 | * |
||
298 | * @see http://php.net/manual/en/splfileobject.key.php |
||
299 | * |
||
300 | * @return int |
||
301 | */ |
||
302 | 12 | public function key() |
|
306 | |||
307 | /** |
||
308 | * Read next line |
||
309 | * |
||
310 | * @see http://php.net/manual/en/splfileobject.next.php |
||
311 | * |
||
312 | */ |
||
313 | 12 | public function next() |
|
318 | |||
319 | /** |
||
320 | * Rewind the file to the first line |
||
321 | * |
||
322 | * @see http://php.net/manual/en/splfileobject.rewind.php |
||
323 | * |
||
324 | */ |
||
325 | 22 | public function rewind() |
|
334 | |||
335 | /** |
||
336 | * Not at EOF |
||
337 | * |
||
338 | * @see http://php.net/manual/en/splfileobject.valid.php |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | 20 | public function valid() |
|
350 | |||
351 | /** |
||
352 | * Retrieves the current line of the file. |
||
353 | * |
||
354 | * @see http://php.net/manual/en/splfileobject.current.php |
||
355 | * |
||
356 | * @return mixed |
||
357 | */ |
||
358 | 24 | public function current() |
|
368 | |||
369 | /** |
||
370 | * Retrieves the current line as a CSV Record |
||
371 | * |
||
372 | * @return array|bool |
||
373 | */ |
||
374 | 22 | protected function getCurrentRecord() |
|
382 | |||
383 | /** |
||
384 | * Seek to specified line |
||
385 | * |
||
386 | * @see http://php.net/manual/en/splfileobject.seek.php |
||
387 | * |
||
388 | * @param int $position |
||
389 | */ |
||
390 | 8 | public function seek($position) |
|
405 | |||
406 | /** |
||
407 | * Output all remaining data on a file pointer |
||
408 | * |
||
409 | * @see http://php.net/manual/en/splfileobject.fpatssthru.php |
||
410 | * |
||
411 | * @return int |
||
412 | */ |
||
413 | 2 | public function fpassthru() |
|
417 | |||
418 | /** |
||
419 | * Read from file |
||
420 | * |
||
421 | * @see http://php.net/manual/en/splfileobject.fread.php |
||
422 | * |
||
423 | * @param int $length The number of bytes to read |
||
424 | * |
||
425 | * @return string|false |
||
426 | */ |
||
427 | 8 | public function fread($length) |
|
431 | |||
432 | /** |
||
433 | * Seek to a position |
||
434 | * |
||
435 | * @see http://php.net/manual/en/splfileobject.fseek.php |
||
436 | * |
||
437 | * @param int $offset |
||
438 | * @param int $whence |
||
439 | * |
||
440 | * @return int |
||
441 | */ |
||
442 | 10 | public function fseek(int $offset, int $whence = SEEK_SET) |
|
446 | |||
447 | /** |
||
448 | * Write to stream |
||
449 | * |
||
450 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
451 | * |
||
452 | * @param string $str |
||
453 | * @param int $length |
||
454 | * |
||
455 | * @return int|bool |
||
456 | */ |
||
457 | 2 | public function fwrite(string $str, int $length = 0) |
|
461 | |||
462 | /** |
||
463 | * Flushes the output to a file |
||
464 | * |
||
465 | * @see http://php.net/manual/en/splfileobject.fwrite.php |
||
466 | * |
||
467 | * @return bool |
||
468 | */ |
||
469 | 2 | public function fflush() |
|
473 | } |
||
474 |