PHP code example of perfilov / php-dto-packer

1. Go to this page and download the library: Download perfilov/php-dto-packer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

perfilov / php-dto-packer example snippets


use DtoPacker\AbstractDto;

class PurchaseDto extends AbstractDto
{
    public int $id;
    protected \DateTimeInterface $date;
    protected ProductDto $product;
}

class ProductDto extends AbstractDto
{
    public string $name;
    public float $price;
}

$data = [
    'id'      => 100,
    'date'    => '2025-10-16',
    'product' => [
        'name'  => 'apple',
        'price' => 10.5,
    ]
];

$purchase = new PurchaseDto($data);

echo "$purchase";
dd($purchase)

PurchaseDto {
    +id: 100
    #date: DateTimeImmutable @1760572800 {
        date: 2025-10-16 00:00:00.0 +00:00
    }
    #product: ProductDto {
        +name: "apple"
        +price: 10.5
    }
}

use DtoPacker\AbstractDto;
use DtoPacker\Alias;

class UserDto extends AbstractDto
{
    #[Alias('lastname', 'family_name')]
    protected string $surname;
}

$data = [
    'lastname' => 'Mask',
];

$user = new UserDto($data);

echo $user->surname; // output: Mask

$user->family_name = 'Trump'; // output: Trump

use DtoPacker\AbstractDto;
use DtoPacker\Dimension;

class ExampleDto extends AbstractDto
{
    protected array|string $strings;
    protected array|int $ints;
    protected array|float $floats;
    protected array|bool $bools;
    protected array|\DateTimeInterface $dates;
    protected array|Object $objects;
    protected array|UserDto $users;
    
    #[Dimension(2)]
    protected array|int $multiInts;
}

class UserDto extends AbstractDto
{
    public string $name;
}

$datetime = new \DateTime('2030-01-01');

$object = new \stdClass();
$object->x = 2;

$user = new UserDto(['name' => 'Trump']);

$data = [
    'strings'   => ['Mask', 'Trump'],
    'ints'      => [100, 200, 300],
    'floats'    => [10.5, 99.9],
    'bools'     => [true, false],
    'dates'     => ['2025-10-16', $datetime],
    'objects'   => [['x' => 1], $object],
    'users'     => [['name' => 'Elon'], $user],
    'multiInts' => [[100, 200], [300, 400, 500]],
];

$dto = new ExampleDto($data);
echo $dto;

use DtoPacker\AbstractDto;
use DtoPacker\PreMutator;

class UserDto extends AbstractDto
{
    #[PreMutator('ucfirst', [CustomMutator::class, 'addPrefixMr'])]
    protected string $lastname;
}

class CustomMutator
{
    public static function addPrefixMr(string $value): string
    {
        return "Mr $value";
    }
}

$user = new UserDto([
    'lastname' => 'elon'
]);

echo $user->lastname; // output: "Mr Elon"

use DtoPacker\AbstractDto;
use DtoPacker\Validators\Array;
use DtoPacker\Validators\Bool;
use DtoPacker\Validators\Datetime;
use DtoPacker\Validators\FieldValidators;
use DtoPacker\Validators\Numeric;
use DtoPacker\Validators\String;
use DtoPacker\Validators\Mixed;

