PHP code example of jdcode / dto-php

1. Go to this page and download the library: Download jdcode/dto-php 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/ */

    

jdcode / dto-php example snippets


use JDCode\DtoPhp\DtoPhp;

class StoreUserDto extends DtoPhp
{
    public string $username;
    public string $password;
    public int $age;
}

$dto = new StoreUserDto([
    'username' => 'admin',
    'password => '123456'
    'age' => 99
]);

use JDCode\DtoPhp\DtoPhp;
use JDCode\DtoPhp\Validations\MinLength;
use JDCode\DtoPhp\Validations\MaxLength;
use JDCode\DtoPhp\Validations\Email;

class StoreUserDto extends DtoPhp
{
    #[MinLength(length: 3), MaxLength(length: 10)]
    public string $username;

    public string $password;

    #[Email]
    public string $email

    public int $age;
}

use JDCode\DtoPhp\DtoPhp;

class StoreUserDto extends DtoPhp
{
    public string $username;
    public string $password;
    public int $age;
    public RoleDto $role
}

use JDCode\DtoPhp\DtoPhp;

class RoleDto extends DtoPhp
{
    public int $id;
    public string $name;
}

$dto = new StoreUserDto([
    'username' => 'admin',
    'password => '123456'
    'age' => 99,
    'roles' => [
        'id' => 1,
        'name' => 'Administrator'
    ]
]);

use JDCode\DtoPhp\DtoPhp;

class RoleDto extends DtoPhp
{
    public int $id;
    public string $name;
}

use JDCode\DtoPhp\DtoPhp;
use JDCode\DtoPhp\Casters\CastArrayWithObject;

class StoreUserDto extends DtoPhp
{
    public string $username;
    public string $password;

    #[CastArrayWithObject(Role::class)]
    public array $roles;
}

$dto = new StoreUserDto([
    'username' => 'admin',
    'password => '123456'
    'age' => 99,
    'roles' => [
       [
            'id' => 1,
            'name' => 'Administrator'
       ],
       [
            'id' => 2,
            'name' => 'Moderator'
       ]
    ]
]);

var_dump($dto->roles) // returns RoleDto[]

composer