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

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

antwerpes/data-transfer-object
==============================

Simple json encoding and decoding for PHP data objects

1.0.5(1y ago)299.2k—9.1%[3 PRs](https://github.com/antwerpes/data-transfer-object/pulls)2MITPHPPHP ^8.2CI passing

Since Sep 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/antwerpes/data-transfer-object)[ Packagist](https://packagist.org/packages/antwerpes/data-transfer-object)[ Docs](https://github.com/antwerpes/data-transfer-object)[ RSS](/packages/antwerpes-data-transfer-object/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (9)Used By (2)

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

[](#data-transfer-object)

[![Latest Version on Packagist](https://camo.githubusercontent.com/9646fe7ec90a8945c691d95d5e7f63599d747c63771550481693c5f8c535decb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e747765727065732f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/antwerpes/data-transfer-object)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/ff7b316b2e98ca41fa3fc842814402e546febe8eb5935f1b4b3b30cc514fa2f2/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616e747765727065732f646174612d7472616e736665722d6f626a6563742f6c696e742e796d6c3f6272616e63683d6d6173746572)](https://github.com/antwerpes/data-transfer-object/actions?query=workflow%3Alint+branch%3Amaster)[![Total Downloads](https://camo.githubusercontent.com/6f25349b25ca3d9dc9a99033da00298c30e2ff28a4671de62ad041ebb4a61e8b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e747765727065732f646174612d7472616e736665722d6f626a6563742e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/antwerpes/data-transfer-object)

Simple library for encoding and decoding JSON structures into PHP objects, e.g. to work with API responses in a strongly typed way.

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

[](#installation)

You can install the package via composer:

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

Usage
-----

[](#usage)

Define a class that extends `Antwerpes\DataTransferObject\DataTransferObject` and define the structure of the object:

```
use Antwerpes\DataTransferObject\Attributes\Cast;
use Antwerpes\DataTransferObject\Attributes\Map;
use Antwerpes\DataTransferObject\Casts\ArrayCaster;
use Antwerpes\DataTransferObject\DataTransferObject;

class User extends DataTransferObject
{
    public function __construct(
        public string $name,
        #[Cast(CustomDateCaster::class)]
        public DateTimeInterface $birthday,
        #[Map(from: 'address.city')]
        public string $city,
        #[Cast(ArrayCaster::class, itemType: Interest:class)]
        public array $interests,
    ) {}
}
```

Then you can use the class to decode JSON strings into PHP objects:

```
$json = '{
    "name": "John Doe",
    "birthday": "1990-01-01",
    "address": {
        "city": "New York"
    },
    "interests": [
        {
            "name": "Music"
        },
        {
            "name": "Programming"
        }
    ]
}';
$user = User::decode(json_decode($json, true));
$encoded = $user->encode();
```

### Custom Casters

[](#custom-casters)

You can define custom casters by implementing the `Antwerpes\DataTransferObject\CastsProperty` interface:

```
use Antwerpes\DataTransferObject\CastsProperty;

class CustomDateCaster implements CastsProperty
{
    public function unserialize(mixed $value): DateTimeInterface
    {
        return new DateTime($value);
    }

    public function serialize(mixed $value): string
    {
        return $value->format('Y-m-d');
    }
}
```

### Mapping

[](#mapping)

You can map nested properties to a flat structure using the `Map` attribute:

```
use Antwerpes\DataTransferObject\Attributes\Map;

class User extends DataTransferObject
{
    public function __construct(
        #[Map(from: 'address.city', to: 'address.city')]
        public string $city,
    ) {}
}
```

### Validation

[](#validation)

Validation is out of scope for this package, use JSON schemas or other libraries like `symfony/validator`to validate the object.

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Contributions are welcome! Leave an issue on GitHub, or create a Pull Request.

Credits
-------

[](#credits)

- [Elisha Witte](https://github.com/chiiya)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance66

Regular maintenance activity

Popularity35

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 60.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 ~5 days

Total

6

Last Release

586d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/34a7cd6c6e8c5fd30b6cd28645c3384f343fc90d10b0df8c80c7ad424e3dba2a?d=identicon)[chiiya](/maintainers/chiiya)

---

Top Contributors

[![chiiya](https://avatars.githubusercontent.com/u/15029301?v=4)](https://github.com/chiiya "chiiya (17 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (3 commits)")

---

Tags

chiiyajson-encoding

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[garveen/laravoole

Get 10x performance for Laravel on Swoole or Workerman

8834.9k](/packages/garveen-laravoole)[aneeskhan47/laravel-pagination-merge

Merge multiple laravel paginate instances

1823.4k](/packages/aneeskhan47-laravel-pagination-merge)

PHPackages © 2026

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