{"id":36978683,"url":"https://github.com/amorphine/data-transfer-object","last_synced_at":"2026-01-13T22:48:12.450Z","repository":{"id":56947260,"uuid":"276402032","full_name":"amorphine/data-transfer-object","owner":"amorphine","description":"PHP library to create objects from arrays","archived":false,"fork":false,"pushed_at":"2021-01-29T09:48:46.000Z","size":33,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-10-19T21:38:00.979Z","etag":null,"topics":["arrays","casting","data-transfer","dto","model","nested-dtos","object","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/amorphine.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-01T14:38:21.000Z","updated_at":"2025-05-15T11:08:53.000Z","dependencies_parsed_at":"2022-08-21T03:10:27.738Z","dependency_job_id":null,"html_url":"https://github.com/amorphine/data-transfer-object","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/amorphine/data-transfer-object","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amorphine%2Fdata-transfer-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amorphine%2Fdata-transfer-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amorphine%2Fdata-transfer-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amorphine%2Fdata-transfer-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amorphine","download_url":"https://codeload.github.com/amorphine/data-transfer-object/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amorphine%2Fdata-transfer-object/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28403742,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-13T21:51:37.118Z","status":"ssl_error","status_checked_at":"2026-01-13T21:45:14.585Z","response_time":56,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arrays","casting","data-transfer","dto","model","nested-dtos","object","php"],"created_at":"2026-01-13T22:48:11.732Z","updated_at":"2026-01-13T22:48:12.436Z","avatar_url":"https://github.com/amorphine.png","language":"PHP","readme":"# Data transfer object for arrays\nZero-dependency library to convert associative arrays to an object model\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require amorphine/data-transfer-object\n```\n\n## Purpose\n\nThe goal of this package is to give a convenient declared way to manipulate data extracted from associative arrays.\nAdvantages of object structure:\n\n- Type safety (as long as PHP makes it possible)\n- Statically analyze and auto completion.\n- Scalar type casting\n\nLet's look at the example of a JSON API call:\n\n```php\n$post = $api-\u003eget('posts', 1); \n\n[\n    'title' =\u003e '…',\n    'body' =\u003e '…',\n    'author' =\u003e '[\"id\" =\u003e 1, ...]',\n]\n```\n\nWorking with this array is difficult, as we'll always have to refer to the documentation to know what's exactly in it. \nThis package allows you to create data transfer object definitions, classes, which will represent the data in a structured way.\n\nWe did our best to keep the syntax and overhead as little as possible:\n\n```php\nclass PostData extends DataTransferObject\n{\n    /** @var string */\n    public $title;\n    \n    /** @var string */\n    public $body;\n    \n    /** @var AnotherDataTransferObjectImplementation */\n    public $author;\n}\n```\n\nAn object of `PostData` can from now on be constructed like so:\n\n```php\n$postData = new PostData([\n    'title' =\u003e '…',\n    'body' =\u003e '…',\n    'author' =\u003e '…',\n]);\n```\n\nNow you can use this data in a structured way:\n\n```php\n$postData-\u003etitle;\n$postData-\u003ebody;\n$postData-\u003eauthor;\n```\n\nBy adding doc blocks to our properties, their values will be validated against the given type; \nand a `TypeError` will be thrown if the value doesn't comply with the given type.\n\nHere are the possible ways of declaring types:\n\n```php\nuse SomeNameSpace\\Author;\n\nclass PostData extends DataTransferObject\n{\n    /**\n     * Built in types: \n     *\n     * @var string \n     */\n    public $property;\n\n    /**\n     * You can override property data source (array key)\n     *\n     * @source keyWithSomeAnotherName\n     */\n    public $property;\n    \n    /**\n     * Classes with their FQCN: \n     *\n     * @var Author\n     */\n    public $property;\n    \n    /**\n     * Lists of types: \n     *\n     * @var Author[]\n     */\n    public $property;\n    \n    /**\n     * Iterator of types: \n     *\n     * @var iterator\u003cAuthor\u003e\n     */\n    public $property;\n    \n    /**\n     * Union types: \n     *\n     * @var string|int\n     */\n    public $property;\n    \n    /**\n     * Nullable types: \n     *\n     * @var string|null\n     */\n    public $property;\n    \n    /**\n     * Mixed types: \n     *\n     * @var mixed|null\n     */\n    public $property;\n    \n    /**\n     * Any iterator : \n     *\n     * @var iterator\n     */\n    public $property;\n    \n    /**\n     * No type, which allows everything\n     */\n    public $property;\n    \n    /** \n     * PHP types declaration supported\n     */\n    public ?int $property;\n\n}\n```\n\nPHP 7.4 typed properties are the source of types like PHP doc is. The compatibility of declared types is upon to the developer. Feel free to use one of them, both or none at all.\n\n### Source override\nBy default, the array key the value is taken from is the property name. If you need to specify the key of the original array the value should be taken from you should use `@source` doc comment:\n\n```php\nclass PostData extends DataTransferObject\n{\n    /** \n     * @var AuthorData\n     * @source authorData\n     */\n    public $author;\n}\n```\n\n\n### Automatic casting of nested DTOs\n\nIf you've got nested DTO fields, data passed to the parent DTO will automatically be cast.\n\n```php\nclass PostData extends DataTransferObject\n{\n    /** @var AuthorData */\n    public $author;\n}\n```\n\n`PostData` can now be constructed like so:\n\n```php\n$postData = new PostData([\n    'author' =\u003e [\n        'name' =\u003e 'Foo',\n    ],\n]);\n```\n\n### Automatic casting of nested array DTOs\n\nSimilarly to above, nested array DTOs will automatically be cast.\n\n```php\nclass TagData extends DataTransferObject\n{\n    /** @var string */\n   public $name;\n}\n\nclass PostData extends DataTransferObject\n{\n    /** @var TagData[] */\n   public $tags;\n}\n```\n\n`PostData` will automatically construct tags like such:\n\n```php\n$postData = new PostData([\n    'tags' =\u003e [\n        ['name' =\u003e 'foo'],\n        ['name' =\u003e 'bar']\n    ]\n]);\n```\n### Exception handling\n\nBeside property type validation, you can also be certain that the data transfer object in its whole is always valid.\nOn constructing a data transfer object, we'll validate whether all required (non-nullable) properties are set. \nIf not, a `Amorphine\\DataTransferObject\\Exceptions\\DataTransferObjectError` will be thrown.\n\nLikewise, if you're trying to set non-defined properties, you'll get a `DataTransferObjectError`.\n\n### Testing\n\n``` bash\ncomposer test\n```\n\n### Limitations\n- Opcache: types and data sources are resolved through PHP comments so ensure `opcache.save_comments` equals `1`\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Powered by\n- [Data Transfer Object by Spatie](https://spatie.be/open-source) - special thanks to them for some patterns, pieces of code and Readme structure\n- [David Grudl (nette/di)](https://davidgrudl.com/) - thanks for class import solutions based on tokens\n\n## See also\n- [Data Transfer Object by Spatie](https://github.com/spatie/data-transfer-object) - mature and popular solution with rich functionality\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famorphine%2Fdata-transfer-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famorphine%2Fdata-transfer-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famorphine%2Fdata-transfer-object/lists"}