eag / easy-hydrator
使用 PHP 8.0 和构造函数注入轻松将数组转换为对象
1.1.0
2023-12-31 16:44 UTC
Requires
- php: >=8.0
- nette/utils: ^3.2|^4.0
- phpstan/phpdoc-parser: ^0.5|^1.2
- symfony/config: ^6.4|^7.0
- symfony/dependency-injection: ^6.4|^7.0
- symfony/framework-bundle: ^6.4|^7.0
- symfony/http-kernel: ^6.4|^7.0
- symfony/string: ^6.4|^7.0
Requires (Dev)
- phpunit/phpunit: ^10.0
README
- 简单!
- PHP 8.0 支持
- 构造函数注入支持
- 自动解析
DateTimeInterface
字符串值 - 根据参数类型声明自动重新类型
- 支持嵌套对象
- 可自定义对象创建
- 缓存
安装
composer require eag/easy-hydrator
添加到 config/bundles.php
return [ EAG\EasyHydrator\EasyHydratorBundle::class => [ 'all' => true, ], ];
使用方法
具有构造函数注入的值对象
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; } }
使用类似以下方式的 hydrator
namespace App\Repository; use App\ValueObject\Person; use EAG\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); // ... } }
多个值对象?
这是如何填充一个项的
$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);
但是,我们如何填充多个项呢?
$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);
可选值
如果对象具有可选参数,并且数据中未提供某些值,则在填充对象中使用默认值。
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();
缺少构造函数数据
如果没有提供所需构造函数参数的数据,则抛出 EAG\EasyHydrator\Exception\MissingDataException
异常。
报告问题
如果您遇到错误或想要请求新功能,请访问 Symplify monorepo issue tracker
贡献
此包的源代码包含在 Symplify monorepo 中。我们欢迎在 symplify/symplify 上为此包做出贡献。