Oihana PHP

JsonTrait

Provides utility methods for managing JSON encoding options and creating standardized JSON HTTP responses within controllers.

This trait is designed to:

  • Initialize and manage JSON encoding flags.
  • Build PSR-7 JSON responses with proper headers.
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

Table of Contents

Properties

$jsonOptions  : int
The default JSON encoding flags used in the controller (bitmask of `JSON_*` constants).
$jsonSerializeOptions  : array<string|int, mixed>
Temporary serialization options passed to the {@see JsonSerializer}.

Methods

initializeJsonOptions()  : static
Initializes the internal `$jsonOptions` and `$jsonSerializeOptions` properties.
jsonResponse()  : ResponseInterface
Builds a PSR-7 JSON response.

Properties

$jsonOptions

The default JSON encoding flags used in the controller (bitmask of `JSON_*` constants).

public int $jsonOptions = \oihana\enums\JsonParam::JSON_NONE

$jsonSerializeOptions

Temporary serialization options passed to the {@see JsonSerializer}.

public array<string|int, mixed> $jsonSerializeOptions = [\oihana\core\options\ArrayOption::REDUCE => true]

(ex: ArrayOption::REDUCE, custom schema flags, etc.)

Methods

initializeJsonOptions()

Initializes the internal `$jsonOptions` and `$jsonSerializeOptions` properties.

public initializeJsonOptions([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static

The JSON encode flags and the serializer options are taken from $init when present; otherwise they are looked up in the DI container under the matching ControllerParam keys. Invalid encode flags fall back to JsonParam::JSON_NONE.

Parameters
$init : array<string|int, mixed> = []

Optional initialization array (e.g. ['jsonOptions' => ..., 'jsonSerializeOptions' => [ ... ]]).

$container : ContainerInterface|null = null

Optional PSR-11 container used to resolve the JSON options.

Tags
throws
ContainerExceptionInterface

If the container encounters an error while retrieving an entry.

NotFoundExceptionInterface

If no entry was found in the container for the given identifier.

Return values
static

Returns the current instance for method chaining.

jsonResponse()

Builds a PSR-7 JSON response.

public jsonResponse(ResponseInterface $response[, mixed $data = null ][, int $status = HttpStatusCode::OK ]) : ResponseInterface

The payload is encoded with JsonSerializer::encode() using the configured self::$jsonOptions encode flags and self::$jsonSerializeOptions, then written to the response body with the JSON Content-Type header.

Parameters
$response : ResponseInterface

The PSR-7 Response object to write into.

$data : mixed = null

The data to encode as JSON (defaults to null).

$status : int = HttpStatusCode::OK

The HTTP status code to set on the response (defaults to HttpStatusCode::OK).

Tags
example
class UserController extends Controller
{
    use JsonTrait ;

    public function show( Request $request , Response $response ) : Response
    {
        return $this->jsonResponse( $response , [ 'id' => 42 , 'name' => 'Alice' ] ) ;
    }
}
Return values
ResponseInterface

The response carrying the JSON-encoded body and header.

On this page

Search results