Validation
Rather than implement Yet Another Validation Library, Hydration supports validation via closure. The closure takes the value being validated and the current Property as arguments. If the return value of the closure evaluates true, then the validation passes.
This facility can also be used to transform the data to conform to application requirements by passing the value by reference.
Class A implements Hydratable
{
private string $name;
public function hydrate($json, $options = []): bool
{
if (!isset(self::$hydrator)) {
self::$hydrator = Hydrator::make()
->addProperty(
Property::make('name')
->validate(function (&$value, Property $property) {
if (strlen($value) < 12) {
return false;
}
$value = strtolower($value);
return true;
})
)
->bind(self::class);
}
return self::$hydrator->hydrate($this, $json, $options);
}
}