krzar/array-dto

v1.2.0 2024-09-18 08:04 UTC

This package is auto-updated.

Last update: 2025-06-18 09:53:13 UTC


README

license mit release last commit

This package allows you to generate object based on array data.

This can be useful for integrations with some APIs, for example.

Supports

Package Version PHP Version Supported
1.x 8.1+

Installation

composer require krzar/array-dto

Usage

Simple object

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 = [];
}

To create this object from array call:

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

UserData::create($data);

If any parameter is not passed any value will be assigned, so a default value should be established for such cases.

Nested object

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;
}

To create this object from array call:

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

UserData::create($data);

Nested object multidimensional

If you want to create array of objects you need to configure this with $arrayMap property.

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),
        ];
     }
}

To create this object from array call:

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

UserData::create($data);

Union types

You can use union types in objects, but with some restrictions:

  • built-in types can be combined in any way
  • ArrayObject type can be combined only with build-in types, not with other ArrayObject

For example:

public CompanyData|string $company;

CompanyData will be created if a company index in array is array type.

Names mapping

You can map names of parameters using $namesMap array:

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')
            ];
         }
    }

Types mapping

Types are mapped automatically.

Custom casts

You can make any custom cast and mapping to parameter. You can also provide array of casts.

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
                ),
            ];
         }
    }