AlterHydratePropertyTrait uses \oihana\reflect\traits\ReflectionTrait
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.