PHPackages                             dldash/data-transfer-object - 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. dldash/data-transfer-object

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

dldash/data-transfer-object
===========================

Data transfer objects.

1.0.0(5y ago)025MITPHPPHP ^8.0

Since Dec 9Pushed 5y ago1 watchersCompare

[ Source](https://github.com/dldash/data-transfer-object)[ Packagist](https://packagist.org/packages/dldash/data-transfer-object)[ RSS](/packages/dldash-data-transfer-object/feed)WikiDiscussions master Synced today

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

💡 Data transfer objects
=======================

[](#-data-transfer-objects)

[![Latest Version](https://camo.githubusercontent.com/e283a89a1b81c22b06e460e7f29491c84bf2b7df6bd8fe2eac137c9fd291a5ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646c646173682f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://github.com/dldash/data-transfer-object/releases)[![License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/78a70e697ab10dcb6db43d02cc941aff1a426aff9cb0851827011ac6ca4d4093/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f646c646173682f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/dldash/data-transfer-object)[![Build](https://github.com/dldash/data-transfer-object/workflows/build/badge.svg)](https://github.com/dldash/data-transfer-object/actions)

⚡️ Requirements
---------------

[](#️-requirements)

- 🐘 PHP &gt;= 8.0

💥 Installation
--------------

[](#-installation)

```
composer require dldash/data-transfer-object
```

✨ Usage
-------

[](#-usage)

- 👉 [Simple DTO](#simple-dto)
- 👉 [Value Objects](#value-objects)
- 👉 [Nested DTO classes](#nested-dto-classes)
- 👉 [Typed DTO arrays and collections](#typed-dto-arrays-and-collections)
- 👉 [Partial update](#partial-update)
- 👉 [Serialized name](#serialized-name)

### Simple DTO

[](#simple-dto)

If extra fields are passed that are not described in the DTO class, they will be ignored.

DTO class:

```
use Dldash\DataTransferObject\Models\DataTransferObject;

class UserDto extends DataTransferObject
{
    public function __construct(
        public int $userId,
        public string|null $username
    ) {}
}
```

Usage:

```
$request = [
    'userId' => 100,
    'username' => 'admin',
    'emailAddress' => 'admin@test.com'
];

$dto = UserDto::create($request);
```

### Value Objects

[](#value-objects)

You can also use value objects in DTO classes.
All you need is to implement the `ValueObjectContract` interface.

Value object class:

```
use Dldash\DataTransferObject\Contracts\ValueObjectContract;

class EmailAddress implements ValueObjectContract, JsonSerializable
{
    public function __construct(private string $value)
    {
        if (!filter_var($value, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException("Email address [${value}] is not valid.");
        }

        $this->value = strtolower($value);
    }

    public function value(): string
    {
        return $this->value;
    }

    public function jsonSerialize(): string
    {
        return $this->value;
    }
}
```

DTO class:

```
use Dldash\DataTransferObject\Models\DataTransferObject;

class OrderDto extends DataTransferObject
{
    public function __construct(
        public int $orderId,
        public EmailAddress $emailAddress
    ) {}
}
```

Usage:

```
$request = [
    'orderId' => 100,
    'emailAddress' => 'admin@test.com'
];

$dto = OrderDto::create($request);
```

### Nested DTO classes

[](#nested-dto-classes)

DTO class:

```
use Dldash\DataTransferObject\Models\DataTransferObject;

class OrderDto extends DataTransferObject
{
    public function __construct(
        public int $orderId,
        public UserDto $user
    ) {}
}
```

Usage:

```
$request = [
    'orderId' => 100,
    'user' => [
        'userId' => 100,
        'username' => 'admin'
    ]
];

$dto = OrderDto::create($request);
```

### Typed DTO arrays and collections

[](#typed-dto-arrays-and-collections)

You can use arrays of DTO objects.
To do this, you need to inherit the abstract `DataTransferObjectCollection` class.

Collection class:

```
use Dldash\DataTransferObject\Objects\DataTransferObjectCollection;

/** @method ArrayIterator|UserDto[] getIterator() */
class UserDtoCollection extends DataTransferObjectCollection
{
    protected function create(mixed $item): object
    {
        return UserDto::create($item);
    }
}
```

DTO class:

```
use Dldash\DataTransferObject\Models\DataTransferObject;

class OrderDto extends DataTransferObject
{
    public function __construct(
        public int $orderId,
        public UserDtoCollection $users
    ) {}
}
```

Usage:

```
$request = [
    'orderId' => 100,
    'users' => [
        [
            'userId' => 100,
            'username' => 'admin'
        ],
        [
            'userId' => 200,
            'username' => null
        ]
    ]
];

$dto = OrderDto::create($request);
```

### Partial update

[](#partial-update)

Let's imagine that we need to update some model, but we want to do a partial update. In this case, not all the required fields can be passed to the DTO class. You can add the `Undefined` type to the desired field.

NOTE: If you pass a `null` value, it will also be `null`.

DTO class:

```
use Dldash\DataTransferObject\Objects\Undefined;
use Dldash\DataTransferObject\Models\DataTransferObject;

class OrderDto extends DataTransferObject
{
    public function __construct(
        public int $orderId,
        public string|null|Undefined $name
    ) {}
}
```

Usage:

```
use Dldash\DataTransferObject\Objects\Undefined;

$request = [
    'orderId' => 100
];

$dto = OrderDto::create($request);

if (Undefined::isPresent($dto->name)) {
    // Update this field
}
```

### Serialized name

[](#serialized-name)

DTO class:

```
use Dldash\DataTransferObject\Attributes\SerializedName;
use Dldash\DataTransferObject\Models\DataTransferObject;

class OrderDto extends DataTransferObject
{
    public function __construct(
        #[SerializedName('order_id')]
        public int $id,

        #[SerializedName('order_name')]
        public string $name
    ) {}
}
```

Usage:

```
$request = [
    'order_id' => 100,
    'order_name' => 'Order'
];

$dto = OrderDto::create($request);

echo $dto->id; // 100
echo $dto->name; // Order
echo json_encode($dto); // {"order_id": 100, "order_name": "Order"}
```

💫 Testing
---------

[](#-testing)

```
composer test
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity63

Established project with proven stability

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

Total

5

Last Release

2010d ago

Major Versions

0.3.0 → 1.0.02020-12-31

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5787193?v=4)[Sergey Sorokin](/maintainers/devemio)[@devemio](https://github.com/devemio)

---

Tags

dtophpserialized-namevalue-object

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dldash-data-transfer-object/health.svg)

```
[![Health](https://phpackages.com/badges/dldash-data-transfer-object/health.svg)](https://phpackages.com/packages/dldash-data-transfer-object)
```

###  Alternatives

[cakemanager/cakephp-utils

Plugin for CakePHP 3.x with useful components and behaviors

3180.9k4](/packages/cakemanager-cakephp-utils)[arielmejiadev/filament-printable

Package to get your Filament Resources Printable

185.5k](/packages/arielmejiadev-filament-printable)

PHPackages © 2026

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