nettrine/hydrator

此包已被废弃且不再维护。未建议替代包。
此包最新版本(v1.0.2)无可用许可证信息。

将 Doctrine 实体转换为数组及其相反操作

v1.0.2 2019-02-22 11:40 UTC

This package is auto-updated.

Last update: 2022-02-03 13:21:59 UTC


README

68747470733a2f2f62616467656e2e6e65742f62616467652f737570706f72742f6769747465722f6379616e 68747470733a2f2f62616467656e2e6e65742f62616467652f737570706f72742f666f72756d2f79656c6c6f77 68747470733a2f2f62616467656e2e6e65742f62616467652f73706f6e736f722f646f6e6174696f6e732f463936383534

网站 🚀 contributte.org | 联系 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte

免责声明

⚠️ 此项目不再维护。
Composer nettrine/hydrator
版本
PHP
许可证

用法

Nette 安装

extensions:
    hydrator: Nettrine\Hydrator\DI\HydratorExtension

基本用法

$entity = $hydrator->toFields(Entity::class, [
	'name' => 'foo',
	'field' => 'value',
]);

$entity = $hydrator->toFields($entityObj, [
	'name' => 'foo',
	'field' => 'value',
]);

实体到数组

$array = $hydrator->toArray($entity);

自定义 ArrayAccessor

用于读取或写入对象的属性。

class CustomPropertyAccessor implements IPropertyAccessor {
	
	public function get(object $object, string $property) { ... }
	
	public function set(object $object, string $property, $value): void { ... }
	
}

Nette 注册

hydrator:
    propertyAccessor: CustomPropertyAccessor

适配器

你有获取或设置对象值的自定义规则吗?现有功能不符合您的需求?适配器可以用来扩展功能。

所有适配器都必须通过 addFieldAdapteraddArrayAdapter 方法进行注册。

In Nette

hydrator:
    adapters:
        fields:
            - Nettrine\DoctrineHydration\Adapters\CallbackFieldAdapter
            - Nettrine\DoctrineHydration\Adapters\TargetEntityFieldAdapter
        array:
            - Nettrine\DoctrineHydration\Adapters\JoinArrayAdapter
            - Nettrine\DoctrineHydration\Adapters\ManyToOneAdapter

ArrayAdapter

IArrayAdapter 接口已实现。内置适配器

ManyToOneArrayAdapter

所有对象关系都转换为 ID。

$entity = new Assoc class {
	public $id = 42;
	
	public $foo = 'foo';
	
	/**
	 * @ManyToOne(targetEntity="Assoc")
	 */
	public $assoc;
};

$entity->assoc->id++;

$array = $hydrator->toArray($entity);

$array === [
	'id' => 42,
	'assoc' => 43,
];
JoinArrayAdapter

对象关联转换为数组。

$entity = new Assoc class {
	public $id = 42;
	
	public $foo = 'foo';
	
	/**
	 * @ManyToOne(targetEntity="Assoc")
	 */
	public $assoc;
};

$entity->assoc->id++;

$array = $hydrator->toArray($entity, [
	'joins' => [
		'assoc' => 'foo'
	]
]);

$array === [
	'id' => 42,
	'assoc' => 'foo',
];

FieldAdapter

IFieldAdapter 接口已实现。内置适配器

CallbackFieldAdapter

可以使用回调

$hydrator->toFields($obj, [
	'name' => 'foo',
], [
	'callbacks' => [
		'name' => function (FieldArgs $args) {
		    $args->value = ucfirst($args->value);
		},
	] 
]);

现在 $name 属性的值是 Foo

TargetEntityFieldAdapter

在关联的情况下,将找到相应的实体

$hydrator->toFields($obj, [
	'assoc' => 42, // Item with the value of 42 will be found
]);

创建自定义适配器

假设我们有以下 image 自定义类型注解

/**
 * @ORM\Column(type="image")
 */

并希望在加湿过程中自动保存图像

class CustomFieldAdapter implements IFieldAdapter {

	public function __construct(IImageStorage $storage) { ... }

	public function isWorkable(FieldArgs $args): bool {
		// Apply only when the type is `image` and it is not an assocation
		return !$args->metadata->isAssociation($field) && $args->metadata->getFieldMapping($field)['type'] === 'image';
	}

	public function work(FieldArgs $args): void {
		$image = new Image($value);
		if ($args->hasSettingsSection('images')) {
			$image->setName($args->getSettingsSection('images'));
		}
		$this->storage->save($image);
		
		$args->value = $image;
	}

}

Nette 中的注册

hydrator:
    adapters:
        fields: 
            - CustomFieldAdapter

用法

$hydrator->toFields($obj, [
	'avatar' => __DIR__ . '/avatar.png',
], [
	'images' => [
		'avatar' => 'foo.png',
	]
]);

开发

该软件包由以下作者维护。

538058?v=3&s=80 749981?v=3&s=80 10145362?v=3&s=80

请考虑支持contributte开发团队。同时也感谢您使用此软件包。