heseya/dto

Heseya Dto with optional properties
4,648
Install
composer require heseya/dto
Latest Version:1.0.2
PHP:^8.0
Laravel:8 – 11
Last Updated:Mar 18, 2024
Links: GitHub  ·  Packagist
Maintainer: bvlinsky

Heseya Dto

Simple Dto system with optional parameters.

Install

composer require heseya/dto

Get started

To create a new Dto you need to extend base Heseya\Dto\Dto class as such:

use Heseya\Dto\Dto;

class ExampleDto extends Dto {
    private string $property1;
    private string $property2;
}

Heseya\Dto\Dto base class provides constructor automatically handling all property initialization.

To create a new Dto use PHP 8 named arguments:

$dto = new ExampleDto(property1: 'value1', property2: 'value2');

Or use an associative array:

$array = ['property1' => 'value1', 'property2' => 'value2'];

$dto = new ExampleDto($array);

The built-in toArray() method automatically returns Dto properties in the associative array format:

$dto->toArray(); // ['property1' => 'value1', 'property2' => 'value2']

Optional properties

To make your Dto properties optional you need to use Heseya\Dto\Missing type as such:

use Heseya\Dto\Dto;
use Heseya\Dto\Missing;

class OptionalDto extends Dto {
    private string $required;
    private string|Missing $optional;
}

Optional properties when not initialized will automatically be hidden in toArray() method result:

$dto = new OptionalDto(required: 'required value')

$dto->toArray(); // ['required' => 'required value']

Hidden properties

Sometimes you want to hide your internal properties from appearing in the toArray() method result

You can hide those properties using Heseya\Dto\Hidden attribute:

use Heseya\Dto\Dto;
use Heseya\Dto\Hidden;

class HiddenDto extends Dto {
    private string $visible;
    #[Hidden]
    private string $hidden;
}

$dto = new HiddenDto(visible: 'visible value', hidden: 'hidden value');

$dto->toArray(); // [visible => 'visible value']

Exceptions

Non-optional properties need to be set explicitly in the constructor, otherwise a Heseya\Dto\DtoException will be raised:

use Heseya\Dto\Dto;

class NonOptionalDto extends Dto {
    private ?string $param;
}

$dto = new NonOptionalDto(); // DtoException("Property NonOptionalDto::$param is required")

Nullable properties such as private ?string $param will not be automatically initialized as null and need to be set explicitly as well.

Optional properties using Heseya\Dto\Missing type will be automatically initialized as new Missing() class instance when not set explicitly.

Related Packages

doctrine/dbal

Powerful PHP database abstraction layer (DBAL) with many features for database s...

648,266,899 9,701
laravel/framework

The Laravel Framework.

598,760,099 34,966
laravel/tinker

Powerful REPL for the Laravel framework.

507,193,856 7,441
laravel/serializable-closure

Laravel Serializable Closure provides an easy and secure way to serialize closur...

423,477,751 607
nunomaduro/collision

Cli error handling for console/command-line PHP applications.

403,217,078 4,664

Version History

Version Released PHP Laravel License
1.0.2 ^8.0 ^8.0|^9.0|^10.0|^11.0
1.0.1 ^8.0 ^8.0|^9.0|^10.0
1.0.0 ^8.0 ^8.0|^9.0