eag/easy-hydrator
Composer 安装命令:
composer require eag/easy-hydrator
包简介
Hydrate arrays to objects easily with PHP 8.0 and constructor injection
README 文档
README
- easy!
- PHP 8.0 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 eag/easy-hydrator
Add to config/bundles.php:
return [ EAG\EasyHydrator\EasyHydratorBundle::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 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); // ... } }
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, EAG\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.
eag/easy-hydrator 适用场景与选型建议
eag/easy-hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53.93k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 eag/easy-hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eag/easy-hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 53.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-01