Hydrator is a library that can help you populate an Data Object properties from array.
build/
src/
examples/
tests/
vendor/
Via Composer
$ composer require gravataLonga/hydrator
$properties = ['id' => 1, 'string' => 'hello world']
class Entity implements HydrableInterface
{
use HydrateTrait;
public int $id;
public string $string;
}
$e = Entity::hydrate($property);
echo $e->id; // 1
echo $e->string; // hello world
You can even use internal functions to format properly value,
$properties = ['name' => 'Jonathan']
class Entity implements HydrableInterface
{
use HydrateTrait;
public string $name;
private function formatName($value)
{
return 'Hi, ' . $value;
}
}
$e = Entity::hydrate($property);
echo $e->string; // Hi, Jonathan
Note: If property is set private or protected, it will not populate that fields, and it will throw an exception.
If you can't or dislike usage of traits, you have another way, is to use: Hydrator
.
$hydrator = new Hydrator(['id' => 1]);
$entity = $hydrator->hydrate(new Entity);
echo $entity->id; // 1
Please see CHANGELOG for more information on what has changed recently.
$ composer test
Please see CONTRIBUTING and CODE_OF_CONDUCT for details.
If you discover any security related issues, please email jonathan.alexey16@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.