stratadox/hydrator
Composer 安装命令:
composer require stratadox/hydrator
包简介
README 文档
README
Lightweight hydrators, usable for various hydration purposes. Hydrate away!
Installation
Install with composer:
composer require stratadox/hydrator
What is this?
The Hydrator package exists in the context of object deserialization.
It is useful when loading objects from a data source.
To hydrate an object, means to assign values to its properties.
A Hydrator populates the
fields of other objects.
Hydration generally works in tandem with Instantiation;
the process of creating empty objects.
How to use this?
Basic Objects
The most basic usage looks like this:
<?php use Stratadox\Hydrator\ObjectHydrator; $hydrator = ObjectHydrator::default(); $thing = new Thing(); $hydrator->writeTo($thing, [ 'foo' => 'Bar.', 'property' => 'value', ]); assert($thing->foo === 'Bar.'); assert($thing->getProperty() === 'value');
The default hydrator requires the hydrated object to have access to all of its own properties.
When that's not the case, for instance when some properties are private to the
parent, a reflective hydrator is available:
<?php use Stratadox\Hydrator\ReflectiveHydrator; $hydrator = ReflectiveHydrator::default();
Collection Objects
To hydrate collection objects, the Hydrator package provides either a
MutableCollectionHydrator, suitable for most collection classes:
<?php use Stratadox\Hydrator\MutableCollectionHydrator; $hydrator = MutableCollectionHydrator::default(); $collection = new SplFixedArray; $hydrator->writeTo($collection, ['foo', 'bar']); assert(2 === count($collection));
The MutableCollectionHydrator hydrates by mutating the collection object.
Naturally, this will not work when your collections are immutable,
in which case the ImmutableCollectionHydrator should be used instead.
What else can it do?
The hydrators can be decorated to extend their capabilities.
Mapping
To transform the input data with hydration mapping,
the Mapping decorator can be used:
<?php use Stratadox\HydrationMapping\IntegerValue; use Stratadox\HydrationMapping\StringValue; use Stratadox\Hydrator\MappedHydrator; use Stratadox\Hydrator\ObjectHydrator; $hydrator = MappedHydrator::using( ObjectHydrator::default(), StringValue::inProperty('title'), IntegerValue::inProperty('rating'), StringValue::inPropertyWithDifferentKey('isbn', 'id') ); $book = new Book; $hydrator->writeTo($book, [ 'title' => 'This is a book.', 'rating' => 3, 'isbn' => '0000000001' ]);
Observing
The hydration process can be observed in two ways: before or after hydrating.
To observe the hydration process right before hydration begins, use:
use Stratadox\Hydrator\ObjectHydrator; use Stratadox\Hydrator\ObserveBefore; $hydrator = ObserveBefore::hydrating(ObjectHydrator::default(), new MyCustomObserver());
To observe the hydration process right after hydration is done, use:
use Stratadox\Hydrator\ObjectHydrator; use Stratadox\Hydrator\ObserveAfter; $hydrator = ObserveAfter::hydrating(ObjectHydrator::default(), new MyCustomObserver());
The observer must be a HydrationObserver.
It will receive both the object instance and the input data.
stratadox/hydrator 适用场景与选型建议
stratadox/hydrator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 977 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 11 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 stratadox/hydrator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 stratadox/hydrator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 977
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-13