symplify / easy-hydrator
Hydrate arrays to objects easily with PHP 7.4 and constructor injection
Package info
github.com/deprecated-packages/easy-hydrator
Type:symfony-bundle
pkg:composer/symplify/easy-hydrator
Requires
- php: >=8.0
- phpstan/phpdoc-parser: ^0.5
- symfony/cache: ^5.3|^6.0
- symfony/config: ^5.3|^6.0
- symfony/dependency-injection: ^5.3|^6.0
- symfony/http-kernel: ^5.3|^6.0
- symplify/autowire-array-parameter: ^9.4.70
- symplify/package-builder: ^9.4.70
- symplify/simple-php-doc-parser: ^9.4.70
Requires (Dev)
- phpunit/phpunit: ^9.5
Conflicts
- symplify/amnesia: <9.4.70
- symplify/astral: <9.4.70
- symplify/coding-standard: <9.4.70
- symplify/composer-json-manipulator: <9.4.70
- symplify/config-transformer: <9.4.70
- symplify/console-color-diff: <9.4.70
- symplify/console-package-builder: <9.4.70
- symplify/easy-ci: <9.4.70
- symplify/easy-coding-standard: <9.4.70
- symplify/easy-testing: <9.4.70
- symplify/git-wrapper: <9.4.70
- symplify/latte-phpstan-compiler: <9.4.70
- symplify/markdown-diff: <9.4.70
- symplify/monorepo-builder: <9.4.70
- symplify/php-config-printer: <9.4.70
- symplify/phpstan-extensions: <9.4.70
- symplify/phpstan-rules: <9.4.70
- symplify/psr4-switcher: <9.4.70
- symplify/rule-doc-generator: <9.4.70
- symplify/rule-doc-generator-contracts: <9.4.70
- symplify/skipper: <9.4.70
- symplify/smart-file-system: <9.4.70
- symplify/symfony-php-config: <9.4.70
- symplify/symfony-static-dumper: <9.4.70
- symplify/symplify-kernel: <9.4.70
- symplify/template-phpstan-compiler: <9.4.70
- symplify/twig-phpstan-compiler: <9.4.70
- symplify/vendor-patches: <9.4.70
This package is auto-updated.
Last update: 2021-10-11 12:54:29 UTC
README
- easy!
- PHP 7.4 support
- constructor injection support
- auto-resolving of
DateTimeInterfacestring value - auto-retype based on param type declarations
- nested objects support
- customizable objects creation
- cached
Install
composer require symplify/easy-hydrator
Add to config/bundles.php:
return [ Symplify\EasyHydrator\EasyHydratorBundle::class => [ 'all' => true, ], Symplify\SimplePhpDocParser\Bundle\SimplePhpDocParserBundle::class => [ 'all' => true, ], ];
Usage
Having value object with constructor injection:
namespace App\ValueObject; use DateTimeInterface; final class Person { private string $name; private int $age; private DateTimeInterface $metAt; public function __construct(string $name, int $age, DateTimeInterface $metAt) { $this->name = $name; $this->age = $age; $this->metAt = $metAt; } public function getName(): string { return $this->name; } public function getAge(): int { return $this->age; } public function getMetAt(): DateTimeInterface { return $this->metAt; } }
Use hydrator with array like this:
namespace App\Repository; use App\ValueObject\Person; use Symplify\EasyHydrator\ArrayToValueObjectHydrator; final class HumanRepository { /** * @var ArrayToValueObjectHydrator */ private $arrayToValueObjectHydrator; public function __construct(ArrayToValueObjectHydrator $arrayToValueObjectHydrator) { $this->arrayToValueObjectHydrator = $arrayToValueObjectHydrator; } public function getPerson(): Person { return $this->arrayToValueObjectHydrator->hydrateArray([ 'name' => 'Tom', // will be retyped to int 'age' => '30', // will be retyped to DateTimeInterface 'metAt' => '2020-02-02', ], Person::class); // ... } }
Multiple Value Objects?
This is how you hydrate 1 item:
$singlePersonAsArray = [ 'name' => 'Tom', // will be retyped to int 'age' => '30', // will be retyped to DateTimeInterface 'metAt' => '2020-02-02', ]); /** @var Person $person */ $person = $this->arrayToValueObjectHydrator->hydrateArray($singlePersonAsArray, Person::class);
But how can we hydrate multiple items?
$manyPersonsAsArray = []; $manyPersonsAsArray[] = [ 'name' => 'Tom', // will be retyped to int 'age' => '30', // will be retyped to DateTimeInterface 'metAt' => '2020-02-02', ]; $manyPersonsAsArray[] = [ 'name' => 'John', // will be retyped to int 'age' => '25', // will be retyped to DateTimeInterface 'metAt' => '2019-12-31', ]; /** @var Person[] $persons */ $persons = $this->arrayToValueObjectHydrator->hydrateArrays($manyPersonsAsArray, Person::class);
Optionable values
If object has optional parameters, and some of their values are not provided in data, default value is used in the hydrated object.
class MyObject { private string $foo; private string $bar; public function __construct(string $foo, string $bar = 'bar') { $this->foo = $foo; $this->bar = $bar; } public function getFoo(): string { return $this->foo; } public function getBar(): string { return $this->bar; } } $data = [ 'foo' => 'foo', ]; $object = $this->arrayToValueObjectHydrator->hydrateArray($data, MyObject::class); // bar $object->getBar();
Missing constructor data
When not provided data for required constructor parameter, Symplify\EasyHydrator\Exception\MissingDataException is thrown.
Report Issues
In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker
Contribute
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.