1 | <?php |
||
2 | |||
3 | namespace sweetrdf\InMemoryStoreSqlite; |
||
4 | |||
5 | /** |
||
6 | * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under |
||
7 | * the terms of the GPL-2 license. |
||
8 | * |
||
9 | * (c) Konrad Abicht <[email protected]> |
||
10 | * (c) Benjamin Nowack |
||
11 | * |
||
12 | * For the full copyright and license information, please view the LICENSE |
||
13 | * file that was distributed with this source code. |
||
14 | */ |
||
15 | |||
16 | /** |
||
17 | * Provides a way to read a given string in chunks. |
||
18 | */ |
||
19 | class StringReader |
||
20 | { |
||
21 | private ?string $base; |
||
22 | |||
23 | private ?string $uri; |
||
24 | |||
25 | private array $stream = []; |
||
26 | |||
27 | 2 | public function getBase(): ?string |
|
28 | { |
||
29 | 2 | return $this->base; |
|
30 | } |
||
31 | |||
32 | 2 | public function init($path, $data) |
|
33 | { |
||
34 | 2 | $this->base = calcBase($path); |
|
35 | 2 | $this->uri = calcURI($path, $this->base); |
|
36 | |||
37 | 2 | $this->stream = [ |
|
38 | 2 | 'type' => 'data', |
|
39 | 2 | 'pos' => 0, |
|
40 | 2 | 'headers' => [], |
|
41 | 2 | 'size' => \strlen($data), |
|
42 | 2 | 'data' => $data, |
|
43 | 2 | 'buffer' => '', |
|
44 | 2 | ]; |
|
45 | } |
||
46 | |||
47 | 2 | public function readStream(int $d_size = 1024): string |
|
48 | { |
||
49 | 2 | $s = $this->stream; |
|
50 | 2 | $r = $this->stream['buffer']; |
|
51 | |||
52 | 2 | $s['buffer'] = ''; |
|
53 | 2 | if ($s['size']) { |
|
54 | 2 | $d_size = min($d_size, $this->stream['size'] - $this->stream['pos']); |
|
55 | } |
||
56 | |||
57 | 2 | if ('data' == $this->stream['type']) { |
|
58 | 2 | if ($d_size > 0) { |
|
59 | 2 | $d = substr($this->stream['data'], $s['pos'], $d_size); |
|
60 | } else { |
||
61 | 2 | $d = ''; |
|
62 | } |
||
63 | } |
||
64 | |||
65 | 2 | $s['pos'] += \strlen($d); |
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
![]() |
|||
66 | |||
67 | 2 | $this->stream = $s; |
|
68 | |||
69 | 2 | return $r.$d; |
|
70 | } |
||
71 | } |
||
72 |