scn / hydrator
一个实用的填充器和提取器库
Requires
- php: ^7.4|^8.0
Requires (Dev)
- phpunit/phpunit: ^9.4
- vimeo/psalm: ^3.17
Suggests
- scn/hydrator-property-values: Provides a set of easy-to-use property callbacks
README
一个实用的填充器和提取器库
安装
通过Composer
$ composer require scn/hydrator
使用
查看examples
文件夹,了解代码使用说明。
提取
首先,你需要一个实现\Scn\Hydrator\Config\ExtractorConfigInterface
的类。唯一的方法getExtractorProperties
必须返回一个键为字符串、值为可调用对象的数组。
这个数组的字符串键用于构建提取的结果。
相应的可调用对象必须具有签名callable(string $propertyName): mixed
。参数$propertyName
将是相应的字符串键。你通常不需要这个信息。
如果这个可调用对象是\Closure
的一个实例,它的$this
和static
上下文将与要提取的实体对象绑定。因此,闭包将能够访问实体的私有和受保护属性和方法。
一个简短的例子
<?php require_once 'vendor/autoload.php'; class ExtractorConfig implements Configuration\ExtractorConfigInterface { public function getExtractorProperties(): array { return [ 'property' => function (string $propertyName): string { return $this->privateProperty; // `$this` will be the entity to extract } ]; } } class Entity { private $privateProperty = 'private value'; } $hydrator = new \Hydrator(); $result = $hydrator->extract( new ExtractorConfig(), new Entity() ); var_dump(assert($result === ['property' => 'private value'])); // -> bool(true)
填充
对于填充,你需要一个实现\Scn\Hydrator\Configuration\HydratorConfigInterface
的类。唯一的方法getHydratorProperties
必须返回一个键为字符串、值为可调用对象的数组。
作为替代,你可以使用\Scn\Hydrator\Configuration\GenericHydratorConfig
。
字符串键必须与要填充对象的数据中的键对应。
相应的可调用对象必须具有签名callable(mixed $value, string $propertyName): void
。参数$propertyName
将是相应的字符串键。你通常不需要这个信息。
如果这个可调用对象是\Closure
的一个实例,它的$this
和static
上下文将与要填充的实体对象绑定。因此,闭包将能够访问实体的私有和受保护属性和方法。
一个简短的例子
<?php require_once __DIR__.'vendor/autoload.php'; class HydratorConfig implements Configuration\HydratorConfigInterface { public function getHydratorProperties(): array { return [ 'property' => function (string $value, string $propertyName): void { $this->privateProperty = $value; // $this will be the entity to hydrate } ]; } } class Entity { private $privateProperty = 'private value'; public function getPropertyValue(): string { return $this->privateProperty; } } $hydrator = new \Hydrator(); $data = [ 'property' => 'hydrated private value', ]; $entity = new Entity(); $hydrator->hydrate( new HydratorConfig(), $entity, $data ); var_dump(assert('hydrated private value' === $entity->getPropertyValue())); // -> bool(true)
填充标志
方法hydrate
有一个可选的第四个位标志参数$flags
。
目前有两个选项可用
\Scn\Hydrator\Hydrator::NO_STRICT_KEYS
(1)
如果这个位在$flags
中设置,填充器将忽略数据数组中没有任何对应键的键。
默认情况下,这个位没有设置,数据数组中的额外键将导致填充器抛出\InvalidArgumentException
异常。
\Scn\Hydrator\Hydrator::IGNORE_KEYS
(2)
如果这个位被设置,填充器将忽略数据数组的键,但假设数据数组中的条目与填充器配置数组中的条目顺序相同。
例子
<?php require_once __DIR__.'vendor/autoload.php'; class HydratorConfig implements Configuration\HydratorConfigInterface { public function getHydratorProperties(): array { return [ 'property_a' => function (string $value, string $propertyName): void { $this->privatePropertyA = $value; }, 'property_b' => function (string $value, string $propertyName): void { $this->privatePropertyB = $value; }, 'property_c' => function (string $value, string $propertyName): void { $this->privatePropertyC = $value; }, ]; } } class Entity { private $privatePropertyA; private $privatePropertyB; private $privatePropertyC; public function getPropertyValues(): array { return [ $this->privatePropertyA, $this->privatePropertyB, $this->privatePropertyC, ]; } } $hydrator = new \Hydrator(); $data = ['value a', 'value_b', 'value_c']; $entity = new Entity(); $hydrator->hydrate( new HydratorConfig(), $entity, $data, Hydrator::IGNORE_KEYS ); var_dump(assert($data === $entity->getPropertyValues())); // -> bool(true)