PHPackages                             romanzipp/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. romanzipp/dto

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

romanzipp/dto
=============

A strongly typed Data Transfer Object without magic for PHP 7.4+

2.3.1(3y ago)1216.1k—0%1[1 issues](https://github.com/romanzipp/DTO/issues)1MITPHPPHP ^8.0

Since Oct 15Pushed 2y ago1 watchersCompare

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

READMEChangelogDependencies (5)Versions (32)Used By (1)

DTO
===

[](#dto)

[![Latest Stable Version](https://camo.githubusercontent.com/4e2a9fb2befdfbef09034ddb13b1700e99d54091195b94836b5107c051d3e17a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f726f6d616e7a6970702f44544f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/dto)[![Total Downloads](https://camo.githubusercontent.com/52eb2dd0f183517fa96d6a09d43624962e23f02462d7e18369149a85f54a52e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f726f6d616e7a6970702f44544f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/dto)[![License](https://camo.githubusercontent.com/d3617dd0b6549053b1a732c98af61d87737b02d9f4bb5f18582a6dd8f7d640d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f726f6d616e7a6970702f44544f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/romanzipp/dto)[![GitHub Build Status](https://camo.githubusercontent.com/2bbf608282178e349904b3fae227ecbb3b723820567554f43a8ae2a543339e84/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f726f6d616e7a6970702f44544f2f54657374733f7374796c653d666c61742d737175617265)](https://github.com/romanzipp/DTO/actions)

A strongly typed **Data Transfer Object** without magic for PHP 8.0+ . Features support for PHP 8 [union types](https://wiki.php.net/rfc/union_types_v2) and [attributes](https://wiki.php.net/rfc/attributes_v2).

Contents
--------

[](#contents)

- [Installation](#installation)
- [Usage](#usage)
- [Validation table](#validation)

Installation
------------

[](#installation)

```
composer require romanzipp/dto

```

- For **PHP 7.4** please use [`1.x`](https://github.com/romanzipp/DTO/tree/1.x)
- For **PHP 8.0** please use [`2.x`](https://github.com/romanzipp/DTO)

Usage
-----

[](#usage)

```
use romanzipp\DTO\AbstractData;
use romanzipp\DTO\Attributes\Required;

class DummyData extends AbstractData
{
    #[Required]
    public string $name;

    public ?string $nickname;

    public string|int $height;

    public DateTime $birthday;

    public bool $subscribeNewsletter = false;
}

$data = new DummyData([
    'name' => 'Roman',
    'height' => 180,
]);
```

### Require properties

[](#require-properties)

When declaring required properties, the DTO will validate all parameters against the declared properties. Take a look at the [validation table](#validation) for more details.

```
use romanzipp\DTO\AbstractData;
use romanzipp\DTO\Attributes\Required;

class DummyData extends AbstractData
{
    #[Required]
    public string $name;
}

$data = new DummyData([]);
```

> romanzipp\\DTO\\Exceptions\\InvalidDataException: The required property `name` is missing

### Array methods

[](#array-methods)

#### Simple array representation

[](#simple-array-representation)

To get an array representation of the DTO, simply call the `toArray` instance method.

When transferring the DTO properties to an array format, the package will respect and call any `toArray` methods of nested DTO instances or otherwise fall back to any declared [`jsonSerialize`](https://www.php.net/manual/de/jsonserializable.jsonserialize.php) method when implementing the [`JsonSerializable`](https://www.php.net/manual/de/class.jsonserializable.php) interface.

```
use romanzipp\DTO\AbstractData;

class DummyData extends AbstractData
{
    public string $firstName;

    public DummyData $childData;

    /** @var self[] */
    public array $children = [];
}

$data = new DummyData([
    'firstName' => 'Roman',
    'childData' => new DummyData([
        'firstName' => 'Tim',
     ]),
    'children' => [
        new DummyData([
            'firstName' => 'Tom'
        ]),
    ],
]);

$data->toArray();
// [
//    'firstName' => 'Roman',
//    'childData' => ['firstName' => 'Tim']
//    'children' => [
//        ['firstName' => 'Tom']
//    ]
// ];
```

#### Convert keys

[](#convert-keys)

The `toArrayConverted` method allows the simple conversion of property keys to a given case.

```
use romanzipp\DTO\AbstractData;
use romanzipp\DTO\Cases;

class DummyData extends AbstractData
{
    public string $firstName;
}

$data = new DummyData([
    'firstName' => 'Roman',
]);

$data->toArrayConverted(Cases\CamelCase::class);  // ['firstName' => 'Roman'];
$data->toArrayConverted(Cases\KebabCase::class);  // ['first-name' => 'Roman'];
$data->toArrayConverted(Cases\PascalCase::class); // ['FirstName' => 'Roman'];
$data->toArrayConverted(Cases\SnakeCase::class);  // ['first_name' => 'Roman'];
```

### Flexible DTOs

[](#flexible-dtos)

When attaching the `Flexible` attribute you can provide more parameters than declared in the DTO instance. All properties will also be included in the `toArray` methods. This would otherwise throw an [`InvalidDataException`](src/Exceptions/InvalidDataException.php).

```
use romanzipp\DTO\AbstractData;
use romanzipp\DTO\Attributes\Flexible;

#[Flexible]
class DummyData extends AbstractData
{
    public string $name;
}

$data = new DummyData([
    'name' => 'Roman',
    'website' => 'ich.wtf',
]);

$data->toArray(); // ['name' => 'Roman', 'website' => 'ich.wtf];
```

Validation
----------

[](#validation)

DefinitionRequiredValueValid`isset()``public $foo`no`''`✅✅`public $foo`no`NULL`✅✅`public $foo`no*none*✅✅`public $foo`**yes**`''`✅✅`public $foo`**yes**`NULL`✅✅`public $foo`**yes***none*🚫-`public string $foo`no`''`✅✅`public string $foo`no`NULL`🚫-`public string $foo`no*none*✅🚫`public string $foo`**yes**`''`✅✅`public string $foo`**yes**`NULL`🚫-`public string $foo`**yes***none*🚫-`public ?string $foo`no`''`✅✅`public ?string $foo`no`NULL`✅✅`public ?string $foo`no*none*✅🚫`public ?string $foo`**yes**`''`✅✅`public ?string $foo`**yes**`NULL`✅✅`public ?string $foo`**yes***none*🚫-`public ?string $foo = null`no`''`✅✅`public ?string $foo = null`no`NULL`✅✅`public ?string $foo = null`no*none*✅✅`public ?string $foo = null`**yes**`''`⚠️\*-`public ?string $foo = null`**yes**`NULL`⚠️\*-`public ?string $foo = null`**yes***none*⚠️\*-\* Attributes with default values cannot be required.

Testing
-------

[](#testing)

```
./vendor/bin/phpunit

```

Credits
-------

[](#credits)

- [Roman Zipp](https://github.com/romanzipp)

This package has been inspired by [Spaties Data-Transfer-Object](https://github.com/spatie/data-transfer-object) released under the [MIT License](https://github.com/spatie/data-transfer-object/blob/2.5.0/LICENSE.md).

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity32

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% 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 ~26 days

Recently: every ~63 days

Total

31

Last Release

1242d ago

Major Versions

1.2.0 → 2.2.02021-10-03

1.2.1 → 2.2.12022-02-04

1.2.2 → 2.2.32022-04-06

1.2.3 → 2.3.02022-04-06

1.x-dev → 2.3.12022-12-14

PHP version history (3 changes)0.0.1PHP ^7.4

1.0.6PHP ^7.4|^8.0

2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/309ea408cc915d1d37b370796df57a24ec31f0b65da69f69c650c8983f9c33a6?d=identicon)[romanzipp](/maintainers/romanzipp)

---

Top Contributors

[![romanzipp](https://avatars.githubusercontent.com/u/11266773?v=4)](https://github.com/romanzipp "romanzipp (91 commits)")[![spekulatius](https://avatars.githubusercontent.com/u/8433587?v=4)](https://github.com/spekulatius "spekulatius (1 commits)")

---

Tags

data-transferdtophpphp7php8showcase

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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