Use DataTransferObjects to map raw array data to strongly typed objects. The boundaries of many php applications send and receive associative arrays with no type safety. Adding typed objects in key locations adds stability and can expose faulty assumptions about the shape of your data.
When dealing with data from user input, it often pays to know if a value has been defined as null or if it wasn't defined at all. DataTransferObjects make defined and undefined "a thing" for php.
DataTransferObject uses flags to change the default behaviour of your types making them useful for a variety of use cases.
Via Composer
composer require rexlabs/data-transfer-object
Define a DTO class using the phpdoc to specify the allowed types for properties.
use Rexlabs\DataTransferObject\DataTransferObject;
/**
* @property string $first_name
* @property null|string $last_name
* @property string $email
* @property null|int $age
* @property null|UserDto $parent
* @property UserDto[] $children
*/
class UserDto extends DataTransferObject
{
}
$rawData = [
'first_name' => 'James',
'last_name' => 'Kirk',
'email' => 'jim@starfleet.ufp',
50,
];
$kirk = UserDto::make($rawData);
Data transfer objects are useful in many contexts and have additional features for convenience and refactoring.
Check the guide for details.
Follow the Upgrade Guide.
Please see CHANGELOG for more information on what has changed recently.
composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email lachlan.krautz@rexsoftware.com.au instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.