, (*1)
Website 🚀 contributte.org | Contact 👨🏻💻 f3l1x.io | Twitter 🐦 @contributte , (*3)
:warning: | This project is no longer being maintained. |
---|---|
Composer | nettrine/hydrator |
---|---|
Version | |
PHP | |
License |
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);
Used to read from or write to an object's property., (*4)
class CustomPropertyAccessor implements IPropertyAccessor { public function get(object $object, string $property) { ... } public function set(object $object, string $property, $value): void { ... } }
Nette registration:, (*5)
hydrator: propertyAccessor: CustomPropertyAccessor
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., (*6)
All the adapters have to be registered via addFieldAdapter
or addArrayAdapter
methods., (*7)
In Nette:, (*8)
hydrator: adapters: fields: - Nettrine\DoctrineHydration\Adapters\CallbackFieldAdapter - Nettrine\DoctrineHydration\Adapters\TargetEntityFieldAdapter array: - Nettrine\DoctrineHydration\Adapters\JoinArrayAdapter - Nettrine\DoctrineHydration\Adapters\ManyToOneAdapter
IArrayAdapter
interface is implemented. Built-in adapters:, (*9)
All object relations are converted to an ID., (*10)
$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, ];
Object association is converted to an array., (*11)
$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', ];
IFieldAdapter
interface is implemented. Built-in adapters:, (*12)
A callback can be used:, (*13)
$hydrator->toFields($obj, [ 'name' => 'foo', ], [ 'callbacks' => [ 'name' => function (FieldArgs $args) { $args->value = ucfirst($args->value); }, ] ]);
The value of the $name
property is now Foo
., (*14)
In case of an association the corresponding entity will be found:, (*15)
$hydrator->toFields($obj, [ 'assoc' => 42, // Item with the value of 42 will be found ]);
Say we have the following image
custom type annotation:, (*16)
/** * @ORM\Column(type="image") */
and we want to automatically save the image during hydration:, (*17)
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:, (*18)
hydrator: adapters: fields: - CustomFieldAdapter
Usage:, (*19)
$hydrator->toFields($obj, [ 'avatar' => __DIR__ . '/avatar.png', ], [ 'images' => [ 'avatar' => 'foo.png', ] ]);
This package was maintain by these authors., (*20)
Consider to support contributte development team. Also thank you for being used this package., (*24)