class StreamJsonEncoder extends AbstractJsonEncoder (View source)

Encodes value into JSON and directly echoes it or passes it to a stream.

Methods

__construct(mixed $value, callable $stream = null)

StreamJsonEncoder constructor.

$this
setOptions(int $options)

Sets the JSON encoding options.

$this
setIndent(string|int $indent)

Sets the indent for the JSON output.

string[]
getErrors()

Returns the list of errors that occurred during the last encoding process.

array
getValueStack()

Returns the current encoding value stack.

int|null
key()

Returns the current number of step in the encoder.

bool
valid()

Tells if the encoder has a valid current state.

mixed
current()

Returns the bytes written in the last step or null if the encoder is not in valid state.

rewind()

Returns the JSON encoding to the beginning.

next()

Iterates the next token or tokens to the output stream.

mixed
resolveValue(mixed $value)

Resolves the actual value of any given value that is about to be processed.

void
write(string $string, int $token)

Echoes to given string or passes it to the stream callback.

int
encode()

Encodes the entire value into JSON and returns the number bytes.

Details

__construct(mixed $value, callable $stream = null)

StreamJsonEncoder constructor.

If a callable is given as the second argument, the callable will be called with two arguments. The first argument is the JSON string to output and the second argument is the type of the token being outputted.

If no second parameter is passed to the constructor, then the encoder will simply output the json using an echo statement.

Parameters

mixed $value The value to encode as JSON
callable $stream An optional stream to pass the output or null to echo it

$this setOptions(int $options)

Sets the JSON encoding options.

Parameters

int $options The JSON encoding options that are used by json_encode

Return Value

$this Returns self for call chaining

Exceptions

RuntimeException If changing encoding options during encoding operation

$this setIndent(string|int $indent)

Sets the indent for the JSON output.

Parameters

string|int $indent A string to use as indent or the number of spaces

Return Value

$this Returns self for call chaining

Exceptions

RuntimeException If changing indent during encoding operation

string[] getErrors()

Returns the list of errors that occurred during the last encoding process.

Return Value

string[] List of errors that occurred during encoding

protected array getValueStack()

Returns the current encoding value stack.

Return Value

array The current encoding value stack

int|null key()

Returns the current number of step in the encoder.

Return Value

int|null The current step number as integer or null if the current state is not valid

bool valid()

Tells if the encoder has a valid current state.

Return Value

bool True if the iterator has a valid state, false if not

mixed current()

Returns the bytes written in the last step or null if the encoder is not in valid state.

Return Value

mixed The current value or state from the encoder

rewind()

Returns the JSON encoding to the beginning.

next()

Iterates the next token or tokens to the output stream.

protected mixed resolveValue(mixed $value)

Resolves the actual value of any given value that is about to be processed.

Parameters

mixed $value The value to resolve

Return Value

mixed The resolved value

protected void write(string $string, int $token)

Echoes to given string or passes it to the stream callback.

Parameters

string $string The given token to write
int $token The type of the token

Return Value

void

int encode()

Encodes the entire value into JSON and returns the number bytes.

Return Value

int Returned the number of bytes outputted