PHPackages                             main-echo/simple-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. [API Development](/categories/api)
4. /
5. main-echo/simple-dto

ActiveLibrary[API Development](/categories/api)

main-echo/simple-dto
====================

A fork of `phpexperts/simple-dto`—a quick and easy DTO package.

v2.5.0.1(5y ago)01.5kMITPHPPHP &gt;=7.2

Since Mar 29Pushed 5y agoCompare

[ Source](https://github.com/main-echo/SimpleDTO)[ Packagist](https://packagist.org/packages/main-echo/simple-dto)[ Docs](https://www.phpexperts.pro/)[ RSS](/packages/main-echo-simple-dto/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (8)Versions (20)Used By (0)

SimpleDTO
=========

[](#simpledto)

[![TravisCI](https://camo.githubusercontent.com/789f3d87304ba87aef0cf08781f300a0f67d42b6a6c99b342e2053b27d826d85/68747470733a2f2f7472617669732d63692e636f6d2f70687065787065727473696e632f53696d706c6544544f2e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/phpexpertsinc/SimpleDTO)[![Maintainability](https://camo.githubusercontent.com/a2a527d9a40c03717fe606aac46785e72d51763c51731c39dc5e4ab6cbda3e93/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35303363626130633533656232363263393437612f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/phpexpertsinc/SimpleDTO/maintainability)[![Test Coverage](https://camo.githubusercontent.com/54eaa7442ea0c2dc73862c27f6b580f29fbb89ce7c13b63004ade8fccb029a37/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35303363626130633533656232363263393437612f746573745f636f766572616765)](https://codeclimate.com/github/phpexpertsinc/SimpleDTO/test_coverage)

SimpleDTO is a PHP Experts, Inc., Project meant to facilitate easy Data Transfer Objects.

Basically, any protected property on the DTO can be set as an array element passed in to the \_\_constructor and/or as a default value on the property itself.

The DTOs are immutable: Once created, they cannot be changed. Create a new object instead.

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

[](#installation)

Via Composer

```
composer require phpexperts/simple-dto
```

Usage
-----

[](#usage)

As of version 2, you *must* define class-level @property docblocks for each one of your properties.

You also must define the data type.

```
use Carbon\Carbon;
use PHPExperts\SimpleDTO\SimpleDTO;

/**
 * @property-read string $name
 * @property-read Carbon $name
 */
class BirthdayDTO extends SimpleDTO
{
    /** @var string */
    protected $name;

    /** @var Carbon */
    protected $date;
}

$birthdayDTO = new BirthdayDTO([
    'name' => 'Donald J. Trump',
    'date' => '1946-06-14',
]);

// Access as a property:
echo $birthday->name; // Donald J. Trump

// Properties with the data type of "Carbon" or "Carbon\Carbon"
// are automagically converted to Carbon objects.
echo $birthday->date->format('F jS, Y'); // June 14th, 1946

// Easily output as an array:
$birthday->toArray();

// Copy from one to another:
$newDTO = new BirthdayDTO($birthdayDTO->toArray());

// Copy from one to another, with new properties:
$newDTO = new BirthdayDTO($birthdayDTO->toArray() + [
    'date' => '2020-11-03',
]);

// Easily output as JSON:
echo json_encode($birthdayDTO);
/* Output:
{
    "name": "Donald J. Trump",
    "date": "1946-06-14T00:00:00.000000Z"
}
*/
```

### Fuzzy Data Types

[](#fuzzy-data-types)

But what if you aren't ready / able to dive into strict PHP data types yet?

Well, just instantiate the parent class like this:

```
    use PHPExperts\DataTypeValidator\DataTypeValidator;
    use PHPExperts\DataTypeValidator\IsAFuzzyDataType;

    /**
     * @property int   $daysAlive
     * @property float $age
     * @property bool  $isHappy
     */
    class MyFuzzyDTO extends SimpleDTO
    {
        public function __construct(array $input)
        {
            parent::__construct($input, new DataTypeValidator(new IsAFuzzyDataType());
        }
    }

    $person = new MyFuzzyDTO([
        'daysAlive' => '5000',
        'age'       => '13.689',
        'isHappy'   => 1,
    ]);

    echo json_encode($person, JSON_PRETTY_PRINT);
    /*
    {
        "daysAlive": "5000",
        "age": "13.689",
        "isHappy": 1
    }
    */
```

NestedDTOs
----------

[](#nesteddtos)

You can nest DTOs inside of each other.

```
    $myDTO = new MyTestDTO([
        'name' => 'PHP Experts, Inc.',
        'age'  => 7.01,
        'year' => 2019,
    ]);

    /**
     * @property MyTestDTO $myDTO
     */
    $dto = new class(['myDTO' => $myDTO], ['myDTO' => MyTestDTO::class]) extends NestedDTO
    {
    };

    /*
    PHPExperts\SimpleDTO\NestedDTO@anonymous {
      -dataTypeRules: array:1 [
        "myDTO" => "?MyTestDTO"
      ]
      -data: array:1 [
        "myDTO" => PHPExperts\SimpleDTO\Tests\MyTestDTO {#355
          -dataTypeRules: array:3 [
            "name" => "?string"
            "age" => "?float"
            "year" => "?int"
          ]
          -data: array:3 [
            "name" => "PHP Experts, Inc."
            "age" => 7.01
            "year" => 2019
          ]
        }
      ]
    }
    */
```

Use cases
=========

[](#use-cases)

PHPExperts\\SimpleDTO\\SimpleDTO
✔ Properties are set via the constructor
✔ Properties are accessed as public properties
✔ Public, private and static protected properties will be ignored
✔ Each DTO is immutable
✔ Setting any property returns an exception
✔ Concrete properties can be used to set default values
✔ Properties with the type carbon become carbon dates
✔ Can easily output to array
✔ Can easily be json encoded
✔ Can easily be json decoded
✔ Nullable properties are allowed
✔ Every property is nullable with permissive mode
✔ Can be serialized
✔ Can be unserialized
✔ Extra validation can be added

PHPExperts\\SimpleDTO\\NestedDTO
✔ Will construct nested DTOs
✔ Can construct arrays of nested DTOs
✔ Will convert arrays into the appropriate Nested DTOs
✔ Will convert stdClasses into the appropriate Nested DTOs
✔ Nested DTOs use Loose typing
✔ All registered Nested DTOs are required
✔ Optional, unregistered, Nested DTOs are handled gracefully
✔ Can be serialized
✔ Can be unserialized

PHPExperts\\SimpleDTO\\WriteOnceTrait
✔ Can accept null values
✔ Will validate on serialize
✔ Will validate on to array
✔ Can write each null value once
✔ Write-Once values must validate

SimpleDTO Sad Paths
✔ Cannot initialize with a nonexisting property
✔ Accessing a nonexisting property throws an error
✔ A DTO must have class property docblocks for each concrete property
✔ Carbon date strings must be parsable dates
✔ Properties must match their data types
✔ Will not unserialize DTOs with invalid data
✔ Cannot overwrite a non-existing property

Testing
-------

[](#testing)

```
phpunit --testdox
```

Contributors
============

[](#contributors)

[Theodore R. Smith](https://www.phpexperts.pro/%5D)
GPG Fingerprint: 4BF8 2613 1C34 87AC D28F 2AD8 EB24 A91D D612 5690
CEO: PHP Experts, Inc.

License
-------

[](#license)

MIT license. Please see the [license file](LICENSE) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 95.8% 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 ~32 days

Recently: every ~107 days

Total

18

Last Release

2047d ago

Major Versions

v1.0.x-dev → v2.0.02019-05-13

PHP version history (2 changes)v1.0.0PHP &gt;=7.1

v2.5.0PHP &gt;=7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/6ca7667a4444a73aeaf91b653d0dd4302ab565749b2342307bc0cc3caaef9fad?d=identicon)[hettiger](/maintainers/hettiger)

---

Top Contributors

[![hopeseekr](https://avatars.githubusercontent.com/u/1125541?v=4)](https://github.com/hopeseekr "hopeseekr (68 commits)")[![hettiger](https://avatars.githubusercontent.com/u/4583871?v=4)](https://github.com/hettiger "hettiger (2 commits)")[![mn-martin](https://avatars.githubusercontent.com/u/12658648?v=4)](https://github.com/mn-martin "mn-martin (1 commits)")

---

Tags

apirestfuldtodata transfer objects

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/main-echo-simple-dto/health.svg)

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

###  Alternatives

[phpexperts/simple-dto

A quick and easy DTO package.

521.1M7](/packages/phpexperts-simple-dto)[teepluss/api

Laravel 4 Internal Request (HMVC)

7034.0k](/packages/teepluss-api)[rekalogika/mapper

An object mapper for PHP and Symfony. Maps an object to another object. Primarily used for transforming an entity to a DTO and vice versa.

3847.7k1](/packages/rekalogika-mapper)

PHPackages © 2026

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