{"id":31045758,"url":"https://github.com/horizom/dto","last_synced_at":"2025-09-14T17:31:55.886Z","repository":{"id":207914378,"uuid":"720414836","full_name":"horizom/dto","owner":"horizom","description":"Data Transfer Objects for all PHP applications.","archived":false,"fork":false,"pushed_at":"2024-03-09T23:11:16.000Z","size":33,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-31T10:48:15.566Z","etag":null,"topics":["data","dto","object","transfer"],"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/horizom.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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}},"created_at":"2023-11-18T12:18:43.000Z","updated_at":"2024-11-20T10:12:21.000Z","dependencies_parsed_at":null,"dependency_job_id":"b328e13c-64d7-49f0-9ee4-22de41f8843d","html_url":"https://github.com/horizom/dto","commit_stats":null,"previous_names":["horizom/dto"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/horizom/dto","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/horizom%2Fdto","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/horizom%2Fdto/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/horizom%2Fdto/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/horizom%2Fdto/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/horizom","download_url":"https://codeload.github.com/horizom/dto/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/horizom%2Fdto/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275139269,"owners_count":25412218,"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-09-14T02:00:10.474Z","response_time":75,"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","dto","object","transfer"],"created_at":"2025-09-14T17:31:54.802Z","updated_at":"2025-09-14T17:31:55.850Z","avatar_url":"https://github.com/horizom.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cdiv align=\"center\"\u003e\n\u003ch1\u003eHorizom DTO\u003c/h1\u003e\n\nData Transfer Objects for all PHP applications.\n\u003c/div\u003e\n\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://packagist.org/packages/horizom/dto\"\u003e\u003cimg src=\"https://poser.pugx.org/horizom/dto/d/total.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/horizom/dto\"\u003e\u003cimg src=\"https://poser.pugx.org/horizom/dto/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/horizom/dto\"\u003e\u003cimg src=\"https://poser.pugx.org/horizom/dto/license.svg\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\nData Transfer Objects (DTOs) are objects that are used to transfer data between systems. DTOs are typically used in applications to provide a simple, consistent format for transferring data between different parts of the application, such as between the user interface and the business logic.\n\n## Installation\n\n```bash\ncomposer require horizom/dto\n```\n\n## Usage\n\n### Defining DTO Properties\n\n```php\nuse Horizom\\DTO\\DTO;\n\nclass UserDTO extends DTO\n{\n    public string $name;\n\n    public string $email;\n\n    public string $password;\n}\n```\n\n### Creating DTO Instances\n\nYou can create a `DTO` instance on many ways:\n\n#### From array\n\n```php\n$dto = new UserDTO([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B'\n]);\n```\n\nYou can also use the `fromArray` static method:\n\n```php\n$dto = UserDTO::fromArray([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B'\n]);\n```\n\n#### From JSON strings\n\n```php\n$dto = UserDTO::fromJson('{\"name\": \"John Doe\", \"email\": \"john.doe@example.com\", \"password\": \"s3CreT!@1a2B\"}');\n```\n\n### Accessing DTO Data\n\nAfter you create your `DTO` instance, you can access any properties like an `object`:\n\n```php\n$dto = new UserDTO([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B'\n]);\n\n$dto-\u003ename; // 'John Doe'\n$dto-\u003eemail; // 'john.doe@example.com'\n$dto-\u003epassword; // 's3CreT!@1a2B'\n```\n\n### Casting DTO Properties\n\nYou can cast your `DTO` properties to some types:\n\n```php\nuse App\\Enums\\UserRole;\nuse Carbon\\Carbon;\nuse Horizom\\DTO\\DTO;\nuse DateTimeImmutable;\nuse Horizom\\DTO\\Casting\\ArrayCast;\nuse Horizom\\DTO\\Casting\\EnumCast;\n\nclass UserDTO extends DTO\n{\n    public string $id;\n\n    public string $name;\n\n    public string $email;\n\n    public string $password;\n\n    public Carbon $created_at;\n\n    public DateTimeImmutable $updated_at;\n\n    public array $roles;\n\n    protected function casts()\n    {\n        return [\n            'id' =\u003e 'integer',\n            'name' =\u003e 'string',\n            'email' =\u003e 'string',\n            'password' =\u003e 'string',\n            'created_at' =\u003e Carbon::class,\n            'updated_at' =\u003e DateTimeImmutable::class,\n            'admin_role' =\u003e UserRole::class,\n            'roles' =\u003e new ArrayCast(new EnumCast(UserRole::class)),\n        ];\n    }\n}\n```\n\n### Defining Default Values\n\nSometimes we can have properties that are optional and that can have default values. You can define the default values for your `DTO` properties in the `defaults` function:\n\n```php\nuse Horizom\\DTO\\DTO;\nuse Illuminate\\Support\\Str;\n\nclass UserDTO extends DTO\n{\n    // ...\n\n    protected function defaults()\n    {\n        return [\n            'username' =\u003e Str::slug($this-\u003ename),\n        ];\n    }\n}\n```\n\nWith the `DTO` definition above you could run:\n\n```php\n$dto = new UserDTO([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B'\n]);\n\n$dto-\u003eusername; // 'john_doe'\n```\n\n### Transforming DTO Data\n\nYou can convert your DTO to some formats:\n\n#### To array\n\n```php\n$dto = new UserDTO([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B',\n]);\n\n$dto-\u003etoArray();\n// [\n//     \"name\" =\u003e \"John Doe\",\n//     \"email\" =\u003e \"john.doe@example.com\",\n//     \"password\" =\u003e \"s3CreT!@1a2B\",\n// ]\n```\n\n#### To JSON string\n\n```php\n$dto = new UserDTO([\n    'name' =\u003e 'John Doe',\n    'email' =\u003e 'john.doe@example.com',\n    'password' =\u003e 's3CreT!@1a2B',\n]);\n\n$dto-\u003etoJson();\n// '{\"name\":\"John Doe\",\"email\":\"john.doe@example.com\",\"password\":\"s3CreT!@1a2B\"}'\n```\n\n### Create Your Own Type Cast\n\n#### Castable classes\n\nYou can easily create new `Castable` types for your project by implementing the `Horizom\\DTO\\Contracts\\CastableContract` interface. This interface has a single method that must be implemented:\n\n```php\npublic function cast(string $property, mixed $value): mixed;\n```\n\nLet's say that you have a `URLWrapper` class in your project, and you want that when passing a URL into your `DTO` it will always return a `URLWrapper` instance instead of a simple string:\n\n```php\nuse Horizom\\DTO\\Contracts\\CastableContract;\n\nclass URLCast implements CastableContract\n{\n    public function cast(string $property, mixed $value): URLWrapper\n    {\n        return new URLWrapper($value);\n    }\n}\n```\n\nThen you could apply this to your DTO:\n\n```php\nuse Horizom\\DTO\\DTO;\n\nclass CustomDTO extends DTO\n{\n    protected function casts()\n    {\n        return [\n            'url' =\u003e new URLCast(),\n        ];\n    }\n\n    protected function defaults()\n    {\n        return [];\n    }\n}\n```\n\n#### Callable casts\n\nYou can also create new Castable types for your project by using a callable/callback:\n\n```php\nuse Horizom\\DTO\\DTO;\n\nclass CustomDTO extends DTO\n{\n    protected function casts(): array\n    {\n        return [\n            'url' =\u003e function (string $property, mixed $value) {\n                return new URLWrapper($value);\n            },\n        ];\n    }\n\n    protected function defaults(): array\n    {\n        return [];\n    }\n}\n```\n\nOr you can use a static method:\n\n```php\nuse Horizom\\DTO\\Casting\\Cast;\nuse Horizom\\DTO\\DTO;\n\nclass CustomDTO extends DTO\n{\n    protected function casts()\n    {\n        return [\n            'url' =\u003e Cast::make(\n                function (string $property, mixed $value) {\n                    return new URLWrapper($value);\n                },\n                function (string $property, URLWrapper $value) {\n                    return $value-\u003etoString();\n                }\n            )\n        ];\n    }\n\n    protected function defaults()\n    {\n        return [];\n    }\n}\n```\n\n### Case of possibility of extending with Laravel\n\nYou can extend the `DTO` class to create your own `DTO` class with some custom methods:\n\n```php\nuse App\\Http\\Resources\\UserResource;\nuse Horizom\\DTO\\DTO;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass UserDTO extends DTO\n{\n    public int $id;\n\n    public string $name;\n\n    public string $email;\n\n    public string $password;\n\n    public Carbon $created_at;\n\n    public CarbonImmutable $updated_at;\n\n    public DateTimeImmutable $verified_at;\n\n    public static function fromModel(Model $model) {\n        return new self($model-\u003etoArray());\n    }\n\n    public function toModel() {\n        return new Model($this-\u003etoArray());\n    }\n\n    public function toResource() {\n        return new UserResource($this-\u003etoArray());\n    }\n\n    protected function casts()\n    {\n        return [\n            'id' =\u003e 'integer',\n            'name' =\u003e 'string',\n            'email' =\u003e 'string',\n            'password' =\u003e 'string',\n            'created_at' =\u003e Carbon::class,\n            'updated_at' =\u003e CarbonImmutable::class,\n            'verified_at' =\u003e DateTimeImmutable::class,\n        ];\n    }\n}\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhorizom%2Fdto","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhorizom%2Fdto","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhorizom%2Fdto/lists"}