Download the PHP package romanzipp/laravel-dto without Composer
On this page you can find all versions of the php package romanzipp/laravel-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download romanzipp/laravel-dto
More information about romanzipp/laravel-dto
Files in romanzipp/laravel-dto
Package laravel-dto
Short Description A strongly typed Data Transfer Object integration for Laravel
License MIT
Informations about the package laravel-dto
Laravel DTO
A strongly typed Data Transfer Object for Laravel without magic for PHP 8.0+
This package extends the functionality of romanzipp/DTO to provide more narrow usecases for Laravel applications.
Laravel-DTO serves as an intermediate and reusable layer between request input & validation and model attribute population.
Contents
- Installation
- Usage
- Validation
- Hydrate models
- Combined usage
- Validate arrays
- Type casting: Arrays to DTOs
- Type casting
- IDE Support
- Testing
Installation
Usage
All data objects must extend the romanzipp\LaravelDTO\AbstractModelData
class.
Validation
When attaching the available validation rules and even built-in rules instances.
This will throw a Illuminate\Validation\ValidationException
if any rule does not pass.
Hydrate Models
You can attach a model to any DTO using the #[ForModel(Model::class)]
attribute.
To associate DTO properties with Model attributes, you need to attach the #[ModelAttribute()]
attribute to each property.
If no parameter is passed to the #[ModelAttribute]
attribute, DTO uses the property name itself.
Attributes saved in Person
model
name |
current_age |
---|---|
John Doe | 25 |
Note: You can also pass an existing model to the toModel()
method.
Note: When passing no existing model to the toModel()
method, default values declared in the DTO will be populated. If a model is passed as argument toModel($model)
default values will not override existing model attributes.
Populate DTO from request input data
When attaching the #[RequestAttribute]
attribute, DTO uses the property name itself.
The controller
Request input data
The PersonData
DTO instance
Combined usage
Of course all those attributes start to make sense if used together. You can attach all attributes separately of make use of the #[ValidationRule]
attributes.
Both properties in the following example behave exactly the same. Use as you prefer.
Request input data
The controller
Validate arrays
If you only want to validate an array without casting the children items to another DTO, you can make use of the ValidationChildrenRule
attribute.
The first parameter to the ValidationChildrenRule
attribute is the validation rule for the children items. The second parameter is the validator path to access the children key to validate.
Validate a simple array with numeric indexes
Validate associative arrays with named keys
Multiple validation rules
Cast arrays to DTOs (Nested data)
In some cases you also want to create realted models with a single HTTP call. In this case you can make use of the #[NestedModelData(NestedData::class)]
which will populate the DTO property with n instances of the defined DTO.
Note that we will not attach an #[ModelAttribute]
attribute to the $address
DTO property since it should not be set to a model attribute.
All attributes attached to the nested DTO will just work as expected.
Request input data
The controller
Type Casting
Type casts will convert any given value to a specified type.
Built-in type casts
CastToDate
The #[CastToDate]
attribute will respect your customly defined date class from Date::use(...)
.
You can also specify a custom date class to be used by passing the date class name as single argument #[CastToDate(MyDateClass::class)]
.
Custom type casts
You can declare custom type cast attributes by simply implementing the CastInterface
interface and attaching an attribute.
IDE Support
Make sure to add a @method
PHPDoc comment like shown below to allow IDE and static analyzer support when calling the toModel()
method.