class ExampleDto extends AbstractDto
{
    #[FieldValidators(
        new Numeric\Min(10),
        new Numeric\Max(20),
        new Numeric\Between(1, 100),
    )]
    protected int $int;

    #[FieldValidators(
        new String\Alpha(),
        new String\Card(),
        new String\Cuid(),
        new String\Email(),
        new String\Email(),
        new String\Ip(),
        new String\IpV4(),
        new String\IpV6(),
        new String\Json(),
        new String\LengthMin(10),
        new String\LengthMax(20),
        new String\LengthBetween(1, 100),
        new String\MacAddress(),
        new String\NanoId(),
        new String\Regex('/^\d$/'),
        new String\Ulid(),
        new String\Url(),
        new String\Uuid\Uuid(),
    )]
    protected string $string;

    #[FieldValidators(
        new Bool\IsFalse(),
        new Bool\IsTrue(),
    )]
    protected bool $bool;

    #[FieldValidators(
        new Datetime\After('2000-01-01'),
        new Datetime\Before('2025-12-31'),
        new Datetime\Between('2000-01-01', '2026-01-01'),
    )]
    protected \DateTimeInterface $datetime;

    #[FieldValidators(
        new Mixed\In([100, 'paid', true]),
        new Mixed\Required(),
        new Mixed\Requires(['string', 'bool']),
    )]
    protected string $mixed;

    #[FieldValidators(
        new Array\CountBetween(1, 100),
        new Array\CountMin(1),
        new Array\CountMax(10),
        new Array\Unique(),
        new Array\UniqueIntegers(),
    )]
    protected array|int $ints;

    #[FieldValidators(
        new Array\UniqueStrings(),
    )]
    protected array|string $strings;
}

$data = [
    'int'      => 5,
    'string'   => 'Mask', 'Trump',
    'bool'     => true,
    'datetime' => '2020-06-15',
    'mixed'    => 'paid',
    'ints'     => [2, 5],
    'strings'  => ['Elon', 'Trump'],
];

try {
    $dto = new ExampleDto($data);
} catch (ValidationExceptions $e) {
    print_r($e->toArray());
}

Array
(
    [0] => Array
        (
            [field] => int
            [error] => Int must be at least 10
            [path]  => int
        )

    [1] => Array
        (
            [field] => string
            [error] => String must be a valid card number
            [path]  => string
        )
    ...
)

use DtoPacker\AbstractDto;
use DtoPacker\Validators\ArrayValidators;
use DtoPacker\Validators\Numeric\Max;
use DtoPacker\Validators\Numeric\Min;

class ExampleDto extends AbstractDto
{
    #[ArrayValidators(
        new Min(10),
        new Max(20),
    )]
    protected array|int $ints;
}

$data = [
    'ints' => [5, 15, 25],
];

try {
    $dto = new ExampleDto($data);
} catch (ValidationExceptions $e) {
    print_r($e->toArray());
}

Array
(
    [0] => Array
        (
            [field] => ints
            [error] => [0] ints must be at least 10
            [path]  => ints[0]
            [index] => Array
                (
                    [0] => 0
                )

        )
    [1] => Array
        (
            [field] => ints
            [error] => [2] ints may not be greater than 20
            [path]  => ints[2]
            [index] => Array
                (
                    [0] => 2
                )

        )
)

use DtoPacker\AbstractDto;
use DtoPacker\Validators\FieldValidators;
use DtoPacker\Validators\Numeric\Max;

class ExampleDto extends AbstractDto
{
    #[FieldValidators(
        [
            new Max(10), 
            new Max(20), // will be skipped if one of the previous validators returns an error
        ],
        new Max(30)
    )]
    protected int $int;
}

$data = [
    'ints' => [5, 15, 25],
];

try {
    $dto = new ExampleDto($data);
} catch (ValidationExceptions $e) {
    print_r($e->toArray());
}

Array
(
    [0] => Array
        (
            [field] => int
            [error] => Int may not be greater than 10
            [path]  => int
        )

    [1] => Array
        (
            [field] => int
            [error] => Int may not be greater than 30
            [path]  => ints
        )

)

use DtoPacker\AbstractDto;
use DtoPacker\Dimension;

class ExampleDto extends AbstractDto
{
    #[Dimension(2)]
    protected array|int $ints;
}

$data = [
    'ints' => [[5, 15, 25], [100, 200]],
];

$dto = new ExampleDto($data);
print_r($dto->toArray());

Array
(
    [ints] => Array
        (
            [0] => Array
                (
                    [0] => 5
                    [1] => 15
                    [2] => 25
                )

            [1] => Array
                (
                    [0] => 100
                    [1] => 200
                )

        )

)
bash
composer 
json
{"id":100,"date":"2025-10-16T00:00:00.000+00:00","product":{"name":"apple","price":10.5}}