1 | <?php |
||
14 | class JsonStream implements StreamInterface |
||
15 | { |
||
16 | /** @var BufferJsonEncoder The encoder used to produce the JSON stream */ |
||
17 | private $encoder; |
||
18 | |||
19 | /** @var int The current position of the cursor in the JSON stream */ |
||
20 | private $cursor; |
||
21 | |||
22 | /** @var string Buffered output from encoding the value as JSON */ |
||
23 | private $buffer; |
||
24 | |||
25 | /** |
||
26 | * JsonStream constructor. |
||
27 | * @param BufferJsonEncoder|mixed $value A JSON encoder to use or a value to encode |
||
28 | */ |
||
29 | 20 | public function __construct($value) |
|
38 | |||
39 | /** |
||
40 | * Returns the JSON encoder used for the JSON stream. |
||
41 | * @return BufferJsonEncoder The currently used JSON encoder |
||
42 | * @throws \RuntimeException If the stream has been closed |
||
43 | */ |
||
44 | 20 | private function getEncoder() |
|
52 | |||
53 | /** |
||
54 | * Returns the entire JSON stream as a string. |
||
55 | * |
||
56 | * Note that this operation performs rewind operation on the JSON encoder. Whether |
||
57 | * this works or not is dependant on the underlying value being encoded. An empty |
||
58 | * string is returned if the value cannot be encoded. |
||
59 | * |
||
60 | * @return string The entire JSON stream as a string |
||
61 | */ |
||
62 | 4 | public function __toString() |
|
71 | |||
72 | /** |
||
73 | * Frees the JSON encoder from memory and prevents further reading from the JSON stream. |
||
74 | */ |
||
75 | 4 | public function close() |
|
79 | |||
80 | /** |
||
81 | * Detaches the underlying PHP stream and returns it. |
||
82 | * @return null Always returns null as no underlying PHP stream exists |
||
83 | */ |
||
84 | 2 | public function detach() |
|
88 | |||
89 | /** |
||
90 | * Returns the total size of the JSON stream. |
||
91 | * @return null Always returns null as the total size cannot be determined |
||
92 | */ |
||
93 | 2 | public function getSize() |
|
97 | |||
98 | /** |
||
99 | * Returns the current position of the cursor in the JSON stream. |
||
100 | * @return int Current position of the cursor |
||
101 | */ |
||
102 | 4 | public function tell() |
|
107 | |||
108 | /** |
||
109 | * Tells if there are no more bytes to read from the JSON stream. |
||
110 | * @return bool True if there are no more bytes to read, false if there are |
||
111 | */ |
||
112 | 6 | public function eof() |
|
116 | |||
117 | /** |
||
118 | * Tells if the JSON stream is seekable or not. |
||
119 | * @return bool Always returns true as JSON streams as always seekable |
||
120 | */ |
||
121 | 2 | public function isSeekable() |
|
125 | |||
126 | /** |
||
127 | * Seeks the given cursor position in the JSON stream. |
||
128 | * |
||
129 | * If the provided seek position is less than the current cursor position, a rewind |
||
130 | * operation is performed on the underlying JSON encoder. Whether this works or not |
||
131 | * depends on whether the encoded value supports rewinding. |
||
132 | * |
||
133 | * Note that since it's not possible to determine the end of the JSON stream without |
||
134 | * encoding the entire value, it's not possible to set the cursor using SEEK_END |
||
135 | * constant and doing so will result in an exception. |
||
136 | * |
||
137 | * @param int $offset The offset for the cursor. |
||
138 | * @param int $whence Either SEEK_CUR or SEEK_SET to determine new cursor position |
||
139 | */ |
||
140 | 20 | public function seek($offset, $whence = SEEK_SET) |
|
152 | |||
153 | /** |
||
154 | * Calculates new position for the cursor based on offset and whence. |
||
155 | * @param int $offset The cursor offset |
||
156 | * @param int $whence One of the SEEK_* constants |
||
157 | * @return int The new cursor position |
||
158 | */ |
||
159 | 20 | private function calculatePosition($offset, $whence) |
|
171 | |||
172 | /** |
||
173 | * Forwards the JSON stream reading cursor to the given position or to the end of stream. |
||
174 | * @param int $position The new position of the cursor. |
||
175 | */ |
||
176 | 20 | private function forward($position) |
|
201 | |||
202 | /** |
||
203 | * Seeks the beginning of the JSON stream. |
||
204 | * |
||
205 | * If the encoding has already been started, rewinding the encoder may not work, |
||
206 | * if the underlying value being encoded does not support rewinding. |
||
207 | */ |
||
208 | 20 | public function rewind() |
|
212 | |||
213 | /** |
||
214 | * Tells if the JSON stream is writable or not. |
||
215 | * @return bool Always returns false as JSON streams are never writable |
||
216 | */ |
||
217 | 2 | public function isWritable() |
|
221 | |||
222 | /** |
||
223 | * Writes the given bytes to the JSON stream. |
||
224 | * |
||
225 | * As the JSON stream does not represent a writable stream, this method will |
||
226 | * always throw a runtime exception. |
||
227 | * |
||
228 | * @param string $string The bytes to write |
||
229 | * @return int The number of bytes written |
||
230 | * @throws \RuntimeException Always throws a runtime exception |
||
231 | */ |
||
232 | 2 | public function write($string) |
|
236 | |||
237 | /** |
||
238 | * Tells if the JSON stream is readable or not. |
||
239 | * @return bool Always returns true as JSON streams are always readable |
||
240 | */ |
||
241 | 2 | public function isReadable() |
|
245 | |||
246 | /** |
||
247 | * Returns the given number of bytes from the JSON stream. |
||
248 | * |
||
249 | * The underlying value is encoded into JSON until enough bytes have been |
||
250 | * generated to fulfill the requested number of bytes. The extraneous bytes are |
||
251 | * then buffered for the next read from the JSON stream. The stream may return |
||
252 | * fewer number of bytes if the entire value has been encoded and there are no |
||
253 | * more bytes to return. |
||
254 | * |
||
255 | * @param int $length The number of bytes to return |
||
256 | * @return string The bytes read from the JSON stream |
||
257 | */ |
||
258 | 6 | public function read($length) |
|
280 | |||
281 | /** |
||
282 | * Returns the remaining bytes from the JSON stream. |
||
283 | * @return string The remaining bytes from JSON stream |
||
284 | */ |
||
285 | 4 | public function getContents() |
|
300 | |||
301 | /** |
||
302 | * Returns the metadata from the underlying PHP stream. |
||
303 | * |
||
304 | * As no underlying PHP stream exists for the JSON stream, this method will |
||
305 | * always return an empty array or a null. |
||
306 | * |
||
307 | * @param string|null The key of the value to return |
||
308 | * @return array|null Always returns an empty array or a null |
||
309 | */ |
||
310 | 2 | public function getMetadata($key = null) |
|
314 | } |
||
315 |
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.