symplify / easy-hydrator
Hydrate arrays to objects easily with PHP 7.4 and constructor injection
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 81 400
Dependents: 1
Suggesters: 0
Security: 0
Stars: 31
Watchers: 4
Forks: 0
Type:symfony-bundle
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
- dev-main / 9.5.x-dev
- 9.4.70
- 9.4.69
- 9.4.68
- 9.4.67
- 9.4.66
- 9.4.65
- 9.4.64
- 9.4.63
- 9.4.62
- 9.4.61
- 9.4.60
- 9.4.59
- 9.4.58
- 9.4.57
- 9.4.56
- 9.4.55
- 9.4.54
- 9.4.53
- 9.4.52
- 9.4.51
- 9.4.50
- 9.4.49
- 9.4.48
- 9.4.47
- 9.4.46
- 9.4.45
- 9.4.44
- 9.4.43
- 9.4.42
- 9.4.41
- 9.4.40
- 9.4.39
- 9.4.38
- 9.4.37
- 9.4.36
- 9.4.35
- 9.4.34
- 9.4.33
- 9.4.32
- 9.4.31
- 9.4.30
- 9.4.29
- 9.4.28
- 9.4.27
- 9.4.26
- 9.4.25
- 9.4.24
- 9.4.23
- 9.4.22
- 9.4.21
- 9.4.20
- 9.4.19
- 9.4.18
- 9.4.17
- 9.4.16
- 9.4.15
- 9.4.14
- 9.4.13
- 9.4.12
- 9.4.11
- 9.4.10
- 9.4.9
- 9.4.8
- 9.4.7
- 9.4.6
- 9.4.5
- 9.4.4
- 9.4.3
- 9.4.2
- v9.4.1
- v9.4.0
- v9.3.27
- v9.3.26
- v9.3.25
- v9.3.24
- v9.3.23
- v9.3.22
- v9.3.21
- v9.3.20
- v9.3.19
- v9.3.18
- v9.3.17
- v9.3.16
- v9.3.15
- v9.3.14
- v9.3.13
- v9.3.12
- v9.3.11
- v9.3.10
- v9.3.8
- v9.3.6
- v9.3.5
- v9.3.4
- v9.3.3
- v9.3.1
- v9.3.0
- v9.2.24
- v9.2.23
- v9.2.22
- v9.2.21
- v9.2.20
- v9.2.19
- v9.2.18
- v9.2.17
- v9.2.16
- v9.2.15
- v9.2.14
- v9.2.13
- v9.2.12
- v9.2.11
- v9.2.10
- v9.2.9
- v9.2.8
- v9.2.7
- v9.2.6
- v9.2.5
- v9.2.4
- v9.2.3
- v9.2.2
- 9.2.1
- 9.2.0
- 9.1.9
- 9.1.8
- 9.1.7
- 9.1.6
- 9.1.5
- 9.1.4
- 9.1.3
- 9.1.1
- 9.1.0
- 9.0.50
- 9.0.49
- 9.0.48
- 9.0.47
- 9.0.46
- 9.0.45
- 9.0.44
- 9.0.43
- 9.0.42
- 9.0.41
- 9.0.40
- 9.0.39
- 9.0.38
- 9.0.37
- 9.0.36
- 9.0.35
- 9.0.34
- 9.0.33
- 9.0.32
- 9.0.31
- 9.0.30
- 9.0.29
- 9.0.28
- 9.0.27
- 9.0.26
- 9.0.25
- 9.0.24
- 9.0.23
- 9.0.22
- 9.0.21
- 9.0.20
- 9.0.19
- 9.0.18
- 9.0.17
- 9.0.16
- 9.0.15
- 9.0.14
- 9.0.13
- 9.0.12
- 9.0.11
- 9.0.10
- 9.0.9
- 9.0.8
- 9.0.7
- 9.0.6
- 9.0.5
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 9.0.0-rc1
- 9.0.0-BETA9
- 9.0.0-BETA8
- 9.0.0-BETA7
- 9.0.0-BETA6
- 9.0.0-BETA5
- 9.0.0-BETA4
- 9.0.0-BETA3
- 9.0.0-BETA2
- 9.0.0-BETA1
- 8.3.48
- 8.3.47
- 8.3.46
- 8.3.45
- 8.3.44
- 8.3.43
- 8.3.42
- 8.3.41
- 8.3.40
- 8.3.39
- 8.3.38
- 8.3.37
- 8.3.36
- 8.3.35
- 8.3.34
- 8.3.33
- 8.3.32
- 8.3.31
- 8.3.30
- 8.3.29
- 8.3.28
- 8.3.27
- 8.3.26
- 8.3.25
- 8.3.24
- 8.3.23
- 8.3.22
- 8.3.21
- 8.3.20
- 8.3.19
- 8.3.18
- 8.3.17
- 8.3.16
- 8.3.15
- 8.3.14
- 8.3.13
- 8.3.12
- 8.3.11
- 8.3.10
- 8.3.7
- 8.3.6
- 8.3.5
- 8.3.4
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.27
- 8.2.26
- 8.2.25
- 8.2.24
- 8.2.22
- 8.2.21
- 8.2.20
- 8.2.19
- 8.2.18
- 8.2.17
- v8.2.16
- v8.2.15
- v8.2.14
- v8.2.13
- v8.2.12
- v8.2.11
- v8.2.10
- v8.2.9
- v8.2.8
- v8.2.7
- v8.2.6
- v8.2.5
- v8.2.4
- v8.2.3
- v8.2.2
- v8.2.1
- v8.2.0
- v8.1.20
- v8.1.19
- v8.1.18
- v8.1.17
- v8.1.16
- v8.1.15
- v8.1.14
- v8.1.13
- v8.1.12
- v8.1.11
- v8.1.10
- v8.1.9
- v8.1.8
- v8.1.7
- v8.1.6
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
- v8.0.1
- v8.0.0
- v8.0.0-beta4
- v8.0.0-beta3
- v8.0.0-beta2
- v8.0.0-beta1
- v7.3.18
- v7.3.17
- v7.3.16
- v7.3.15
- v7.3.14
- v7.3.13
- v7.3.11
- v7.3.10
- v7.3.9
- v7.3.8
- v7.3.7
- v7.3.6
- v7.3.5
- v7.3.4
- v7.3.3
- v7.3.2
- v7.3.1
- v7.3.0
- v7.2.20
- v7.2.19
- v7.2.18
- v7.2.17
- v7.2.16
- v7.2.15
- v7.2.14
- v7.2.13
- v7.2.12
- v7.2.11
- v7.2.10
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
DateTimeInterface
string 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.