gouh/laminas-handlers

JsonResponse implementation of laminas/diactoros with a structured data and Data Transfer Object class.

1.0.1 2021-05-20 20:06 UTC

This package is auto-updated.

Last update: 2024-03-21 05:57:25 UTC


README

JsonResponse implementation of laminas/diactoros with a structured data and Data Transfer Object class.

Built With

Usage

You can use class JsonResponse for produce ResponseInterface

use StructuredHandlers\JsonResponse;

then you can use like this, produces a response 200 by default

return new JsonResponse(
        $responseData,
        'This is a cool message'
);

Or in a more specific case something like this

return new JsonResponse(
        $responseData, // Data to client
        'This is a bad message', // Personalized message
        500, // Http status code
        true, // is error
        $arrayHeaders // Array of headers
);

You can use class DataTransferObject for extends in your DTO

class MyDtoRequest extends DataTransferObject {
    public $className;
    public $version;
    public $property;
}

To construct an object of the class, an array can be used thanks to the use of reflection in DataTransferObject class

$myDtoArray = [
    'className' => 'Example',
    'version' => 1.0,
    'property' => 'hi!' 
];

$myDto = new MyDtoRequest($myDtoArray);

You can build an array based on a single value or also excluding some value from the entire class

$myDto->only('className')->toArray();

Only get class_name, the names are obtained in snake case if the false parameter is not specified to the toArray function

[
    'class_name' => 'Example'
]

You can exclude a property from the class using the except function

$myDto->except('className')->toArray();

produces

[
    'class_name' => 'Example',
    'property' => 'hi!'
]