{"id":21992430,"url":"https://github.com/soyhuce/data-transfer-object-casts","last_synced_at":"2025-03-23T03:31:55.460Z","repository":{"id":57748624,"uuid":"523676188","full_name":"Soyhuce/data-transfer-object-casts","owner":"Soyhuce","description":"Common casts for spatie/data-transfer-object","archived":false,"fork":false,"pushed_at":"2023-10-09T15:55:22.000Z","size":41,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-19T20:16:09.756Z","etag":null,"topics":[],"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/Soyhuce.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null},"funding":{"github":"soyhuce"}},"created_at":"2022-08-11T10:15:38.000Z","updated_at":"2022-08-11T10:19:16.000Z","dependencies_parsed_at":"2023-01-28T13:32:47.704Z","dependency_job_id":null,"html_url":"https://github.com/Soyhuce/data-transfer-object-casts","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":"Soyhuce/package-skeleton-laravel","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Fdata-transfer-object-casts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Fdata-transfer-object-casts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Fdata-transfer-object-casts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soyhuce%2Fdata-transfer-object-casts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Soyhuce","download_url":"https://codeload.github.com/Soyhuce/data-transfer-object-casts/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245052619,"owners_count":20553161,"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":[],"created_at":"2024-11-29T20:13:46.264Z","updated_at":"2025-03-23T03:31:55.433Z","avatar_url":"https://github.com/Soyhuce.png","language":"PHP","readme":"# Common casts for spatie/data-transfer-object\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/soyhuce/data-transfer-object-casts.svg?style=flat-square)](https://packagist.org/packages/soyhuce/data-transfer-object-casts)\n[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/soyhuce/data-transfer-object-casts/run-tests?label=tests)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3Arun-tests+branch%3Amain)\n[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/soyhuce/data-transfer-object-casts/Fix%20PHP%20code%20style%20issues?label=code%20style)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3A\"Fix+PHP+code+style+issues\"+branch%3Amain)\n[![GitHub PHPStan Action Status](https://img.shields.io/github/workflow/status/soyhuce/data-transfer-object-casts/PHPStan?label=phpstan)](https://github.com/soyhuce/data-transfer-object-casts/actions?query=workflow%3APHPStan+branch%3Amain)\n[![Total Downloads](https://img.shields.io/packagist/dt/soyhuce/data-transfer-object-casts.svg?style=flat-square)](https://packagist.org/packages/soyhuce/data-transfer-object-casts)\n\nCommon casts for spatie/data-transfer-object\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require soyhuce/data-transfer-object-casts\n```\n\n## Usage\n\n### BooleanCaster\n\nCasts the input into boolean, if applicable.\n\n```php\nuse Soyhuce\\DataTransferObjectCasts\\BooleanCaster;\nuse Spatie\\DataTransferObject\\Attributes\\DefaultCast;\nuse Spatie\\DataTransferObject\\DataTransferObject;\n\n#[DefaultCast('bool', BooleanCaster::class)]\nclass MyDTO extends DataTransferObject\n{\n    public bool $bool;\n}\n\n$dto = new MyDTO(\n    bool: 'true',\n);\n\n$dto-\u003ebool; // true\n```\n\n### CarbonImmutableCaster\n\nCast the input into a CarbonImmutable instance. If the input is not a string, it will be returned as is.\n\nBy default, the format is `'!Y-m-d H:i:s'`.\n\n```php\nuse Carbon\\CarbonImmutable;\nuse Soyhuce\\DataTransferObjectCasts\\CarbonImmutableCaster;\nuse Spatie\\DataTransferObject\\Attributes\\CastWith;\nuse Spatie\\DataTransferObject\\Attributes\\DefaultCast;\nuse Spatie\\DataTransferObject\\DataTransferObject;\n\n#[DefaultCast(CarbonImmutable::class, CarbonImmutableCaster::class)]\nclass MyDTO extends DataTransferObject\n{\n    public CarbonImmutable $dateTime;\n\n    #[CastWith(CarbonImmutableCaster::class, '!Y-m-d')]\n    public CarbonImmutable $date;\n}\n\n$dto = new MyDTO(\n    dateTime: '2022-08-11 14:44:45',\n    date: '2022-08-01',\n);\n\n$dto-\u003edateTime; // CarbonImmutable instance\n$dto-\u003edate; // CarbonImmutable instance\n```\n\n### StringEnumCaster\n\nCast the input into a backed string enum.\n\n```php\nuse Soyhuce\\DataTransferObjectCasts\\StringEnumCaster;\nuse Spatie\\DataTransferObject\\Attributes\\CastWith;\nuse Spatie\\DataTransferObject\\DataTransferObject;\n\n#[CastWith(StringEnumCaster::class)]\nenum StringEnum: string\n{\n    case ok = 'ok';\n    case nok = 'nok';\n}\n\nclass MyDTO extends DataTransferObject\n{\n    public StringEnum $stringEnum;\n}\n\n$dto = new MyDTO(\n    stringEnum: 'ok',\n);\n\n$dto-\u003estringEnum; // StringEnum::ok\n```\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Bastien Philippe](https://github.com/bastien-phi)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/soyhuce"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyhuce%2Fdata-transfer-object-casts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoyhuce%2Fdata-transfer-object-casts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoyhuce%2Fdata-transfer-object-casts/lists"}