Skip to content
This repository has been archived by the owner on Nov 1, 2018. It is now read-only.

serlo/athene2-hydrator

Repository files navigation

athene2-hydrator

Build Status Scrutinizer Code Quality Code Coverage Build Status

Attention, this module is not fully tested yet

Installation

athene2-versioning only officially supports installation through Composer. For Composer documentation, please refer to getcomposer.org.

Install the module:

$ php composer.phar require serlo-org/athene2-hydrator:dev-master

Hydrators?

This Zend Framework 2 module aims to provide you with advanced PluginAware Hydrators for Hydrators bundled with ZF2 and the Doctrine Module. The following Hydrators have been modified for Plugin use:

  • DoctrineObject
  • ArraySerializable
  • ClassMethods
  • ObjectProperty
  • Reflection

Plugins?

Object hydration can - sometimes - become difficult. Some hydration steps may need advanced logic. Let's take a real world example:

Bob updates the artists name of a music track entry on a web page. The new artist name is not in the database and needs to be created first and then associated with the track entry. A plugin could do this easily:

public function hydrate(array $data, $object)
{
    $data['artist'] = $this->myArtistService->createIfNotExists($data['artist']);
    return $data;
}

Usage

To create a new plugin, just implement Athene2\Hydrator\Plugin\HydratorPluginInterface. To add that plugin to the HydratorPluginManager, add the following to your module.config.php:

return [
    'hydrator_plugins' => [
        'invokables' => [
            'myPlugin' => 'MyNamespace\Hydrator\Plugin\MyPlugin'
        ]
    ]
];

The HydratorPluginManager is a ZF2 PluginManager, so you can use the factory and alias key as well.

Now let's create a Hydrator!

$pluginManager = $serviceManager->get('Athene2\Hydrator\Plugin\HydratorPluginManager');
$hydrator      = new PluginAwareClassMethods($pluginManager);

// Let's add the plugin 'myPlugin' to this hydrator
$hydrator->addPlugin('myPlugin');

// The plugin will be called before hydration takes place.
// What you do in the plugin does not matter. You can inject stuff or just modify the hydration data.
// Be aware however, that every key value pair you return will be hydrated by the ClassMethod hydrator.
// Make sure to remove key value pairs, which should be ignored by ClassMethod (or any other hydrator for that matter).
$hydrator->hydrate($myObject, $myData);

// The plugin will be called after extraction takes place.
$data = $hydrator->extract($myObject);

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages