Data transfer objects

v1.1.1 2022-12-07 13:35 UTC

This package is auto-updated.

Last update: 2024-04-07 16:20:03 UTC


README

Run tests Latest Stable Version

Installation

This package requires php:^8.1.
You can install it via composer:

composer require bluestone/dto

Usage

This package goal to simplify the build of objects whose serve to pass structured data.

Build simple DTO

An exemple of class extends DTO :

use Bluestone\DataTransferObject\DataTransferObject;

class Hooman extends DataTransferObject
{
    public string $name;
}

You can instantiate this class like this :

$jane = new Hooman(name: 'Jane');
$john = new Hooman(['name' => 'John']);

Build complex DTO with Casting

An exemple of class with property with casting :

use Bluestone\DataTransferObject\DataTransferObject;
use Bluestone\DataTransferObject\Attributes\CastWith;
use Bluestone\DataTransferObject\Casters\ArrayCaster;

class Hooman extends DataTransferObject
{
    public string $name;
    
    #[CastWith(ArrayCaster::class, type: Hooman::class)]
    public array $children;
}

You can instantiate this class like this :

$jane = new Hooman(
    name: 'Jane', 
    children: [
        new Hooman(name: 'Mario'), 
        new Hooman(name: 'Luigi'),
    ],
);

$john = new Hooman([
    'name' => 'John',
    'children' => [
        ['name' => 'Mario'],
        ['name' => 'Luigi'],
    ],
]);

Build complex DTO with Mapping

An exemple of class with property with mapping :

use Bluestone\DataTransferObject\DataTransferObject;
use Bluestone\DataTransferObject\Attributes\Map;

class Hooman extends DataTransferObject
{
    #[Map('date_of_birth')]
    public string $bornAt;
}

You can instantiate this class like this :

$jane = new Hooman(
    date_of_birth: '1970-01-01', 
);

$john = new Hooman([
    'date_of_birth' => '1970-01-01',
]);

Contributing

DTO is an open source project under MIT License and is open for contributions.