Download the PHP package rjds/php-dto without Composer
On this page you can find all versions of the php package rjds/php-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package php-dto
Short Description A PHP library to map associative arrays to typed DTOs using attributes.
License MIT
Informations about the package php-dto
PHP DTO
A PHP library to map associative arrays to typed DTOs using attributes.
Installation
Install from Packagist:
Requirements:
- PHP 8.1 or higher
Overview
DtoMapper converts associative arrays to typed constructor-based DTOs.
Features
- Zero-boilerplate name-based mapping for constructor arguments
#[MapFrom]support for renamed and nested keys using dot notation#[CastTo]support forint,float,string,bool, anddatetime#[ArrayOf]support for mapping nested DTO collections- Constructor default values respected when source keys are missing
MappingExceptionwith structured context (DTO class, parameter, map key, array index, nestedArrayOfparent) for easier debugging- Optional PHPStan extension to narrow
DtoMapper::map()return types
Errors
Mapping failures throw Rjds\PhpDto\Exception\MappingException (a subclass of InvalidArgumentException). Use getDtoClass(), getParameterName(), getMapKey(), getArrayIndex(), and getParentDtoClass() to locate problems in large payloads. Failures while mapping an #[ArrayOf] element chain the inner exception via getPrevious() / getPreviousMappingException().
Static analysis (PHPStan)
DtoMapper::map() is documented with @template and @param class-string<T> so PHPStan can infer the concrete DTO when you pass a class constant:
For clearer results in all setups, include the bundled extension from your phpstan.neon:
That extension refines the return type when the second argument is a constant string or a class-string<SpecificDto> type.
Documentation
Extended usage and attribute details are in the GitHub Wiki. The wiki is tracked as a Git submodule at wiki/. Clone the library with git clone --recurse-submodules, or run git submodule update --init after a plain clone. To publish wiki edits, commit inside wiki/ and run git push origin master from that directory (GitHub creates the wiki Git remote when you add the first wiki page in the repository Wiki tab, if it is not there yet).
Quick Reference
#[MapFrom]
Map a parameter from a different key, including nested paths:
#[CastTo]
Cast scalar string input into strongly typed DTO fields:
#[ArrayOf]
Map list entries to nested DTO instances:
Development
Run mutation testing:
Contributing
Contributions are welcome. See CONTRIBUTING.md for branch strategy, commit conventions, and PR workflow.
License
This project is released under the MIT License. See CHANGELOG.md for release history.