PHP code example of krzar / array-dto

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

    

krzar / array-dto example snippets


use KrZar\ArrayDto\ArrayDto;

class UserData extends ArrayDto {
    public string $name;
    public string $email;
    public int $age;
    public float $money;
    public bool $isActive = false;
    public array $roles = [];
}

$data = [
    'name' => 'Test',
    'email' => '[email protected]',
    'age' => 99,
    'money' => 1520.50,
    'isActive' => true,
    'roles' => ['ADMIN']
];

UserData::create($data);

class CompanyData extends ArrayObject {
    public string $name;
    public string $city;
    public string $street;
}

class UserData extends ArrayObject {
    public string $name;
    public string $email;
    public int $age;
    public float $money;
    public bool $isActive = false;
    public array $roles = [];
    public CompanyData $company;
}

$data = [
    'name' => 'Test',
    'email' => '[email protected]',
    'age' => 99,
    'money' => 1520.50,
    'isActive' => true,
    'roles' => ['ADMIN'],
    'company' => [
        'name' => 'Test Company',
        'city' => 'Test',
        'street' => 'Test Street 1'
    ]   
];

UserData::create($data);

class UserData extends ArrayObject {
    public string $name;
    public string $email;
    public int $age;
    public float $money;
    public bool $isActive = false;
    public array $roles = [];
    public CompanyData $company;
    public array $children;
    
     protected function casts(): array {
        return [
            'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class),
        ];
     }
}

$data = [
    'name' => 'Test',
    'email' => '[email protected]',
    'age' => 99,
    'money' => 1520.50,
    'isActive' => true,
    'roles' => ['ADMIN'],
    'company' => [
        'name' => 'Test Company',
        'city' => 'Test',
        'street' => 'Test Street 1'
    ],
    'children' => [
        [
            'name' => 'Test 2',
            'email' => '[email protected]',
            'age' => 98,
            'money' => 2400,
            'isActive' => true,
            'roles' => ['MODERATOR']
        ]       
    ]       
];

UserData::create($data);

public CompanyData|string $company;

class UserData extends ArrayObject {
    public string $name;
    public string $email;
    public int $age;
    public float $money;
    public bool $isActive = false;
    public array $roles = [];
    public CompanyData $company;
    public array $children;

    protected function casts(): array {
            return [
                'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class),
                'is_active' => new \KrZar\ArrayDto\Casts\NameCast('isActive')
            ];
         }
    }

class UserData extends ArrayObject {
    public string $name;
    public string $email;
    public int $age;
    public float $money;
    public bool $isActive = false;
    public array $roles = [];
    public CompanyData $company;
    public array $children;
    public int $agePlusTen;

    protected function casts(): array {
            return [
                'children' => new \KrZar\ArrayDto\Casts\MultidimensionalCast(UserData::class),
                'is_active' => new \KrZar\ArrayDto\Casts\NameCast('isActive'),
                'agePlusTen' => new \KrZar\ArrayDto\Casts\CustomCast(
                    fn(mixed $value, array $raw) => $raw['age'] + 10
                ),
            ];
         }
    }