PHPackages                             krzar/array-dto - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. krzar/array-dto

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

krzar/array-dto
===============

v1.2.0(1y ago)115MITPHPPHP ^8.1

Since Sep 1Pushed 1y ago1 watchersCompare

[ Source](https://github.com/krzar/array-dto)[ Packagist](https://packagist.org/packages/krzar/array-dto)[ RSS](/packages/krzar-array-dto/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (1)Versions (7)Used By (0)

PHP Array DTO
=============

[](#php-array-dto)

[![license mit](https://camo.githubusercontent.com/7e829ca9c3fdf356dab47b86ba6b56624728b3aaada7897841d301dd2fbbe283/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6963656e73652f6b727a61722f61727261792d64746f)](https://camo.githubusercontent.com/7e829ca9c3fdf356dab47b86ba6b56624728b3aaada7897841d301dd2fbbe283/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6963656e73652f6b727a61722f61727261792d64746f)[![release](https://camo.githubusercontent.com/686d17489cdef10f4f67a39d5f9d4ce784c7aac7cd6b73440e5d850089d9a53a/68747470733a2f2f62616467656e2e6e65742f6769746875622f72656c656173652f6b727a61722f61727261792d64746f)](https://camo.githubusercontent.com/686d17489cdef10f4f67a39d5f9d4ce784c7aac7cd6b73440e5d850089d9a53a/68747470733a2f2f62616467656e2e6e65742f6769746875622f72656c656173652f6b727a61722f61727261792d64746f)[![last commit](https://camo.githubusercontent.com/f4bdbd44c7755ef37331cf8ec8b07df20b9f94f379cb26cbcabf9606a0a4aa25/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6173742d636f6d6d69742f6b727a61722f61727261792d64746f)](https://camo.githubusercontent.com/f4bdbd44c7755ef37331cf8ec8b07df20b9f94f379cb26cbcabf9606a0a4aa25/68747470733a2f2f62616467656e2e6e65742f6769746875622f6c6173742d636f6d6d69742f6b727a61722f61727261792d64746f)

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

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

Supports
--------

[](#supports)

Package VersionPHP VersionSupported1.x8.1+✅Installation
------------

[](#installation)

```
composer require krzar/array-dto
```

Usage
-----

[](#usage)

### Simple object

[](#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

[](#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

[](#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

[](#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

[](#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-mapping)

Types are mapped automatically.

### Custom casts

[](#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
                ),
            ];
         }
    }
```

###  Health Score

28

—

LowBetter than 54% of packages

Maintenance35

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

6

Last Release

607d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/a31d5d8f368bca9ca8afd763d4787b727cfbcc795149aaff9cb5e495d3ec3835?d=identicon)[krzar](/maintainers/krzar)

---

Top Contributors

[![krzar](https://avatars.githubusercontent.com/u/39550721?v=4)](https://github.com/krzar "krzar (32 commits)")

---

Tags

dto

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/krzar-array-dto/health.svg)

```
[![Health](https://phpackages.com/badges/krzar-array-dto/health.svg)](https://phpackages.com/packages/krzar-array-dto)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
