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.
Interfaces
Constants
JSON encoding flags to be applied by default.
Properties
Methods
Constructs a new JsonStream instance with the provided payload.
Retrieves the decoded payload associated with the stream.
Returns a new instance of the stream with the specified payload.
Encodes the given data as JSON, enforcing proper error handling.
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.
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.
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
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
Description
a new JsonStream instance containing the updated payload
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.
Description
if the data contains a resource
Description
if JSON encoding fails
Return values
Description
the JSON-encoded string representation of the data