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

ActiveLibrary

bssphp/dto
==========

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

1.4(3y ago)021↓100%1MITPHPPHP ^8.0

Since Jan 16Pushed 3y ago1 watchersCompare

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

READMEChangelog (5)Dependencies (5)Versions (6)Used By (1)

dto
===

[](#dto)

[![Latest Stable Version](https://camo.githubusercontent.com/9be3f1d507e41cadaf0b833e3566c528a9fdaf090850e8812708283e29d1e613/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6273737068702f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/dto)[![Total Downloads](https://camo.githubusercontent.com/5b280333eaf2f3f92a45f10c5e70f4e8cc6738dad291dd385a85f4840a70a32a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6273737068702f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/dto)[![License](https://camo.githubusercontent.com/3c0cb3ac0b284cf2f1f54b0b8844560fadc023417cebc3e464ff0b00af6b3261/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6273737068702f64746f2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/bssphp/dto)[![GitHub Build Status](https://camo.githubusercontent.com/3406e610d5e3febf7c78a3f1f57b33910604415c9b305d24360745c1dcd6bdba/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6273737068702f64746f2f54657374733f7374796c653d666c61742d737175617265)](https://github.com/bssphp/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 bssphp/dto

```

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

Usage
-----

[](#usage)

```
use bssphp\dto\AbstractData;
use bssphp\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 bssphp\dto\AbstractData;
use bssphp\dto\Attributes\Required;

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

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

> bssphp\\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 bssphp\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 bssphp\dto\AbstractData;
use bssphp\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 bssphp\dto\AbstractData;
use bssphp\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/bssphp)

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

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity54

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 ~0 days

Total

5

Last Release

1218d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/6843e87aada702611e52851f3a751ed2150d054fbf4e9152b6ffc754fcf7a36a?d=identicon)[beanstacksys](/maintainers/beanstacksys)

---

Top Contributors

[![beanstacksys](https://avatars.githubusercontent.com/u/59993458?v=4)](https://github.com/beanstacksys "beanstacksys (1 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

PHPackages © 2026

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