gravatalonga / hydrator
Hydrator class from array to object or objecto to array
Fund package maintenance!
gravataLonga
Requires
- php: ^8.0 || ^7.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.18
- infection/infection: ^0.21.5
- phly/keep-a-changelog: ^2.11
- phpstan/phpstan: ^0.12.83
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-12-08 01:29:53 UTC
README
Hydrator
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.