Download the PHP package friendsofhyperf/validated-dto without Composer
On this page you can find all versions of the php package friendsofhyperf/validated-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download friendsofhyperf/validated-dto
More information about friendsofhyperf/validated-dto
Files in friendsofhyperf/validated-dto
Package validated-dto
Short Description The Data Transfer Objects with validation for Hyperf.
License MIT
Informations about the package validated-dto
Validated DTO
The Data Transfer Objects with validation for Hyperf applications. Forked from laravel-validated-dto
Documentation
https://wendell-adriel.gitbook.io/laravel-validated-dto
Installation
Generating DTO
You can create DTO
using the gen:dto
command:
The DTO
are going to be created inside app/DTO
.
Defining Validation Rules
You can validate data in the same way you validate Request
data:
Creating DTO instances
You can create a DTO
instance on many ways:
From arrays
From JSON strings
From Request objects
From Model
Beware that the fields in the $hidden
property of the Model
won't be used for the DTO
.
From Artisan Commands
You have three ways of creating a DTO
instance from an Artisan Command
:
From the Command Arguments
From the Command Options
From the Command Arguments and Options
Accessing DTO data
After you create your DTO
instance, you can access any properties like an object
:
If you pass properties that are not listed in the rules
method of your DTO
, this data will be ignored and won't be available in your DTO
:
Defining Default Values
Sometimes we can have properties that are optional and that can have default values. You can define the default values for
your DTO
properties in the defaults
function:
With the DTO
definition above you could run:
Converting DTO data
You can convert your DTO to some formats:
To array
To JSON string
To Eloquent Model
Customizing Error Messages, Attributes and Exceptions
You can define custom messages and attributes implementing the messages
and attributes
methods in your DTO
class:
Type Casting
You can easily cast your DTO properties by defining a casts method in your DTO:
Available Types
Array
For JSON strings, it will convert into an array, for other types, it will wrap them in an array.
Boolean
For string values, this uses the filter_var
function with the FILTER_VALIDATE_BOOLEAN
flag.
Carbon
This accepts any value accepted by the Carbon
constructor. If an invalid value is found it will throw a
\FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
You can also pass a timezone when defining the cast if you need that will be used when casting the value.
You can also pass a format when defining the cast to be used to cast the value. If the property has a different format than
the specified it will throw a \FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
CarbonImmutable
This accepts any value accepted by the CarbonImmutable
constructor. If an invalid value is found it will throw a
\FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
You can also pass a timezone when defining the cast if you need that will be used when casting the value.
You can also pass a format when defining the cast to be used to cast the value. If the property has a different format than
the specified it will throw a \FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
Collection
For JSON strings, it will convert into an array first, before wrapping it into a Collection
object.
If you want to cast all the elements inside the Collection
, you can pass a Castable
to the CollectionCast
constructor. Let's say that you want to convert all the items inside the Collection
into integers:
This works with all Castable
, including DTOCast
and ModelCast
for nested data.
DTO
This works with arrays and JSON strings. This will validate the data and also cast the data for the given DTO.
This will throw a Hyperf\Validation\ValidationException
exception if the data is not valid for the DTO.
This will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception if the property is not a valid
array or valid JSON string.
This will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastTargetException
exception if the class passed to the
DTOCast
constructor is not a ValidatedDTO
instance.
Float
If a not numeric value is found, it will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
Integer
If a not numeric value is found, it will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
Model
This works with arrays and JSON strings.
This will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception if the property is not a valid
array or valid JSON string.
This will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastTargetException
exception if the class passed to the
ModelCast
constructor is not a Model
instance.
Object
This works with arrays and JSON strings.
This will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception if the property is not a valid
array or valid JSON string.
String
If the data can't be converted into a string, this will throw a FriendsOfHyperf\ValidatedDTO\Exception\CastException
exception.
Create Your Own Type Cast
You can easily create new Castable
types for your project by implementing the FriendsOfHyperf\ValidatedDTO\Casting\Castable
interface. This interface has a single method that must be implemented:
Let's say that you have a URLWrapper
class in your project, and you want that when passing a URL into your
DTO
it will always return a URLWrapper
instance instead of a simple string:
Then you could apply this to your DTO:
Contact
- Gmail
License
MIT
All versions of validated-dto with dependencies
hyperf/code-parser Version ~3.1.0
hyperf/collection Version ~3.1.0
hyperf/context Version ~3.1.0
hyperf/stringable Version ~3.1.0
hyperf/validation Version ~3.1.0