AlterHydratePropertyTrait uses \oihana\reflect\traits\ReflectionTrait
Hydrates an array value into a typed object instance during property alteration.
This alteration is declared with the Alter::HYDRATE type. It is used in property
transformation pipelines to turn a raw associative array (for instance the decoded
payload of a nested document) into a real object of a given class, so that downstream
code works with strongly-typed instances instead of loose arrays:
Property::GEO => [ Alter::HYDRATE , GeoCoordinates::class ] ,
Two hydration strategies are used depending on the target class: classes extending
Thing are built directly through their constructor, while any other class is
populated through ReflectionTrait::hydrate() (reflection-based property mapping).
The input is normalized beforehand (configurable through CleanFlag), and an empty
normalized value yields null.
The $modified flag is set to true whenever the resulting value differs from the
original input.
Tags
Table of Contents
Methods
- alterHydrateProperty() : mixed
- Hydrate a property value into a specific class instance using reflection.
Methods
alterHydrateProperty()
Hydrate a property value into a specific class instance using reflection.
public
alterHydrateProperty(mixed $value[, array<string|int, mixed> $definition = [] ][, bool &$modified = false ]) : mixed
This method transforms a raw value (typically an array) into an object of the specified class. If the input value is an array, it can be normalized and then hydrated using either the Thing constructor or the ReflectionTrait::hydrate() method depending on the class type.
Behavior
- If
$valueis not an array, it is returned as-is. - If
$valueis an empty array, the method returnsnull(by default). - If
$schemarefers to a class extending Thing, the object is created directly via its constructor. - Otherwise, hydration is performed via ReflectionTrait::hydrate().
- The
$modifiedflag is set totrueif the resulting value differs from the input.
Usage Example
Property::GEO => [ Alter::HYDRATE, GeoCoordinates::class ],
Custom Normalization
You can specify a custom normalization flag as a third element in the definition:
[ Alter::HYDRATE, GeoCoordinates::class, true , CleanFlag::ALL ]
By default, the value is normalized using:
normalize() with flags CleanFlag::DEFAULT | CleanFlag::RETURN_NULL.
Parameters
- $value : mixed
-
The original value to hydrate. Can be a scalar, array, or object.
- $definition : array<string|int, mixed> = []
-
The alter definition, expected as:
[ 0 => string|null $schema, // Fully qualified class name to hydrate into 1 => bool $normalize // Whether to normalize the value before hydration (default true) 2 => int $flags // Optional CleanFlag bitmask ] - $modified : bool = false
-
Reference flag set to
trueif the resulting value differs from the original.
Tags
Return values
mixed —The hydrated value, possibly an instance of $schema, or null if empty.