CborTrait
Provides utility methods for managing CBOR serialization options and creating standardized CBOR HTTP responses within controllers.
This trait is designed to:
- Initialize and manage the CBOR serialization options (e.g. ArrayOption::REDUCE).
- Build PSR-7 CBOR responses with the proper
Content-TypeandContent-Lengthheaders.
Tags
Table of Contents
Properties
- $cborSerializeOptions : array<string|int, mixed>
- Temporary serialization options passed to the {@see CborSerializer}.
Methods
- cborResponse() : ResponseInterface
- Builds a PSR-7 CBOR response.
- initializeCborOptions() : static
- Initializes the internal `$cborSerializeOptions` property.
Properties
$cborSerializeOptions
Temporary serialization options passed to the {@see CborSerializer}.
public
array<string|int, mixed>
$cborSerializeOptions
= [\oihana\core\options\ArrayOption::REDUCE => true]
(ex: ArrayOption::REDUCE, custom schema flags, etc.)
Methods
cborResponse()
Builds a PSR-7 CBOR response.
public
cborResponse(ResponseInterface $response[, mixed $data = null ][, int $status = HttpStatusCode::OK ]) : ResponseInterface
The payload is encoded with CborSerializer::encode() using the configured self::$cborSerializeOptions, any pending output buffer is cleared, and the resulting binary is written to a fresh stream advertised with the CBOR MIME type.
Parameters
- $response : ResponseInterface
-
The PSR-7 Response object to write into.
- $data : mixed = null
-
The data to encode as CBOR (defaults to
null). - $status : int = HttpStatusCode::OK
-
The HTTP status code to set on the response (defaults to HttpStatusCode::OK).
Tags
Return values
ResponseInterface —The response carrying the CBOR-encoded body and headers.
initializeCborOptions()
Initializes the internal `$cborSerializeOptions` property.
public
initializeCborOptions([array<string|int, mixed> $init = [] ][, ContainerInterface|null $container = null ]) : static
The options are taken from $init[ControllerParam::CBOR_SERIALIZE_OPTIONS] when present;
otherwise, if empty, they are looked up in the DI container under the same key.
Parameters
- $init : array<string|int, mixed> = []
-
Optional initialization array (e.g.
['cborSerializeOptions' => [ ... ]]). - $container : ContainerInterface|null = null
-
Optional PSR-11 container used to resolve the serialization options.
Tags
Return values
static —Returns the current instance for method chaining.