heseya/dto

Heseya Dto with optional properties
4,618
Install
composer require heseya/dto
Latest Version:1.0.2
PHP:^8.0
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...

637,827,751 9,703
laravel/framework

The Laravel Framework.

589,172,971 34,950
laravel/tinker

Powerful REPL for the Laravel framework.

498,216,183 7,442
laravel/serializable-closure

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

413,412,504 609
nunomaduro/collision

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

391,429,217 4,660