• Stars
    star
    2
  • Language
    PHP
  • License
    MIT License
  • Created over 3 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Hydrate Data Object for PHP

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 [email protected] instead of using the issue tracker.

Credits

License

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