<commit_before># Generated Hydrator

A [hydrator](http://framework.zend.com/manual/2.1/en/modules/zend.stdlib.hydrator.html) is an object capable of
extracting data from other objects, or filling them with data.

ProxyManager uses proxying to instantiate very fast generators, since this will allow access to protected properties
of the object to be handled by the hydrator.

## Usage

Here's an example of how you can create and use a hydrator generated by ProxyManager:

```php
<?php

use ProxyManager\Configuration;
use ProxyManager\Factory\HydratorFactory as Factory;

require_once __DIR__ . '/vendor/autoload.php';

class Example
{
    public    $foo = 1;
    protected $bar = 2;
    protected $baz = 3;
}

$config  = new Configuration();
$factory = new Factory($config);

$hydrator = $factory->createProxy('Foo');

$object = new Example();

var_dump($hydrator->extract($object)); // array('foo' => 1, 'bar' => 2, 'baz' => 3)
$hydrator->hydrate(
    array('foo' => 4, 'bar' => 5, 'baz' => 6),
    $object
);
var_dump($hydrator->extract($object)); // array('foo' => 4, 'bar' => 5, 'baz' => 6)
```

## Performance comparison

A hydrator generated by ProxyManager is very fast.
Here's the performance of the various hydrators of `Zend\Stdlib\Hydrator` compared to a ProxyManager's generated proxy:

```php
<?php
require_once __DIR__ . '/vendor/autoload.php';

$iterations = 1000000;

class Example
{
    public $foo;
    public $bar;
    public $baz;
    public function setFoo($foo) { $this->foo = $foo; }
    public function setBar($bar) { $this->bar = $bar; }
    public function setBaz($baz) { $this->baz = $baz; }
    public function getFoo() { return $this->foo; }
    public function getBar() { return $this->bar; }
    public function getBaz() { return $this->baz; }
}

$object  = new Example();
$data    = array('foo' => 1, 'bar' => 2, 'baz' => 3);
$factory = new ProxyManager\Factory\HydratorFactory(new ProxyManager\Configuration());
$proxies = array(
    $factory->createProxy('Example'),
    new Zend\Stdlib\Hydrator\ClassMethods(),
    new Zend\Stdlib\Hydrator\Reflection(),
);

foreach ($proxies as $proxy) {
    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i += 1) {
        $proxy->hydrate($data, $object);
        $proxy->extract($object);
    }
<commit_msg>Reducing iterations since otherwise reproducing the performance test is a pain<commit_after># Generated Hydrator

A [hydrator](http://framework.zend.com/manual/2.1/en/modules/zend.stdlib.hydrator.html) is an object capable of
extracting data from other objects, or filling them with data.

ProxyManager uses proxying to instantiate very fast generators, since this will allow access to protected properties
of the object to be handled by the hydrator.

## Usage

Here's an example of how you can create and use a hydrator generated by ProxyManager:

```php
<?php

use ProxyManager\Configuration;
use ProxyManager\Factory\HydratorFactory as Factory;

require_once __DIR__ . '/vendor/autoload.php';

class Example
{
    public    $foo = 1;
    protected $bar = 2;
    protected $baz = 3;
}

$config  = new Configuration();
$factory = new Factory($config);

$hydrator = $factory->createProxy('Foo');

$object = new Example();

var_dump($hydrator->extract($object)); // array('foo' => 1, 'bar' => 2, 'baz' => 3)
$hydrator->hydrate(
    array('foo' => 4, 'bar' => 5, 'baz' => 6),
    $object
);
var_dump($hydrator->extract($object)); // array('foo' => 4, 'bar' => 5, 'baz' => 6)
```

## Performance comparison

A hydrator generated by ProxyManager is very fast.
Here's the performance of the various hydrators of `Zend\Stdlib\Hydrator` compared to a ProxyManager's generated proxy:

```php
<?php
require_once __DIR__ . '/vendor/autoload.php';

$iterations = 10000;

class Example
{
    public $foo;
    public $bar;
    public $baz;
    public function setFoo($foo) { $this->foo = $foo; }
    public function setBar($bar) { $this->bar = $bar; }
    public function setBaz($baz) { $this->baz = $baz; }
    public function getFoo() { return $this->foo; }
    public function getBar() { return $this->bar; }
    public function getBaz() { return $this->baz; }
}

$object  = new Example();
$data    = array('foo' => 1, 'bar' => 2, 'baz' => 3);
$factory = new ProxyManager\Factory\HydratorFactory(new ProxyManager\Configuration());
$proxies = array(
    $factory->createProxy('Example'),
    new Zend\Stdlib\Hydrator\ClassMethods(),
    new Zend\Stdlib\Hydrator\Reflection(),
);

foreach ($proxies as $proxy) {
    $start = microtime(true);

    for ($i = 0; $i < $iterations; $i += 1) {
        $proxy->hydrate($data, $object);
        $proxy->extract($object);
    }

{'fraction_non_alphanumeric': 0.15209452201933404, 'fraction_numerical': 0.010741138560687433, 'mean_word_length': 3.990353697749196, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7456042', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1489, 'n_words': 456}