Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.67% |
11 / 12 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
| StringSource | |
91.67% |
11 / 12 |
|
87.50% |
7 / 8 |
9.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| read | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| readByte | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
2.15 | |||
| peek | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| seek | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| tell | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| size | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isEof | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Phpdftk\Pdf\Reader\Tokenizer; |
| 6 | |
| 7 | /** |
| 8 | * Tokenizer source backed by an in-memory string -- used by |
| 9 | * {@see \Phpdftk\Pdf\Reader\PdfReader::fromString()}. |
| 10 | */ |
| 11 | final class StringSource implements Source |
| 12 | { |
| 13 | private int $position = 0; |
| 14 | private readonly int $length; |
| 15 | |
| 16 | public function __construct(private readonly string $data) |
| 17 | { |
| 18 | $this->length = strlen($data); |
| 19 | } |
| 20 | |
| 21 | public function read(int $length): string |
| 22 | { |
| 23 | $result = substr($this->data, $this->position, $length); |
| 24 | $this->position += strlen($result); |
| 25 | return $result; |
| 26 | } |
| 27 | |
| 28 | public function readByte(): ?string |
| 29 | { |
| 30 | if ($this->position >= $this->length) { |
| 31 | return null; |
| 32 | } |
| 33 | return $this->data[$this->position++]; |
| 34 | } |
| 35 | |
| 36 | public function peek(int $length = 1): string |
| 37 | { |
| 38 | return substr($this->data, $this->position, $length); |
| 39 | } |
| 40 | |
| 41 | public function seek(int $offset): void |
| 42 | { |
| 43 | $this->position = max(0, min($offset, $this->length)); |
| 44 | } |
| 45 | |
| 46 | public function tell(): int |
| 47 | { |
| 48 | return $this->position; |
| 49 | } |
| 50 | |
| 51 | public function size(): int |
| 52 | { |
| 53 | return $this->length; |
| 54 | } |
| 55 | |
| 56 | public function isEof(): bool |
| 57 | { |
| 58 | return $this->position >= $this->length; |
| 59 | } |
| 60 | } |