Documentation

Zend\Stdlib\Hydrator — Zend Framework 2 2.4.9 documentation

Zend\Stdlib

Zend\Stdlib\Hydrator

Hydration is the act of populating an object from a set of data.

The Hydrator is a simple component to provide mechanisms both for hydrating objects, as well as extracting data sets from them.

The component consists of an interface, and several implementations for common use cases.

HydratorInterface

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
namespace Zend\Stdlib\Hydrator;

interface HydratorInterface
{
    /**
     * Extract values from an object
     *
     * @param  object $object
     * @return array
     */
    public function extract($object);

    /**
     * Hydrate $object with the provided $data.
     *
     * @param  array $data
     * @param  object $object
     * @return object
     */
    public function hydrate(array $data, $object);
}

Usage

Usage is quite simple: simply instantiate the hydrator, and then pass information to it.

1
2
3
4
5
6
7
8
9
use Zend\Stdlib\Hydrator;
$hydrator = new Hydrator\ArraySerializable();

$object = new ArrayObject(array());

$hydrator->hydrate($someData, $object);

// or, if the object has data we want as an array:
$data = $hydrator->extract($object);

Available Implementations

  • Zend\Stdlib\Hydrator\ArraySerializable

    Follows the definition of ArrayObject. Objects must implement either the exchangeArray() or populate() methods to support hydration, and the getArrayCopy() method to support extraction.

  • Zend\Stdlib\Hydrator\ClassMethods

    Any data key matching a setter method will be called in order to hydrate; any method matching a getter method will be called for extraction.

  • Zend\Stdlib\Hydrator\DelegatingHydrator

    Composes a hydrator locator containing hydrators and will delegate hydrate() and extract() calls to the appropriate one based upon the class name of the object being operated on.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
 // Instantiate each hydrator you wish to delegate to
 $albumHydrator = new Zend\Stdlib\Hydrator\ClassMethods;
 $artistHydrator = new Zend\Stdlib\Hydrator\ClassMethods;

 // Map the entity class name to the hydrator using the HydratorPluginManager
 // In this case we have two entity classes, "Album" and "Artist"
 $hydrators = new Zend\Stdlib\Hydrator\HydratorPluginManager;
 $hydrators->setService('Album', $albumHydrator);
 $hydrators->setService('Artist', $artistHydrator);

 // Create the DelegatingHydrator and tell it to use our configured hydrator locator
 $delegating = new Zend\Stdlib\Hydrator\DelegatingHydrator($hydrators);

 // Now we can use $delegating to hydrate or extract any supported object
 $array = $delegating->extract(new Artist);
 $artist = $delegating->hydrate($data, new Artist);
  • Zend\Stdlib\Hydrator\ObjectProperty

    Any data key matching a publicly accessible property will be hydrated; any public properties will be used for extraction.

  • Zend\Stdlib\Hydrator\Reflection

    Similar to the ObjectProperty hydrator, but uses PHP’s reflection API to hydrate or extract properties of any visibility. Any data key matching an existing property will be hydrated; any existing properties will be used for extraction.

Copyright

© 2006-2017 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts