This package offers a set of traits to simplify working with DTOs (Data Transfer Objects) in Laravel. It allows you to:
- Convert a DTO to an array using the
AsArraytrait. - Transform a DTO into a JSON string via the
AsJsontrait. - Seamlessly serialize a DTO with
json_encode()using theAsJsonSerializetrait. - Clone a DTO with modified properties through the
AsCloneabletrait. - Make a DTO with static method
make(), usingAsMaketrait - Use DTOs as custom casts in Eloquent models.
- Use Array helpers
only()orexcept()from DTO with usingAsArraytrait.
Install the library using Composer:
composer require lemax10/dto-helpersConverts your DTO to an array.
Requires: Implements Illuminate\Contracts\Support\Arrayable.
use LeMaX10\DtoHelpers\Traits\AsArray;
use Illuminate\Contracts\Support\Arrayable;
class MyData implements Arrayable
{
use AsArray;
public function __construct(
public string $key,
public string $value
) {}
}
$dto = new MyData(key: 'test1', value: 'value1');
dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']
dump($dto->only('key'));
// Output: ['key' => 'test1']
dump($dto->except('key'));
// Output: ['value' => 'value1']Converts your DTO to a JSON string.
Requires: Implements Illuminate\Contracts\Support\Jsonable.
use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;
class MyData implements Jsonable
{
use AsJson;
// ...
}
$dto = new MyData(key: 'test1', value: 'value1');
dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'Enables JSON serialization with json_encode().
Requires: Implements JsonSerializable.
use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;
class MyData implements JsonSerializable
{
use AsJsonSerialize;
// ...
}
$dto = MyData::make(key: 'test1', value: 'value1');
dump(json_encode($dto));
// Output: {"key":"test1","value":"value1"}Enables JSON serialization with json_encode().
Requires: Implements LeMaX10\DtoHelpers\Contracts\Makeable.
use LeMaX10\DtoHelpers\Contracts\Makeable;
use JsonSerializable;
class MyData implements Makeable
{
use AsMake;
// ...
}
class MyDataCustomMake implements Makeable
{
// ...
public static function make(...$arguments): static
{
// Example so-so
if (Arr::get($arguments, 'key') === 1) {
$arguments['value'] = 'valueExample';
}
return new static(...$arguments);
}
}
$dto = MyData::make(key: 'test1', value: 'value1');
dump($dto);
// Output: instance of MyData
$dtoCustom = MyDataCustomMake::make(key: 1, value: 'value1');
dump($dto);
// Output: instance of MyDataCustomMake and value equals "valueExample"Clones a DTO while allowing property overrides.
Requires: Implements LeMaX10\DtoHelpers\Contracts\Cloneable
use LeMaX10\DtoHelpers\Traits\AsArray;
use LeMaX10\DtoHelpers\Traits\AsCloneable;
use LeMaX10\DtoHelpers\Contracts\Cloneable;
use Illuminate\Contracts\Support\Arrayable;
class MyData implements Cloneable, Arrayable
{
use AsCloneable, AsArray;
// ...
}
$dto = new MyData(key: 'test1', value: 'value1');
dump($dto->toArray());
// Output: ['key' => 'test1', 'value' => 'value1']
$clone = $dto->clone(['key' => 'test2']);
dump($clone->toArray());
// Output: ['key' => 'test2', 'value' => 'value1']Converts your DTO to a JSON string.
Requires: Implements Illuminate\Contracts\Support\Jsonable.
use LeMaX10\DtoHelpers\Traits\AsJson;
use Illuminate\Contracts\Support\Jsonable;
class MyData implements Jsonable
{
use AsJson;
// ...
}
$dto = new MyData(key: 'test1', value: 'value1');
dump($dto->toJson());
// Output: '{"key":"test1","value":"value1"}'Use your DTO as a custom cast in an Eloquent model.
use LeMaX10\DtoHelpers\Casts\AsDto;
use Illuminate\Database\Eloquent\Model;
class ExampleModel extends Model
{
protected $casts = [
'dto' => AsDto::class . ':' . MyData::class,
];
// OR
public function casts()
{
return [
'dto' => AsDto::cast(MyData::class),
];
}
}
$model = ExampleModel::find(1);
dump($model->dto);
// Output: instance of MyDataThank you for considering contributing to the DTO Helpers package!
The DTO Helpers package is open-sourced software licensed under the MIT license.