Download the PHP package amorphine/data-transfer-object without Composer
On this page you can find all versions of the php package amorphine/data-transfer-object. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-transfer-object
Data transfer object for arrays
Zero-dependency library to convert associative arrays to an object model
Installation
You can install the package via composer:
Purpose
The goal of this package is to give a convenient declared way to manipulate data extracted from associative arrays. Advantages of object structure:
- Type safety (as long as PHP makes it possible)
- Statically analyze and auto completion.
- Scalar type casting
Let's look at the example of a JSON API call:
Working with this array is difficult, as we'll always have to refer to the documentation to know what's exactly in it. This package allows you to create data transfer object definitions, classes, which will represent the data in a structured way.
We did our best to keep the syntax and overhead as little as possible:
An object of PostData
can from now on be constructed like so:
Now you can use this data in a structured way:
By adding doc blocks to our properties, their values will be validated against the given type;
and a TypeError
will be thrown if the value doesn't comply with the given type.
Here are the possible ways of declaring types:
PHP 7.4 typed properties are the source of types like PHP doc is. The compatibility of declared types is upon to the developer. Feel free to use one of them, both or none at all.
Source override
By default, the array key the value is taken from is the property name. If you need to specify the key of the original array the value should be taken from you should use @source
doc comment:
Automatic casting of nested DTOs
If you've got nested DTO fields, data passed to the parent DTO will automatically be cast.
PostData
can now be constructed like so:
Automatic casting of nested array DTOs
Similarly to above, nested array DTOs will automatically be cast.
PostData
will automatically construct tags like such:
Exception handling
Beside property type validation, you can also be certain that the data transfer object in its whole is always valid.
On constructing a data transfer object, we'll validate whether all required (non-nullable) properties are set.
If not, a Amorphine\DataTransferObject\Exceptions\DataTransferObjectError
will be thrown.
Likewise, if you're trying to set non-defined properties, you'll get a DataTransferObjectError
.
Testing
Limitations
- Opcache: types and data sources are resolved through PHP comments so ensure
opcache.save_comments
equals1
License
The MIT License (MIT). Please see License File for more information.
Powered by
- Data Transfer Object by Spatie - special thanks to them for some patterns, pieces of code and Readme structure
- David Grudl (nette/di) - thanks for class import solutions based on tokens
See also
- Data Transfer Object by Spatie - mature and popular solution with rich functionality