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.
Informations about the package data-model
Zerotoprod\DataModel
Contents
- Introduction
- Why Use DataModel?
- Installation
- Additional Packages
- Features
- Type-Safe
- Recursive Instantiation
- Type Casting
- Life-Cycle Hooks
- Transformations
- Required Properties
- Default Values
- Nullable Missing Values
- Remapping
- Ignoring Properties
- Examples
- Array of DataModels
- Collection of DataModels
- Laravel Validation
- How It Works
- Why It Works
- Eliminate Defensive Programming
- Increase the Static Analysis Surface
- Self-Documentation
- Showcase
- Usage
- Hydrating from Data
- Recursive Hydration
- Transformations
- Describe Attribute
- Order of Precedence
- Property-Level Cast
- Life-Cycle Hooks
pre
Hookpost
Hook- Method-Level Cast
- Union Types
- Class-Level Cast
- Required Properties
- Default Values
- Limitations
- Nullable Missing Values
- Limitations
- Re-Mapping
- Ignoring Properties
- Using the Constructor
- Examples
- Array of DataModels
- Collection of DataModels
- Laravel Validation
- Testing
Introduction
A lightweight, trait-based approach to type-safe object hydration.
Define your data resolution logic in one place. No more scattered checks, no inheritance hassles—just straightforward, type-safe PHP objects.
Why you’ll love it:
- Simplify object hydration with recursive instantiation
- Enforce type safety so your objects are always correct
- Reduce boilerplate by eliminating repetitive validation checks
- Use transformations with PHP attributes for flexible value resolution
- Stay non-invasive: just use the
DataModel
trait—no base classes or interfaces required
Installation
You can install the package via Composer:
Additional Packages
- DataModelHelper: Helpers for a
DataModel
. - DataModelFactory: A factory helper to set the value of your
DataModel
. - Transformable: Transform a
DataModel
into different types.
Why Use DataModel?
- Automated Hydration: Let the package handle mapping and casting data into your objects.
- Type Safety: PHP enforces your declared property types automatically.
- Less Boilerplate: Centralize your validation and defaults—stop scattering checks all over your code.
- Flexible Customization: Tap into transformations, re-mapping, and lifecycle hooks.
- No Overhead: Use a trait—no extending or complicated class hierarchy.
Features
- Type-Safe: Type-safety is enforced by the PHP language itself.
- Non-Invasive: Simply add the
DataModel
trait to a class. No need to extend, implement, or construct. - Recursive Instantiation: Recursively instantiate classes based on their type.
- Type Casting: Supports primitives, custom classes, enums, and more.
- post.
- Transformations: Describe how to resolve a value before instantiation.
- Required Properties: Throw an exception when a property is not set.
- Default Values: Set a default property value.
- Nullable Missing Values: Resolve a missing value as null.
- Remapping: Re-map a key to a property of a different name.
- Ignoring Properties: Skip properties as needed
How It Works
DataModel uses:
- Reflection to find property types
- PHP attributes (the
#[Describe()]
) to define transformations and rules - Recursive Instantiation for nested objects
- Hooks before and after assignment
Just call YourClass::from($data)
and let it handle the rest.
Why it Works
A DataModel removes guesswork by centralizing how values get resolved. You define resolution logic up front, then trust the rest of your code to operate with correct, typed data. Less repetition, fewer checks, more clarity.
Eliminate Defensive Programming
Traditional defensive programming forces you to layer checks everywhere:
- Verbose: sprinkled validations and type checks
- Error-prone: easy to miss something
With DataModel, a single #[Describe()] attribute declaration handles it all. This:
- Reduces boilerplate: define once, use everywhere
- Minimizes risk: fewer places to forget checks
- Improves clarity: your code focuses on logic, not defensive guardrails
Increase the Static Analysis Surface
DataModel uses native PHP type mechanics. Language servers and LLMs can:
- Understand your properties and rules
- Warn on mismatches
- Optimize code suggestions
The #[Describe] attribute is explicit, boosting readability and tooling compatibility.
Self-Documentation
DataModel bakes critical info—types, defaults, transforms—into actual PHP attributes:
- No buried docs or sidecar validations
- The properties practically document themselves
- Anyone reading the code sees clearly how data is resolved
Showcase
Projects that use DataModels:
Usage
Use the DataModel
trait in a class.
Hydrating from Data
Use the from
method to instantiate your class, passing an associative array or object.
Recursive Hydration
A DataModel
recursively instantiates classes based on their type declarations.
If a property’s type hint is a class, its value is passed to that class’s from()
method.
In this example, the address
element is automatically converted into an Address
object,
allowing direct access to its properties: $User->address->city
.
Transformations
A DataModel
provides a variety of ways to transform data before the value is assigned to a property.
The Describe
attribute provides a declarative way describe how property values are resolved.
Describe Attribute
Resolve a value by adding the Describe
attribute to a property.
The Describe
attribute can accept these arguments.
Order of Precedence
There is an order of precedence when resolving a value for a property.
- Property-level Cast
- Method-level Cast
- Union Types
- Class-level Casts
- Types that have a concrete static method
from()
. - Native Types
Property-Level Cast
The using the Describe
attribute directly on the property takes the highest precedence.
Life-Cycle Hooks
You can run methods before and after a value is resolved.
pre
Hook
You can use pre
to run a void
method before the value is resolved.
post
Hook
You can use post
to run a void
method after the value is resolved.
Method-level Cast
Use the Describe
attribute to resolve values with class methods. Methods receive $value
and $context
as parameters.
Union Types
A value passed to property with a union type is directly assigned to the property. If you wish to resolve the value in a specific way, use a class method.
Class-Level Cast
You can define how to resolve different types at the class level.
Required Properties
Enforce that certain properties are required using the Describe attribute:
Default Values
You can set a default value for a property like this:
Limitations
Note that using null
as a default will not work: #[Describe(['default' => null])]
.
Use #[Describe(['nullable' => true])]
to set a null value.
Nullable Missing Values
Set missing values to null by setting ['nullable' => true]
. This can be placed at the class or property level.
This prevents an Error when attempting to assess a property that has not been initialized.
Error: Typed property User::$age must not be accessed before initialization
Limitations
Note that using null
as a default will not work: #[Describe(['default' => null])]
.
Use #[Describe(['nullable' => true])]
to set a null value.
Re-Mapping
You can map a key to a property of a different name like this:
Ignoring Properties
You can ignore a property like this:
Using the Constructor
You can use the constructor to instantiate a DataModel like this:
Examples
Array of DataModels
This examples uses the DataModelHelper.
Collection of DataModels
This examples uses the DataModelHelper and Laravel Collections.
Laravel Validation
By leveraging the pre
life-cycle hook, you run a validator before a value is resolved.