milo/hydrator
Composer 安装命令:
composer require milo/hydrator
包简介
Hydrates object from array, exports object to array
README 文档
README
Usage
class Person { /** @var string */ public $name; public string $surname; public string|null $email; /** @var Address[] */ public array $addresses; } class Address { /** @var string */ public $city; } $data = [ 'name' => 'Miloslav', 'surname' => 'Hůla', 'addresses' => [ ['city' => 'Prague'], ['city' => 'Roztoky'], ], ]; $backend = new Milo\Hydrator\Backend\PublicPropertiesBackend; $hydrator = new Milo\Hydrator\Hydrator($backend); /** @var Person $person created from array */ $person = $hydrator->hydrate(Person::class, $data);
Personally, I'm using it for hydrate and export configuration written in NEON. Configuration stored in an array is fine, but when grows too much, it's hard to refactor. Working with type hinted objects in IDE is much more comfortable and less error prone.
Backend
There is only one backend for now.
PublicPropertiesBackend
It manipulates with public properties of class.
It makes type check according a property type definition or @var annotation.
Annotation type string has to be in a union format (e.g. int|string|null).
Type checking is strict which means, integer cannot be cast to string or vice versa even it would be possible.
Implement Milo\Hydrator\IHydratorBackend for your own backend.
Caching
Implement ICache and use it with the backend. It saves resources associated with
classes reflection and FQCN (Fully Qualified Class Name) resolving.
I'm using a simple cache class with Nette Caching. It looks like:
use Milo\Hydrator; use Nette\Caching\Cache; use Nette\Caching\Storage; class HydratorCache implements Hydrator\Backend\ICache { private Cache $cache; public function __construct(Storage $storage) { $this->cache = new Cache($storage, 'milo.hydrator'); } public function save($key, $value, array $dependentFiles = null) { $dependencies = $dependentFiles ? [Cache::FILES => $dependentFiles] : null; $this->cache->save($key, $value, $dependencies); } public function load($key) { return $this->cache->load($key); } }
Installation
Use Composer: composer require milo/hydrator
TODO
- improve error messages for deep structures because now are terrible
- support more than one array dimension in Hydrator
milo/hydrator 适用场景与选型建议
milo/hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 72.18k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2016 年 07 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 milo/hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 milo/hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 72.18k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-09