|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * League.Csv (https://csv.thephpleague.com) |
| 5 | + * |
| 6 | + * (c) Ignace Nyamagana Butera <nyamsprod@gmail.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace League\Csv; |
| 15 | + |
| 16 | +use Iterator; |
| 17 | +use SplFileObject; |
| 18 | +use const SEEK_SET; |
| 19 | + |
| 20 | +/** |
| 21 | + * @internal |
| 22 | + */ |
| 23 | +final class SkipBomIterator implements Iterator |
| 24 | +{ |
| 25 | + public static function fromDocument( |
| 26 | + SplFileObject|Stream $document, |
| 27 | + string $delimiter = ',', |
| 28 | + string $enclosure = '"', |
| 29 | + string $escape = '\\', |
| 30 | + ): self { |
| 31 | + |
| 32 | + return new self( |
| 33 | + $document, |
| 34 | + Bom::tryFromSequence($document)?->length() ?? 0, |
| 35 | + $delimiter, |
| 36 | + $enclosure, |
| 37 | + $escape, |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + public function __construct( |
| 42 | + private readonly SplFileObject|Stream $file, |
| 43 | + private readonly int $offset, |
| 44 | + private readonly string $delimiter = ',', |
| 45 | + private readonly string $enclosure = '"', |
| 46 | + private readonly string $escape = '\\', |
| 47 | + ) { |
| 48 | + $this->file->setFlags(SplFileObject::READ_CSV); |
| 49 | + $this->file->setCsvControl($this->delimiter, $this->enclosure, $this->escape); |
| 50 | + } |
| 51 | + |
| 52 | + public function current(): mixed |
| 53 | + { |
| 54 | + return $this->file->current(); |
| 55 | + } |
| 56 | + |
| 57 | + public function fseek(int $offset, int $whence): int |
| 58 | + { |
| 59 | + return $this->file->fseek($offset, $whence); |
| 60 | + } |
| 61 | + |
| 62 | + public function key(): int |
| 63 | + { |
| 64 | + return $this->file->key(); |
| 65 | + } |
| 66 | + |
| 67 | + public function next(): void |
| 68 | + { |
| 69 | + $this->file->next(); |
| 70 | + } |
| 71 | + |
| 72 | + public function rewind(): void |
| 73 | + { |
| 74 | + $this->file->rewind(); |
| 75 | + if (0 !== $this->offset) { |
| 76 | + $this->fseek($this->offset, SEEK_SET); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public function valid(): bool |
| 81 | + { |
| 82 | + return !$this->file->eof(); |
| 83 | + } |
| 84 | +} |
0 commit comments