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

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

imbue/data-transfer-object
==========================

1.0.0(3y ago)37.2k↑133.3%1MITPHPPHP ^7.1|~8.0

Since Dec 30Pushed 3y agoCompare

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

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

Data Transfer Object
====================

[](#data-transfer-object)

[![Latest Version](https://camo.githubusercontent.com/8bb793145c55eadd6cad3f0c49b8ec67a3a89ea7466ef915fb2b8bc827f2b4bd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f696d6275652f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://github.com/imbue/data-transfer-object/releases)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Latest Version on Packagist](https://camo.githubusercontent.com/89d97024714976e784bfd253c88a42a70171a323c520a79c9db270db252f2a0c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696d6275652f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/imbue/data-transfer-object)[![Total Downloads](https://camo.githubusercontent.com/5dd7b744dda81b0db7e6653dc259d89d8fb79765b050c159aae18be0da061b9b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696d6275652f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/imbue/data-transfer-object)

A variation / interpretation of the Data Transfer Object (DTO) design pattern (Distribution Pattern). A DTO is nothing more than an object that can hold some data. Most commonly it is used for transporting that data between system layers.

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

[](#installation)

You can install the package via composer

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

Usage
-----

[](#usage)

- [Introduction](#introduction)
- [Accessors &amp; Mutators](#getters-and-setters)
    - [Defining A Getter](#defining-a-getter)
    - [Defining A Setter](#defining-a-setter)
- [Serializing Objects &amp; Collections](#serializing-objects-and-collections)
    - [Serializing To Arrays](#serializing-to-arrays)
    - [Serializing To JSON](#serializing-to-json)
- [Collections](#collections)
- [Helpers](#helpers)
- [Example](#example)

Introduction
------------

[](#introduction)

Using getter/setter methods gives the advantage of type hinting all data being set. Thus any data object will be transparent and easy to use without the need of additional documentation, for example the API client you're writing.

Getters &amp; Setters
---------------------

[](#getters--setters)

### Defining A Getter

[](#defining-a-getter)

To define a getter, simply create a `get...()` method on your data object

```
class BookData extends DataTransferObject
{
    protected $title;
    protected $author;

    /**
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * @return AuthorData
     */
    public function getAuthor(): AuthorData
    {
        return $this->author;
    }
}
```

### Defining A Setter

[](#defining-a-setter)

To define a setters, simply create a `set...()` method on your data object

```
class BookData extends DataTransferObject
{
    protected $title;
    protected $author;

    /**
     * @param nullable|string $title
     */
    public function setTitle(?string $title)
    {
        return $this->title;
    }

    /**
     * @param AuthorData $author
     */
    public function setAuthor(AuthorData $author)
    {
        return $this->author;
    }
}
```

Serializing Objects &amp; Collections
-------------------------------------

[](#serializing-objects--collections)

### Serializing To Arrays

[](#serializing-to-arrays)

To convert a value object and its nested objects/collections to an array, you can use the `toArray` method. This method is recursive, so all attributes will be converted to arrays:

```
return $dataObject->toArray();
```

### Serializing To JSON

[](#serializing-to-json)

To convert a value object and its nested objects/collections to a JSON object, you can use the `toJson` method. This method is recursive, so all attributes will be converted into JSON:

```
return $dataObject->toJson();
```

Collections
-----------

[](#collections)

In some cases you may want to have a collection of multiple data objects. By extending the provided `DataTransferObjectColletion` class, you can easily set up a group of DTOs

```
$collection = new BooksCollection([
    $bookOne,
    $bookTwo,
    $bookThree
]);

$collection->toArray();
```

#### Auto completion for collections

[](#auto-completion-for-collections)

By overriding the `current()` method and setting the return value, you can enable type hinting.

```
class BooksCollection extends DataTransferObjectCollection
{
    public function current(): BookData
    {
        return parent::current();
    }
}
```

```
foreach ($booksCollection as $bookData) {
    $bookData-> // type hinting
}
```

Helpers
-------

[](#helpers)

There are a few helper methods to provide some extra functionality

#### only()

[](#only)

```
$dataObject
    ->only('title')
    ->toArray();
```

#### except()

[](#except)

```
$dataObject
    ->except('title')
    ->toArray();
```

##### Immutability

[](#immutability)

These helpers are immutable, which means they wont affect the original data transfer object.

Example
-------

[](#example)

Using the data objects is made as simple as possible

```
$book = new BookData();
$book->setTitle('Harry Potter: The Goblet of Fire');

$author = new Author();
// ....

$book->setAuthor($author);
```

Security
--------

[](#security)

If you discover any security related issues, please use the issue tracker.

Testing
-------

[](#testing)

```
composer test
```

Credits
-------

[](#credits)

- This package is based on the [data-transfer-object package](https://github.com/spatie/data-transfer-object) from [Spatie](https://github.com/spatie).
- `Arr` class contains functions copied from [Laravel's](https://github.com/laravel) Arr helper.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 91.7% 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 ~799 days

Total

3

Last Release

1145d ago

Major Versions

0.1.1 → 1.0.02023-05-16

PHP version history (2 changes)0.1.0PHP ^7.1

1.0.0PHP ^7.1|~8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/28404530?v=4)[Liam](/maintainers/imbue)[@imbue](https://github.com/imbue)

---

Top Contributors

[![imbue](https://avatars.githubusercontent.com/u/28404530?v=4)](https://github.com/imbue "imbue (11 commits)")[![TimmyGuy](https://avatars.githubusercontent.com/u/14965875?v=4)](https://github.com/TimmyGuy "TimmyGuy (1 commits)")

---

Tags

data-transfer-objectdtophpphpdata-transfer-object

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21623.4k](/packages/imanghafoori-laravel-anypass)[php-collective/dto

Framework-agnostic Data Transfer Object library with code generation

2814.6k7](/packages/php-collective-dto)

PHPackages © 2026

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