JsonStream

Class
extends Stream
Final: Yes

Class JsonStream.

Description

Provides a JSON-specific stream implementation, extending Nyholm's PSR-7 Stream. This class SHALL encapsulate a JSON-encoded payload within an in-memory PHP stream, while retaining the original decoded payload for convenient retrieval.

Implementations of this class MUST properly handle JSON encoding errors and SHALL explicitly prohibit the inclusion of resource types within the JSON payload.

Table of Contents

Interfaces

PayloadStreamInterface

Interface PayloadStreamInterface.

Constants

ENCODING_OPTIONS

JSON encoding flags to be applied by default.

 = \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE

Properties

Methods

__construct()

Constructs a new JsonStream instance with the provided payload.

 : mixed
getPayload()

Retrieves the decoded payload associated with the stream.

 : mixed
withPayload()

Returns a new instance of the stream with the specified payload.

 : self
jsonEncode()

Encodes the given data as JSON, enforcing proper error handling.

 : string
Constants

Constants

ENCODING_OPTIONS

Public

JSON encoding flags to be applied by default.

public int ENCODING_OPTIONS = \JSON_THROW_ON_ERROR | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE

Description

The options JSON_THROW_ON_ERROR, JSON_UNESCAPED_SLASHES, and JSON_UNESCAPED_UNICODE SHALL be applied to enforce strict error handling and produce readable JSON output.

Properties

$encodingOptions

Private Read-only
private int $encodingOptions = self::ENCODING_OPTIONS

$payload

Private Read-only
private mixed $payload = []
Methods

__construct()

Public

Constructs a new JsonStream instance with the provided payload.

public __construct([ mixed  $payload = []][, int  $encodingOptions = self::ENCODING_OPTIONS]) : mixed

Description

The payload SHALL be JSON-encoded and written to an in-memory stream. The original payload is retained in its decoded form for later access via getPayload().

Parameters
$payload : mixed = []

Description

The data to encode as JSON. MUST be JSON-encodable. Resources are explicitly prohibited.

$encodingOptions : int = self::ENCODING_OPTIONS

Description

Optional JSON encoding flags. If omitted, ENCODING_OPTIONS will be applied.

getPayload()

Public

Retrieves the decoded payload associated with the stream.

public getPayload() : mixed

Description

This method SHALL return the original JSON-encodable payload provided during construction or via withPayload().

Return values

Description

the decoded payload

withPayload()

Public

Returns a new instance of the stream with the specified payload.

public withPayload( mixed  $payload) : self

Description

This method MUST return a new JsonStream instance with the body replaced by a stream containing the JSON-encoded form of the new payload. The current instance SHALL remain unchanged.

Parameters
$payload : mixed

Description

the new JSON-encodable payload

Return values
self

Description

a new JsonStream instance containing the updated payload

jsonEncode()

Private

Encodes the given data as JSON, enforcing proper error handling.

private jsonEncode( mixed  $data, int  $encodingOptions) : string

Description

If the provided data is a resource, this method SHALL throw an \InvalidArgumentException, as resource types are not supported by JSON.

Parameters
$data : mixed

Description

the data to encode as JSON

$encodingOptions : int

Description

JSON encoding options to apply. JSON_THROW_ON_ERROR will always be enforced.

Tags
throws
InvalidArgumentException

Description

if the data contains a resource

throws
JsonException

Description

if JSON encoding fails

Return values
string

Description

the JSON-encoded string representation of the data