Download the PHP package zero-to-prod/data-model without Composer
On this page you can find all versions of the php package zero-to-prod/data-model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zero-to-prod/data-model
More information about zero-to-prod/data-model
Files in zero-to-prod/data-model
Package data-model
Short Description Transform data into a class.
License MIT
Homepage https://github.com/zero-to-prod/data-model
Informations about the package data-model
Zerotoprod\DataModel
The Zerotoprod\DataModel
trait simplifies data handling by allowing developers to create class instances from arrays or JSON, dynamically
assigning and casting property values based on type annotations.
Whether you’re managing simple strings or complex object types, this package ensures your data models are both flexible and reliable.
Perfect for developers looking to simplify their Data Transfer Objects (DTOs);
Installation
You can install the package via Composer:
Usage
Import the Zerotoprod\DataModel\DataModel
trait in your class. Use the from()
static method to map array keys to class properties.
It is recommended to extend your own DataModel
trait with the Zerotoprod\DataModel\DataModel
trait.
Basic Example
Automatically Typecast Properties With Doc Comments
Use PHPDoc @var
annotations to link and cast properties to their classes, ensuring accurate mapping and conversion, especially with nested objects.
Inlining the fully qualified namespace is faster as a lookup does not have to be performed.
This directs the from()
method to instantiate the Address
class for $Address
when mapping data.
Automatically casted types include:
string
array
int
bool
object
float
- Classes with a
from()
method
Notes
- The
from()
method will only assign values to existing properties in the class. - If a key does not correspond to a property in the class, it will be ignored.
- Unions like this are ignored:
/** @var Address|string $Address */
. - Use the fully qualified namespace unless the class is in the same namespace:
/** @var Address $Address */
.
Suggested Traits
Zerotoprod\DataModel\FromJson
This will decode a valid json string and return the data model.
Usage
To use the Zerotoprod\DataModel\FromJson
trait in your class.
Zerotoprod\Transformable
The Transformable trait provides methods to convert an object’s properties into an array or a JSON string. This is particularly useful for serializing your data models.
Usage
To use the Zerotoprod\Transformable\Transformable
trait in your class.
All versions of data-model with dependencies
ext-json Version *