Skip to content

gravataLonga/hydrator

Repository files navigation

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.

About

Hydrate Data Object for PHP

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published

Languages