Download the PHP package dgame/php-dto without Composer
On this page you can find all versions of the php package dgame/php-dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dgame/php-dto
More information about dgame/php-dto
Files in dgame/php-dto
Package php-dto
Short Description A data transfer object inspired by Rust's serde
License MIT
Informations about the package php-dto
Data Transfer Object
Want to deserialize an object with data on the fly? Go for it by using the From
trait.
How is this package any different from spaties popular data-transfer-object, you may ask? Well, it's not meant to be a replacement by any means. But while using it I've often come across some things I've missed since I knew them from serde, like renaming and ignoring properties, something that spatie's data-transfer-object might not get in the near future. So there it is, my own little DTO package :) I hope it helps someone, as it helps me in my daily work. Feel free to open issues or pull requests - any help is greatly appreciated!
Requirements
This package is designed for PHP ≥ 8.0 only since it's using PHP 8.0 Attributes.
Attributes
Name
You get a parameter which is not named as the parameter in your class? #[Name(...)]
to the rescue - just specify the name from the Request:
Now the key size
will be mapped to the property $limit
- but keep in mind: the name limit
is no longer known
since you overwrote it with size
. If that is not your intention, take a look at the Alias Attribute.
Alias
You get a parameter which is not always named as the parameter in your class? #[Alias(...)]
can help you - just specify the alias from the Request:
Now the keys size
and limit
will be mapped to the property $limit
. You can mix #[Name(...)]
and #[Alias(...)]
as you want:
The keys a
and z
are mapped to the property id
- but not the key id
since you overwrote it with a
. But the following
will accept the keys a
, z
and id
.
Transformations
If you want to transform a value before it is assigned to the property, you can use Transformations. You just need to implement the Transformation interface.
Cast
Cast is currently the only built-in Transformation and let you apply a Type-Cast before the value is assigned to the property:
If not told otherwise, a simple type-cast is performed. In the example below it would just call something like $this->id = (int) $id
:
But that would be tried for any input. If you want to limit this to certain types, you can use types
:
Here the cast would only be performed if the incoming value is either an int
, string
, float
or bool
.
If you want more control, you can use a static method inside of the class:
or a function:
If a class is given but not a method
, by default __invoke
will be used:
Validation
You want to validate the value before it is assigned? We can do that. There are a few pre-defined validations prepared, but you can easily write your own by implementing the Validation
-interface.
Min
Both $offset
and $limit
must be at least have the value 0
(so they must be positive-integers). If not, an exception is thrown. You can configure the message of the exception by specifying the message
parameter:
Max
Both $offset
and $limit
may not exceed 1000
. If they do, an exception is thrown. You can configure the message of the exception by specifying the message
parameter:
Instance
Do you want to make sure that a property is an instance of a certain class or that each item in an array is an instance of that said class?
Type
If you are trying to cover objects or other class instances, you should probably take a look at Instance.
As long as you specify a type for your properties, the Type
validation is automatically added to ensure that the specified values can be assigned to the specified types. If not, a validation exception will be thrown.
Without this validation, a TypeError
would be thrown, which may not be desirable.
So this code
is actually seen as this:
The following snippets are equivalent to the snippet above:
If you want to change the exception message, you can do so using the message
parameter:
Custom
Do you want your own Validation? Just implement the Validation
-interface:
Ignore
You don't want a specific key-value to override your property? Just ignore it:
Reject
You want to go one step further than simply ignoring a value? Then Reject
it:
Required
Normally, a nullable-property or a property with a provided default value is treated with said default-value or null if the property cannot be assigned from the provided data.
If no default-value is provided and the property is not nullable, an error is thrown in case the property was not found.
But in some cases you might want to specify the reason, why the property is required or even want to require an otherwise default-able property. You can do that by using Required
:
Optional
The counterpart of Required.
If you don't want to or can't provide a default/nullable value, Optional
will assign the default value of the property-type in case of a missing value:
Of course you can specify which value should be used if no data is provided:
In case you're using Optional
together with a provided default-value, the default-value has always priority:
Numeric
You have int
or float
properties but aren't sure if those aren't delivered as e.g. string
? Numeric
to the rescue! It will translate the value to a numeric representation (to int
or float
):
Boolean
You have bool
properties but aren't sure if those aren't delivered as string
or int
? Boolean
can help you with that!
Date
You want a DateTime
but got a string? No problem:
In
Your value must be one of a specific range or enumeration? You can ensure that with In
:
NotIn
Your value must not be one of a specific range or enumeration? You can ensure that with NotIn
:
Matches
You must be sure that your values match a specific pattern? You can do that for all scalar values by using Matches
:
Trim
You have to make sure, that string
values are trimmed? No worries, we have Trim
:
Path
Did you ever wanted to extract a value from a provided array? Path
to the rescue:
It helps while with JSON's special $value
attribute
and with XML's #text
.
But we can do even more. You can choose which parts of the field are taken
and can even assign them directly to an object:
SelfValidation
In addition to the customary validations you can specify a class-wide validation after all assignments are done:
ValidationStrategy
The default validation strategy is fail-fast which means an Exception is thrown as soon as an error is detected.
But that might not desirable, so you can configure this with a ValidationStrategy
:
The example above would throw a combined exception that name
is not long enough and id
must be at least 0.
You can configure this as well by extending the ValidationStrategy
and provide a FailureHandler
and/or a FailureCollection
.
Property promotion
In the above examples, property promotion is not always used because it is more readable that way, but property promotion is supported. So the following example
can be rewritten as shown below
and it still works.
Nested object detection
You have nested objects and want to deserialize them all at once? That is a given:
Have you noticed the missing From
in Bar
? From
is just a little wrapper for the actual DTO. So your nested classes don't need to use it at all.
There is no limit to the depth of nesting, the responsibility is yours! :)
All versions of php-dto with dependencies
dgame/php-cast Version ^0.1.0
dgame/php-type Version ^1.0
thecodingmachine/safe Version ^1.3