Oihana PHP

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-Type and Content-Length headers.
Tags
author

Marc Alcaraz (ekameleon)

since
1.0.0

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
example
class FeedController extends Controller
{
    use CborTrait ;

    public function index( Request $request , Response $response ) : Response
    {
        return $this->cborResponse( $response , [ 'hello' => 'world' ] ) ;
    }
}
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
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.

On this page

Search results