{"id":13548945,"url":"https://github.com/spatie/data-transfer-object","last_synced_at":"2026-01-15T04:02:27.479Z","repository":{"id":38237971,"uuid":"153632216","full_name":"spatie/data-transfer-object","owner":"spatie","description":"Data transfer objects with batteries included","archived":true,"fork":false,"pushed_at":"2022-11-01T08:48:41.000Z","size":453,"stargazers_count":2235,"open_issues_count":0,"forks_count":188,"subscribers_count":37,"default_branch":"main","last_synced_at":"2026-01-14T14:47:18.833Z","etag":null,"topics":["objects","php","value"],"latest_commit_sha":null,"homepage":"https://spatie.be/open-source","language":"PHP","has_issues":false,"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/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2018-10-18T13:54:50.000Z","updated_at":"2026-01-14T09:33:30.000Z","dependencies_parsed_at":"2022-07-12T01:31:04.906Z","dependency_job_id":null,"html_url":"https://github.com/spatie/data-transfer-object","commit_stats":null,"previous_names":[],"tags_count":68,"template":false,"template_full_name":null,"purl":"pkg:github/spatie/data-transfer-object","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdata-transfer-object","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdata-transfer-object/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdata-transfer-object/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdata-transfer-object/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/data-transfer-object/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Fdata-transfer-object/sbom","scorecard":{"id":840559,"data":{"date":"2025-08-11","repo":{"name":"github.com/spatie/data-transfer-object","commit":"22e55c26dfa2a3fca713e492e3bde3307eb7f296"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3.6,"checks":[{"name":"Code-Review","score":2,"reason":"Found 4/19 approved changesets -- score normalized to 2","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Maintained","score":0,"reason":"project is archived","details":["Warn: Repository is archived."],"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/style.yml:1","Warn: no topLevel permission defined: .github/workflows/test.yml:1","Warn: no topLevel permission defined: .github/workflows/update-changelog.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/style.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/style.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/style.yml:16: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/style.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/style.yml:21: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/style.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/test.yml:19: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/test.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/test.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/test.yml/main?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/update-changelog.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/update-changelog.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/update-changelog.yml:18: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/update-changelog.yml/main?enable=pin","Warn: third-party GitHubAction not pinned by hash: .github/workflows/update-changelog.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/spatie/data-transfer-object/update-changelog.yml/main?enable=pin","Info:   0 out of   3 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   5 third-party GitHubAction dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE.md:0","Info: FSF or OSI recognized license: MIT License: LICENSE.md:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'main'","Warn: branch protection not enabled for branch 'v1'","Warn: branch protection not enabled for branch 'v2'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 16 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-23T20:23:30.255Z","repository_id":38237971,"created_at":"2025-08-23T20:23:30.255Z","updated_at":"2025-08-23T20:23:30.255Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28442357,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-15T00:55:22.719Z","status":"online","status_checked_at":"2026-01-15T02:00:08.019Z","response_time":62,"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":["objects","php","value"],"created_at":"2024-08-01T12:01:16.347Z","updated_at":"2026-01-15T04:02:27.452Z","avatar_url":"https://github.com/spatie.png","language":"PHP","readme":"\u003e **Warning**\n\u003e We [have decided](https://stitcher.io/blog/deprecating-spatie-dto) to stop maintaining this package.\n\u003e \n\u003e Consider migrating to [spatie/laravel-data](https://spatie.be/docs/laravel-data) or [cuyz/valinor](https://github.com/cuyz/valinor).\n\u003e\n\u003e Feel free to fork our code and adapt it to your needs.\n\n# Data transfer objects with batteries included\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/data-transfer-object.svg?style=flat-square)](https://packagist.org/packages/spatie/data-transfer-object)\n![Test](https://github.com/spatie/data-transfer-object/workflows/Test/badge.svg)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/data-transfer-object.svg?style=flat-square)](https://packagist.org/packages/spatie/data-transfer-object)\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/data-transfer-object\n```\n\n* **Note**: v3 of this package only supports `php:^8.0`. If you're looking for the older version, check out [v2](https://github.com/spatie/data-transfer-object/tree/v2).\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/data-transfer-object.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/data-transfer-object)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Usage\n\nThe goal of this package is to make constructing objects from arrays of (serialized) data as easy as possible. Here's what a DTO looks like:\n\n```php\nuse Spatie\\DataTransferObject\\Attributes\\MapFrom;\nuse Spatie\\DataTransferObject\\DataTransferObject;\n\nclass MyDTO extends DataTransferObject\n{\n    public OtherDTO $otherDTO;\n    \n    public OtherDTOCollection $collection;\n    \n    #[CastWith(ComplexObjectCaster::class)]\n    public ComplexObject $complexObject;\n    \n    public ComplexObjectWithCast $complexObjectWithCast;\n    \n    #[NumberBetween(1, 100)]\n    public int $a;\n    \n    #[MapFrom('address.city')]\n    public string $city;\n}\n```\n\nYou could construct this DTO like so:\n\n```php\n$dto = new MyDTO(\n    a: 5,\n    collection: [\n        ['id' =\u003e 1],\n        ['id' =\u003e 2],\n        ['id' =\u003e 3],\n    ],\n    complexObject: [\n        'name' =\u003e 'test',\n    ],\n    complexObjectWithCast: [\n        'name' =\u003e 'test',\n    ],\n    otherDTO: ['id' =\u003e 5],\n);\n```\n\nLet's discuss all possibilities one by one.\n\n## Named arguments\n\nConstructing a DTO can be done with named arguments. It's also possible to still use the old array notation. This example is equivalent to the one above.\n\n```php\n$dto = new MyDTO([\n    'a' =\u003e 5,\n    'collection' =\u003e [\n        ['id' =\u003e 1],\n        ['id' =\u003e 2],\n        ['id' =\u003e 3],\n    ],\n    'complexObject' =\u003e [\n        'name' =\u003e 'test',\n    ],\n    'complexObjectWithCast' =\u003e [\n        'name' =\u003e 'test',\n    ],\n    'otherDTO' =\u003e ['id' =\u003e 5],\n]);\n```\n\n## Value casts\n\nIf a DTO has a property that is another DTO or a DTO collection, the package will take care of automatically casting arrays of data to those DTOs:\n\n```php\n$dto = new MyDTO(\n    collection: [ // This will become an object of class OtherDTOCollection\n        ['id' =\u003e 1],\n        ['id' =\u003e 2], // Each item will be an instance of OtherDTO\n        ['id' =\u003e 3],\n    ],\n    otherDTO: ['id' =\u003e 5], // This data will be cast to OtherDTO\n);\n```\n\n### Custom casters\n\nYou can build your own caster classes, which will take whatever input they are given, and will cast that input to the desired result.\n\nTake a look at the `ComplexObject`:\n\n```php\nclass ComplexObject\n{\n    public string $name;\n}\n```\n\nAnd its caster `ComplexObjectCaster`:\n\n```php\nuse Spatie\\DataTransferObject\\Caster;\n\nclass ComplexObjectCaster implements Caster\n{\n    /**\n     * @param array|mixed $value\n     *\n     * @return mixed\n     */\n    public function cast(mixed $value): ComplexObject\n    {\n        return new ComplexObject(\n            name: $value['name']\n        );\n    }\n}\n```\n\n### Class-specific casters\n\nInstead of specifying which caster should be used for each property, you can also define that caster on the target class itself:\n\n```php\nclass MyDTO extends DataTransferObject\n{\n    public ComplexObjectWithCast $complexObjectWithCast;\n}\n```\n\n```php\n#[CastWith(ComplexObjectWithCastCaster::class)]\nclass ComplexObjectWithCast\n{\n    public string $name;\n}\n```\n\n### Default casters\n\nIt's possible to define default casters on a DTO class itself. These casters will be used whenever a property with a given type is encountered within the DTO class.\n\n```php\n#[\n    DefaultCast(DateTimeImmutable::class, DateTimeImmutableCaster::class),\n    DefaultCast(MyEnum::class, EnumCaster::class),\n]\nabstract class BaseDataTransferObject extends DataTransferObject\n{\n    public MyEnum $status; // EnumCaster will be used\n    \n    public DateTimeImmutable $date; // DateTimeImmutableCaster will be used\n}\n```\n\n### Using custom caster arguments\n\nAny caster can be passed custom arguments, the built-in [`ArrayCaster` implementation](https://github.com/spatie/data-transfer-object/blob/master/src/Casters/ArrayCaster.php) is a good example of how this may be used.\n\nUsing named arguments when passing input to your caster will help make your code more clear, but they are not required.\n\nFor example:\n\n```php\n    /** @var \\Spatie\\DataTransferObject\\Tests\\Foo[] */\n    #[CastWith(ArrayCaster::class, itemType: Foo::class)]\n    public array $collectionWithNamedArguments;\n    \n    /** @var \\Spatie\\DataTransferObject\\Tests\\Foo[] */\n    #[CastWith(ArrayCaster::class, Foo::class)]\n    public array $collectionWithoutNamedArguments;\n```\n\nNote that the first argument passed to the caster constructor is always the array with type(s) of the value being casted.\nAll other arguments will be the ones passed as extra arguments in the `CastWith` attribute.\n\n## Validation\n\nThis package doesn't offer any specific validation functionality, but it does give you a way to build your own validation attributes. For example, `NumberBetween` is a user-implemented validation attribute:\n\n```php\nclass MyDTO extends DataTransferObject\n{\n    #[NumberBetween(1, 100)]\n    public int $a;\n}\n```\n\nIt works like this under the hood:\n\n```php\n#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]\nclass NumberBetween implements Validator\n{\n    public function __construct(\n        private int $min,\n        private int $max\n    ) {\n    }\n\n    public function validate(mixed $value): ValidationResult\n    {\n        if ($value \u003c $this-\u003emin) {\n            return ValidationResult::invalid(\"Value should be greater than or equal to {$this-\u003emin}\");\n        }\n\n        if ($value \u003e $this-\u003emax) {\n            return ValidationResult::invalid(\"Value should be less than or equal to {$this-\u003emax}\");\n        }\n\n        return ValidationResult::valid();\n    }\n}\n```\n\n## Mapping\n\nYou can map a DTO property from a source property with a different name using the `#[MapFrom]` attribute.\n\nIt works with a \"dot\" notation property name or an index.\n\n```php\nclass PostDTO extends DataTransferObject\n{\n    #[MapFrom('postTitle')]\n    public string $title;\n    \n    #[MapFrom('user.name')]\n    public string $author;\n}\n\n$dto = new PostDTO([\n    'postTitle' =\u003e 'Hello world',\n    'user' =\u003e [\n        'name' =\u003e 'John Doe'\n    ]\n]);\n```\n\n```php\nclass UserDTO extends DataTransferObject\n{\n\n    #[MapFrom(0)]\n    public string $firstName;\n    \n    #[MapFrom(1)]\n    public string $lastName;\n}\n\n$dto = new UserDTO(['John', 'Doe']);\n```\n\nSometimes you also want to map them during the transformation to Array. \nA typical usecase would be transformation from camel case to snake case. \nFor that you can use the `#[MapTo]` attribute.\n\n```php\nclass UserDTO extends DataTransferObject\n{\n\n    #[MapFrom(0)]\n    #[MapTo('first_name')]\n    public string $firstName;\n    \n    #[MapFrom(1)]\n    #[MapTo('last_name')]\n    public string $lastName;\n}\n\n$dto = new UserDTO(['John', 'Doe']);\n$dto-\u003etoArray() // ['first_name' =\u003e 'John', 'last_name'=\u003e 'Doe'];\n$dto-\u003eonly('first_name')-\u003etoArray() // ['first_name' =\u003e 'John'];\n```\n\n## Strict DTOs\n\nThe previous version of this package added the `FlexibleDataTransferObject` class which allowed you to ignore properties that didn't exist on the DTO. This behaviour has been changed, all DTOs are flexible now by default, but you can make them strict by using the `#[Strict]` attribute:\n\n\n```php\nclass NonStrictDto extends DataTransferObject\n{\n    public string $name;\n}\n\n// This works\nnew NonStrictDto(\n    name: 'name',\n    unknown: 'unknown'\n);\n```\n\n```php\nuse \\Spatie\\DataTransferObject\\Attributes\\Strict;\n\n#[Strict]\nclass StrictDto extends DataTransferObject\n{\n    public string $name;\n}\n\n// This throws a \\Spatie\\DataTransferObject\\Exceptions\\UnknownProperties exception\nnew StrictDto(\n    name: 'name',\n    unknown: 'unknown'\n);\n```\n\n## Helper functions\n\nThere are also some helper functions provided for working with multiple properties at once.\n\n```php\n$postData-\u003eall();\n\n$postData\n    -\u003eonly('title', 'body')\n    -\u003etoArray();\n    \n$postData\n    -\u003eexcept('author')\n    -\u003etoArray();\n```\n\nNote that `all()` will simply return all properties, while `toArray()` will cast nested DTOs to arrays as well. \n\nYou can chain the `except()` and `only()` methods:\n\n```php\n$postData\n    -\u003eexcept('title')\n    -\u003eexcept('body')\n    -\u003etoArray();\n```\n\nIt's important to note that `except()` and `only()` are immutable, they won't change the original data transfer object.\n\n## Immutable DTOs and cloning\n\nThis package doesn't force immutable objects since PHP doesn't support them, but you're always encouraged to keep your DTOs immutable. To help you, there's a `clone` method on every DTO which accepts data to override:\n\n```php\n$clone = $original-\u003eclone(other: ['name' =\u003e 'a']);\n```\n\nNote that no data in `$original` is changed.\n\n## Collections of DTOs\n\nThis version removes the `DataTransferObjectCollection` class. Instead you can use simple casters and your own collection classes.\n\nHere's an example of casting a collection of DTOs to an array of DTOs:\n\n```php\nclass Bar extends DataTransferObject\n{\n    /** @var \\Spatie\\DataTransferObject\\Tests\\Foo[] */\n    #[CastWith(FooArrayCaster::class)]\n    public array $collectionOfFoo;\n}\n\nclass Foo extends DataTransferObject\n{\n    public string $name;\n}\n```\n\n```php\nclass FooArrayCaster implements Caster\n{\n    public function cast(mixed $value): array\n    {\n        if (! is_array($value)) {\n            throw new Exception(\"Can only cast arrays to Foo\");\n        }\n\n        return array_map(\n            fn (array $data) =\u003e new Foo(...$data),\n            $value\n        );\n    }\n}\n```\n\nIf you don't want the redundant typehint, or want extended collection functionality; you could create your own collection classes using any collection implementation. In this example, we use Laravel's:\n\n```php\nclass Bar extends DataTransferObject\n{\n    #[CastWith(FooCollectionCaster::class)]\n    public CollectionOfFoo $collectionOfFoo;\n}\n\nclass Foo extends DataTransferObject\n{\n    public string $name;\n}\n```\n\n```php\nuse Illuminate\\Support\\Collection;\n\nclass CollectionOfFoo extends Collection\n{\n    // Add the correct return type here for static analyzers to know which type of array this is \n    public function offsetGet($key): Foo\n    {\n        return parent::offsetGet($key);\n    }\n}\n```\n\n```php\nclass FooCollectionCaster implements Caster\n{\n    public function cast(mixed $value): CollectionOfFoo\n    {\n        return new CollectionOfFoo(array_map(\n            fn (array $data) =\u003e new Foo(...$data),\n            $value\n        ));\n    }\n}\n```\n\n## Simple arrays of DTOs\n\nFor a simple array of DTOs, or an object that implements PHP's built-in `ArrayAccess`, consider using the `ArrayCaster` which requires an item type to be provided:\n\n```php\nclass Bar extends DataTransferObject\n{\n    /** @var \\Spatie\\DataTransferObject\\Tests\\Foo[] */\n    #[CastWith(ArrayCaster::class, itemType: Foo::class)]\n    public array $collectionOfFoo;\n}\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](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n### Security\n\nIf you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker.\n\n## Postcardware\n\nYou're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## External tools\n\n- [json2dto](https://json2dto.atymic.dev): a GUI to convert JSON objects to DTO classes (with nesting support). Also provides a [CLI tool](https://github.com/atymic/json2dto#cli-tool) for local usage.\n- [Data Transfer Object Factory](https://github.com/anteris-dev/data-transfer-object-factory): Intelligently generates a DTO instance using the correct content for your properties based on its name and type.\n\n## Credits\n\n- [Brent Roose](https://github.com/brendt)\n- [All Contributors](../../contributors)\n\nOur `Arr` class contains functions copied from Laravels `Arr` helper.\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["Data Processing","PHP","Repositories","Packages"],"sub_categories":["Tokenizers \u0026 Prompt Utilities"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fdata-transfer-object","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Fdata-transfer-object","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Fdata-transfer-object/lists"}