nettrine/hydrator

This package is abandoned and no longer maintained. No replacement package was suggested.
There is no license information available for the latest version (v1.0.2) of this package.

Convert Doctrine entity to array and conversely

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

Website 🚀 contributte.org | Contact 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte

Disclaimer

⚠️ This project is no longer being maintained.
Composer nettrine/hydrator
Version
PHP
License

Usage

Nette installation

extensions:
    hydrator: Nettrine\Hydrator\DI\HydratorExtension

Basic usage

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

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

Entity to an array

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

Custom ArrayAccessor

Used to read from or write to an object's property.

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

Nette registration:

hydrator:
    propertyAccessor: CustomPropertyAccessor

Adapters

Do you have custom rules of getting or setting an object's value? The existing features don't suit your needs? Adapters can be used to extend the functionality.

All the adapters have to be registered via addFieldAdapter or addArrayAdapter methods.

In Nette:

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

ArrayAdapter

IArrayAdapter interface is implemented. Built-in adapters:

ManyToOneArrayAdapter

All object relations are converted to an 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

Object association is converted to an array.

$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 interface is implemented. Built-in adapters:

CallbackFieldAdapter

A callback can be used:

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

The value of the $name property is now Foo.

TargetEntityFieldAdapter

In case of an association the corresponding entity will be found:

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

Creating a custom adapter

Say we have the following image custom type annotation:

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

and we want to automatically save the image during hydration:

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;
	}

}

Registration in Nette:

hydrator:
    adapters:
        fields: 
            - CustomFieldAdapter

Usage:

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

Development

This package was maintain by these authors.

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

Consider to support contributte development team. Also thank you for being used this package.