{"id":16078524,"url":"https://github.com/romanzipp/dto","last_synced_at":"2025-03-18T05:31:33.805Z","repository":{"id":40448032,"uuid":"303936718","full_name":"romanzipp/DTO","owner":"romanzipp","description":"A strongly typed Data Transfer Object without magic for PHP 8","archived":false,"fork":false,"pushed_at":"2024-03-07T09:20:04.000Z","size":85,"stargazers_count":12,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-13T22:09:15.963Z","etag":null,"topics":["data-transfer","dto","php","php7","php8","showcase"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/romanzipp/dto","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/romanzipp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"romanzipp"}},"created_at":"2020-10-14T07:29:52.000Z","updated_at":"2025-02-11T12:29:27.000Z","dependencies_parsed_at":"2024-10-27T17:22:27.862Z","dependency_job_id":"9a484240-b81d-460f-bebf-0f824ba1543f","html_url":"https://github.com/romanzipp/DTO","commit_stats":{"total_commits":91,"total_committers":3,"mean_commits":"30.333333333333332","dds":0.03296703296703296,"last_synced_commit":"0a5fec28bf06f8ed2988007e72068f2c1fae642d"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzipp%2FDTO","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzipp%2FDTO/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzipp%2FDTO/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/romanzipp%2FDTO/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/romanzipp","download_url":"https://codeload.github.com/romanzipp/DTO/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243905999,"owners_count":20366953,"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","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","dto","php","php7","php8","showcase"],"created_at":"2024-10-09T10:13:25.032Z","updated_at":"2025-03-18T05:31:33.290Z","avatar_url":"https://github.com/romanzipp.png","language":"PHP","funding_links":["https://github.com/sponsors/romanzipp"],"categories":[],"sub_categories":[],"readme":"# DTO\n\n[![Latest Stable Version](https://img.shields.io/packagist/v/romanzipp/DTO.svg?style=flat-square)](https://packagist.org/packages/romanzipp/dto)\n[![Total Downloads](https://img.shields.io/packagist/dt/romanzipp/DTO.svg?style=flat-square)](https://packagist.org/packages/romanzipp/dto)\n[![License](https://img.shields.io/packagist/l/romanzipp/DTO.svg?style=flat-square)](https://packagist.org/packages/romanzipp/dto)\n[![GitHub Build Status](https://img.shields.io/github/workflow/status/romanzipp/DTO/Tests?style=flat-square)](https://github.com/romanzipp/DTO/actions)\n\nA strongly typed **Data Transfer Object** without magic for PHP 8.0+ . Features support for PHP 8 [union types](https://wiki.php.net/rfc/union_types_v2) and [attributes](https://wiki.php.net/rfc/attributes_v2).\n\n## Contents\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [Validation table](#validation)\n\n## Installation\n\n```\ncomposer require romanzipp/dto\n```\n\n- For **PHP 7.4** please use [`1.x`](https://github.com/romanzipp/DTO/tree/1.x)\n- For **PHP 8.0** please use [`2.x`](https://github.com/romanzipp/DTO)\n\n## Usage\n\n```php\nuse romanzipp\\DTO\\AbstractData;\nuse romanzipp\\DTO\\Attributes\\Required;\n\nclass DummyData extends AbstractData\n{\n    #[Required]\n    public string $name;\n\n    public ?string $nickname;\n\n    public string|int $height;\n\n    public DateTime $birthday;\n\n    public bool $subscribeNewsletter = false;\n}\n\n$data = new DummyData([\n    'name' =\u003e 'Roman',\n    'height' =\u003e 180,\n]);\n```\n\n### Require properties\n\nWhen declaring required properties, the DTO will validate all parameters against the declared properties. Take a look at the [validation table](#validation) for more details.\n\n```php\nuse romanzipp\\DTO\\AbstractData;\nuse romanzipp\\DTO\\Attributes\\Required;\n\nclass DummyData extends AbstractData\n{\n    #[Required]\n    public string $name;\n}\n\n$data = new DummyData([]);\n```\n\n\u003e romanzipp\\DTO\\Exceptions\\InvalidDataException: The required property \\`name\\` is missing\n\n### Array methods\n\n#### Simple array representation\n\nTo get an array representation of the DTO, simply call the `toArray` instance method.\n\nWhen transferring the DTO properties to an array format, the package will respect and call any `toArray` methods of nested DTO instances or otherwise fall back to any declared [`jsonSerialize`](https://www.php.net/manual/de/jsonserializable.jsonserialize.php) method when implementing the [`JsonSerializable`](https://www.php.net/manual/de/class.jsonserializable.php) interface.\n\n```php\nuse romanzipp\\DTO\\AbstractData;\n\nclass DummyData extends AbstractData\n{\n    public string $firstName;\n\n    public DummyData $childData;\n    \n    /** @var self[] */\n    public array $children = [];\n}\n\n$data = new DummyData([\n    'firstName' =\u003e 'Roman',\n    'childData' =\u003e new DummyData([\n        'firstName' =\u003e 'Tim',\n     ]),\n    'children' =\u003e [\n        new DummyData([\n            'firstName' =\u003e 'Tom'\n        ]),\n    ],\n]);\n\n$data-\u003etoArray();\n// [\n//    'firstName' =\u003e 'Roman',\n//    'childData' =\u003e ['firstName' =\u003e 'Tim']\n//    'children' =\u003e [\n//        ['firstName' =\u003e 'Tom']\n//    ] \n// ];\n```\n\n#### Convert keys\n\nThe `toArrayConverted` method allows the simple conversion of property keys to a given case.\n\n```php\nuse romanzipp\\DTO\\AbstractData;\nuse romanzipp\\DTO\\Cases;\n\nclass DummyData extends AbstractData\n{\n    public string $firstName;\n}\n\n$data = new DummyData([\n    'firstName' =\u003e 'Roman',\n]);\n\n$data-\u003etoArrayConverted(Cases\\CamelCase::class);  // ['firstName' =\u003e 'Roman'];\n$data-\u003etoArrayConverted(Cases\\KebabCase::class);  // ['first-name' =\u003e 'Roman'];\n$data-\u003etoArrayConverted(Cases\\PascalCase::class); // ['FirstName' =\u003e 'Roman'];\n$data-\u003etoArrayConverted(Cases\\SnakeCase::class);  // ['first_name' =\u003e 'Roman'];\n```\n\n### Flexible DTOs\n\nWhen attaching the `Flexible` attribute you can provide more parameters than declared in the DTO instance.\nAll properties will also be included in the `toArray` methods. This would otherwise throw an [`InvalidDataException`](src/Exceptions/InvalidDataException.php).\n\n```php\nuse romanzipp\\DTO\\AbstractData;\nuse romanzipp\\DTO\\Attributes\\Flexible;\n\n#[Flexible]\nclass DummyData extends AbstractData\n{\n    public string $name;\n}\n\n$data = new DummyData([\n    'name' =\u003e 'Roman',\n    'website' =\u003e 'ich.wtf',\n]);\n\n$data-\u003etoArray(); // ['name' =\u003e 'Roman', 'website' =\u003e 'ich.wtf];\n```\n\n## Validation\n\n| Definition | Required | Value | Valid | `isset()` |\n| --- | :---: | --- | :---: | :---: |\n| `public $foo` | no | `''` | ✅ | ✅ |\n| `public $foo` | no | `NULL` | ✅ | ✅ |\n| `public $foo` | no | *none* | ✅ | ✅ |\n| `public $foo` | **yes** | `''` | ✅ | ✅ |\n| `public $foo` | **yes** | `NULL` | ✅ | ✅ |\n| `public $foo` | **yes** | *none* | 🚫 | - |\n| | | | |\n| `public string $foo` | no | `''` | ✅ | ✅ |\n| `public string $foo` | no | `NULL` | 🚫 | - |\n| `public string $foo` | no | *none* | ✅ | 🚫 |\n| `public string $foo` | **yes** | `''` | ✅ | ✅ |\n| `public string $foo` | **yes** | `NULL` | 🚫 | - |\n| `public string $foo` | **yes** | *none* | 🚫 | - | \n| | | | |\n| `public ?string $foo` | no | `''` | ✅ | ✅ |\n| `public ?string $foo` | no | `NULL` | ✅ | ✅ |\n| `public ?string $foo` | no | *none* | ✅ | 🚫 |\n| `public ?string $foo` | **yes** | `''` | ✅ | ✅ |\n| `public ?string $foo` | **yes** | `NULL` | ✅ | ✅ |\n| `public ?string $foo` | **yes** | *none* | 🚫 | - |\n| | | | |\n| `public ?string $foo = null` | no | `''` | ✅ | ✅ |\n| `public ?string $foo = null` | no | `NULL` | ✅ | ✅ |\n| `public ?string $foo = null` | no | *none* | ✅ | ✅ |\n| `public ?string $foo = null` | **yes** | `''` | ⚠️* | - |\n| `public ?string $foo = null` | **yes** | `NULL` | ⚠️* | - |\n| `public ?string $foo = null` | **yes** | *none* | ⚠️* | - |\n\n\\* Attributes with default values cannot be required.\n\n## Testing\n\n```\n./vendor/bin/phpunit\n```\n\n## Credits\n\n- [Roman Zipp](https://github.com/romanzipp)\n\nThis package has been inspired by [Spaties Data-Transfer-Object](https://github.com/spatie/data-transfer-object) released under the [MIT License](https://github.com/spatie/data-transfer-object/blob/2.5.0/LICENSE.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanzipp%2Fdto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fromanzipp%2Fdto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fromanzipp%2Fdto/lists"}