class JsonStream implements StreamInterface (View source)

Provides a http stream interface for encoding JSON.

Methods

__construct(BufferJsonEncoder|mixed $value)

JsonStream constructor.

string
__toString()

Returns the entire JSON stream as a string.

close()

Frees the JSON encoder from memory and prevents further reading from the JSON stream.

null
detach()

Detaches the underlying PHP resource from the stream and returns it.

null
getSize()

Returns the total size of the JSON stream.

int
tell()

Returns the current position of the cursor in the JSON stream.

bool
eof()

Tells if there are no more bytes to read from the JSON stream.

bool
isSeekable()

Tells if the JSON stream is seekable or not.

seek(int $offset, int $whence = SEEK_SET)

Seeks the given cursor position in the JSON stream.

rewind()

Seeks the beginning of the JSON stream.

bool
isWritable()

Tells if the JSON stream is writable or not.

int
write(string $string)

Writes the given bytes to the JSON stream.

bool
isReadable()

Tells if the JSON stream is readable or not.

string
read(int $length)

Returns the given number of bytes from the JSON stream.

string
getContents()

Returns the remaining bytes from the JSON stream.

array|mixed|null
getMetadata(string|null $key = null)

Returns the metadata related to the JSON stream.

Details

__construct(BufferJsonEncoder|mixed $value)

JsonStream constructor.

Parameters

BufferJsonEncoder|mixed $value A JSON encoder to use or a value to encode

string __toString()

Returns the entire JSON stream as a string.

Note that this operation performs rewind operation on the JSON encoder. Whether this works or not is dependant on the underlying value being encoded. An empty string is returned if the value cannot be encoded.

Return Value

string The entire JSON stream as a string

close()

Frees the JSON encoder from memory and prevents further reading from the JSON stream.

null detach()

Detaches the underlying PHP resource from the stream and returns it.

Return Value

null Always returns null as no underlying PHP resource exists

null getSize()

Returns the total size of the JSON stream.

Return Value

null Always returns null as the total size cannot be determined

int tell()

Returns the current position of the cursor in the JSON stream.

Return Value

int Current position of the cursor

bool eof()

Tells if there are no more bytes to read from the JSON stream.

Return Value

bool True if there are no more bytes to read, false if there are

bool isSeekable()

Tells if the JSON stream is seekable or not.

Return Value

bool Always returns true as JSON streams as always seekable

seek(int $offset, int $whence = SEEK_SET)

Seeks the given cursor position in the JSON stream.

If the provided seek position is less than the current cursor position, a rewind operation is performed on the underlying JSON encoder. Whether this works or not depends on whether the encoded value supports rewinding.

Note that since it's not possible to determine the end of the JSON stream without encoding the entire value, it's not possible to set the cursor using SEEK_END constant and doing so will result in an exception.

Parameters

int $offset The offset for the cursor
int $whence Either SEEK_CUR or SEEK_SET to determine new cursor position

Exceptions

RuntimeException If SEEK_END is used to determine the cursor position

rewind()

Seeks the beginning of the JSON stream.

If the encoding has already been started, rewinding the encoder may not work, if the underlying value being encoded does not support rewinding.

bool isWritable()

Tells if the JSON stream is writable or not.

Return Value

bool Always returns false as JSON streams are never writable

int write(string $string)

Writes the given bytes to the JSON stream.

As the JSON stream does not represent a writable stream, this method will always throw a runtime exception.

Parameters

string $string The bytes to write

Return Value

int The number of bytes written

Exceptions

RuntimeException Always throws a runtime exception

bool isReadable()

Tells if the JSON stream is readable or not.

Return Value

bool Always returns true as JSON streams are always readable

string read(int $length)

Returns the given number of bytes from the JSON stream.

The underlying value is encoded into JSON until enough bytes have been generated to fulfill the requested number of bytes. The extraneous bytes are then buffered for the next read from the JSON stream. The stream may return fewer number of bytes if the entire value has been encoded and there are no more bytes to return.

Parameters

int $length The number of bytes to return

Return Value

string The bytes read from the JSON stream

string getContents()

Returns the remaining bytes from the JSON stream.

Return Value

string The remaining bytes from JSON stream

array|mixed|null getMetadata(string|null $key = null)

Returns the metadata related to the JSON stream.

No underlying PHP resource exists for the stream, but this method will still return relevant information regarding the stream that is similar to PHP stream meta data.

Parameters

string|null $key The key of the value to return

Return Value

array|mixed|null The meta data array, a specific value or null if not defined