gravatalonga/hydrator

Hydrator class from array to object or objecto to array

Fund package maintenance!
gravataLonga

1.0.3 2021-04-26 18:51 UTC

This package is auto-updated.

Last update: 2024-11-08 01:23:40 UTC


README

Hydrator

Hydrator

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

Hydrator is a library that can help you populate an Data Object properties from array.

Structure

build/
src/
examples/
tests/
vendor/

Install

Via Composer

$ composer require gravataLonga/hydrator

Usage

$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

Change log

Please see CHANGELOG for more information on what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please email jonathan.alexey16@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.