2017 © Pedro Peláez
 

library athene2-hydrator

Zend Framework 2 Module that provides a hydrator for Athene2

image

serlo-org/athene2-hydrator

Zend Framework 2 Module that provides a hydrator for Athene2

  • Sunday, August 10, 2014
  • by arekkas
  • Repository
  • 2 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

athene2-hydrator

Build Status Scrutinizer Code Quality Code Coverage Build Status, (*1)

Attention, this module is not fully tested yet, (*2)

Installation

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

Install the module:, (*4)

$ 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:, (*5)

  • 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:, (*6)

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:, (*7)

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:, (*8)

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., (*9)

Now let's create a Hydrator!, (*10)

$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);

The Versions