1 | <?php |
||
17 | class MemoryStream implements InputOutputStream |
||
18 | { |
||
19 | use Interpolator { |
||
20 | interpolate as tinterpolate; |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * The local memory buffer |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | private $memory; |
||
29 | |||
30 | /** |
||
31 | * Current position in memory buffer |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | private $current; |
||
36 | |||
37 | /** |
||
38 | * Whether it is possible to perform reading action |
||
39 | * |
||
40 | * @var boolean |
||
41 | */ |
||
42 | private $ready; |
||
43 | |||
44 | /** |
||
45 | * Whether stream is closed |
||
46 | * |
||
47 | * @var boolean |
||
48 | */ |
||
49 | private $closed; |
||
50 | |||
51 | /** |
||
52 | * Create a new MemoryStream |
||
53 | * |
||
54 | * @param InputStream $in |
||
55 | * optional existing input stream - will be copied |
||
56 | */ |
||
57 | 50 | public function __construct(InputStream $in = null) |
|
72 | |||
73 | /** |
||
74 | * |
||
75 | * {@inheritdoc} |
||
76 | * @see \Generics\Streams\Stream::close() |
||
77 | */ |
||
78 | 5 | public function close() |
|
85 | |||
86 | /** |
||
87 | * |
||
88 | * {@inheritdoc} |
||
89 | * @see \Generics\Streams\Stream::ready() |
||
90 | */ |
||
91 | 38 | public function ready(): bool |
|
95 | |||
96 | /** |
||
97 | * |
||
98 | * {@inheritdoc} |
||
99 | * @see \Generics\Streams\OutputStream::write() |
||
100 | */ |
||
101 | 45 | public function write($buffer) |
|
109 | |||
110 | /** |
||
111 | * |
||
112 | * {@inheritdoc} |
||
113 | * @see \Generics\Streams\InputStream::read() |
||
114 | */ |
||
115 | 41 | public function read($length = 1, $offset = null): string |
|
143 | |||
144 | /** |
||
145 | * |
||
146 | * {@inheritdoc} |
||
147 | * @see \Countable::count() |
||
148 | */ |
||
149 | 25 | public function count(): int |
|
159 | |||
160 | /** |
||
161 | * |
||
162 | * {@inheritdoc} |
||
163 | * @see \Generics\Resettable::reset() |
||
164 | */ |
||
165 | 28 | public function reset() |
|
173 | |||
174 | /** |
||
175 | * Write to stream by interpolation of context vars into a string |
||
176 | * |
||
177 | * @param string $string |
||
178 | * The string to interpolate, may contains placeholders in format {placeholder}. |
||
179 | * @param array $context |
||
180 | * The context array containing the associative replacers and its values. |
||
181 | */ |
||
182 | 36 | public function interpolate($string, array $context) |
|
186 | |||
187 | /** |
||
188 | * |
||
189 | * {@inheritdoc} |
||
190 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
191 | */ |
||
192 | 1 | public function isWriteable(): bool |
|
196 | |||
197 | /** |
||
198 | * |
||
199 | * {@inheritdoc} |
||
200 | * @see \Generics\Streams\OutputStream::flush() |
||
201 | */ |
||
202 | 6 | public function flush() |
|
212 | |||
213 | /** |
||
214 | * |
||
215 | * {@inheritdoc} |
||
216 | * @see \Generics\Streams\Stream::isOpen() |
||
217 | */ |
||
218 | public function isOpen(): bool |
||
222 | |||
223 | /** |
||
224 | * Retrieve the whole memory string content |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | 1 | public function slurp(): string |
|
237 | } |
||
238 |