This package requires php:^8.1
.
You can install it via composer:
composer require bluestone/dto
This package goal to simplify the build of objects whose serve to pass structured data.
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']);
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'],
],
]);
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',
]);
DTO is an open source project under MIT License and is open for contributions.