{"id":29926507,"url":"https://github.com/imbue/data-transfer-object","last_synced_at":"2025-08-02T12:41:36.784Z","repository":{"id":56990284,"uuid":"163615536","full_name":"imbue/data-transfer-object","owner":"imbue","description":"Using getter/setter methods gives the advantage of type hinting all data being set.","archived":false,"fork":false,"pushed_at":"2023-05-22T07:04:01.000Z","size":14,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-07-07T13:42:10.527Z","etag":null,"topics":["data-transfer-object","dto","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/imbue.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":"2018-12-30T20:28:08.000Z","updated_at":"2024-12-17T10:51:15.000Z","dependencies_parsed_at":"2022-08-21T13:50:15.587Z","dependency_job_id":null,"html_url":"https://github.com/imbue/data-transfer-object","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/imbue/data-transfer-object","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbue%2Fdata-transfer-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbue%2Fdata-transfer-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbue%2Fdata-transfer-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbue%2Fdata-transfer-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imbue","download_url":"https://codeload.github.com/imbue/data-transfer-object/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbue%2Fdata-transfer-object/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268391999,"owners_count":24243296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-02T02:00:12.353Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["data-transfer-object","dto","php"],"created_at":"2025-08-02T12:41:34.270Z","updated_at":"2025-08-02T12:41:36.739Z","avatar_url":"https://github.com/imbue.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Data Transfer Object\n\n[![Latest Version][ico-version]][link-version]\n[![Software License][ico-license]](LICENSE.md)\n[![Latest Version on Packagist][ico-packagist]][link-packagist]\n[![Total Downloads][ico-downloads]][link-downloads]\n\nA 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.\n\n## Installation\nYou can install the package via composer\n\n```bash\ncomposer require imbue/data-transfer-object\n```\n\n## Usage\n- [Introduction](#introduction)\n- [Accessors \u0026 Mutators](#getters-and-setters)\n    - [Defining A Getter](#defining-a-getter)\n    - [Defining A Setter](#defining-a-setter)\n- [Serializing Objects \u0026 Collections](#serializing-objects-and-collections)\n    - [Serializing To Arrays](#serializing-to-arrays)\n    - [Serializing To JSON](#serializing-to-json)\n- [Collections](#collections)\n- [Helpers](#helpers)\n- [Example](#example)\n\n\n\u003ca name=\"introduction\"\u003e\u003c/a\u003e\n## Introduction\n\nUsing 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. \n\n\u003ca name=\"getters-and-setters\"\u003e\u003c/a\u003e\n## Getters \u0026 Setters\n\n\u003ca name=\"defining-a-getter\"\u003e\u003c/a\u003e\n### Defining A Getter\n\nTo define a getter, simply create a `get...()` method on your data object\n\n```php\nclass BookData extends DataTransferObject\n{\n    protected $title;\n    protected $author;\n\n    /**\n     * @return string\n     */\n    public function getTitle()\n    {\n        return $this-\u003etitle;\n    }\n\n    /**\n     * @return AuthorData\n     */\n    public function getAuthor(): AuthorData\n    {\n        return $this-\u003eauthor;\n    }\n}\n```\n\n\u003ca name=\"defining-a-setter\"\u003e\u003c/a\u003e\n### Defining A Setter\n\nTo define a setters, simply create a `set...()` method on your data object\n\n```php\nclass BookData extends DataTransferObject\n{\n    protected $title;\n    protected $author;\n\n    /**\n     * @param nullable|string $title\n     */\n    public function setTitle(?string $title)\n    {\n        return $this-\u003etitle;\n    }\n\n    /**\n     * @param AuthorData $author\n     */\n    public function setAuthor(AuthorData $author)\n    {\n        return $this-\u003eauthor;\n    }\n}\n```\n\n\n\u003ca name=\"serializing-objects-and-collections\"\u003e\u003c/a\u003e\n## Serializing Objects \u0026 Collections\n\n\u003ca name=\"serializing-to-arrays\"\u003e\u003c/a\u003e\n### Serializing To Arrays\n\nTo 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:\n\n```php\nreturn $dataObject-\u003etoArray();\n```\n\n\u003ca name=\"serializing-to-json\"\u003e\u003c/a\u003e\n### Serializing To JSON\n\nTo 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:\n\n```php\nreturn $dataObject-\u003etoJson();\n```\n\n\u003ca name=\"collections\"\u003e\u003c/a\u003e\n## Collections\n\nIn 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\n\n```php\n$collection = new BooksCollection([\n    $bookOne,\n    $bookTwo,\n    $bookThree\n]);\n\n$collection-\u003etoArray();\n```\n\n#### Auto completion for collections\nBy overriding the `current()` method and setting the return value, you can enable type hinting.\n\n```php\nclass BooksCollection extends DataTransferObjectCollection\n{\n    public function current(): BookData\n    {\n        return parent::current();\n    }\n}\n```\n\n```php\nforeach ($booksCollection as $bookData) {\n    $bookData-\u003e // type hinting \n}\n```\n\n\u003ca name=\"helpers\"\u003e\u003c/a\u003e\n## Helpers\n\nThere are a few helper methods to provide some extra functionality\n\n#### only()\n```php\n$dataObject\n    -\u003eonly('title')\n    -\u003etoArray();\n```\n\n#### except()\n```php\n$dataObject\n    -\u003eexcept('title')\n    -\u003etoArray();\n```\n\n##### Immutability\nThese helpers are immutable, which means they wont affect the original data transfer object.\n\n\u003ca name=\"example\"\u003e\u003c/a\u003e\n## Example\n\nUsing the data objects is made as simple as possible\n```php\n$book = new BookData();\n$book-\u003esetTitle('Harry Potter: The Goblet of Fire');\n\n$author = new Author();\n// ....\n\n$book-\u003esetAuthor($author);\n```\n\n## Security\nIf you discover any security related issues, please use the issue tracker.\n\n## Testing\n```bash\ncomposer test\n```\n\n## Credits\n- This package is based on the [data-transfer-object package](https://github.com/spatie/data-transfer-object) from [Spatie](https://github.com/spatie).  \n- `Arr` class contains functions copied from [Laravel's](https://github.com/laravel) Arr helper.\n\n[ico-version]: https://img.shields.io/github/release/imbue/data-transfer-object.svg?style=flat-square\n[ico-packagist]: https://img.shields.io/packagist/v/imbue/data-transfer-object.svg?style=flat-square\n[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/imbue/data-transfer-object.svg?style=flat-square\n\n[link-version]: https://github.com/imbue/data-transfer-object/releases\n[link-packagist]: https://packagist.org/packages/imbue/data-transfer-object\n[link-downloads]: https://packagist.org/packages/imbue/data-transfer-object\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbue%2Fdata-transfer-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimbue%2Fdata-transfer-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbue%2Fdata-transfer-object/lists"}