PHP code example of anonymous-php / hydrator

1. Go to this page and download the library: Download anonymous-php/hydrator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

anonymous-php / hydrator example snippets




nonymous\Hydrator\Hydrator;

class Vehicle
{
    private $doors;
    private $seats;
}

$hydrator = new Hydrator();

$model = new Vehicle();
$data = ['doors' => 3, 'seats' => 2];

$hydrator->hydrate($data, $model);

var_dump($model);

$properties = $hydrator->extract($model);

var_dump($properties);

/*
    object(Vehicle)#3 (2) {
      ["doors":"Vehicle":private]=>
      int(3)
      ["seats":"Vehicle":private]=>
      int(2)
    }
    array(2) {
      ["doors"]=>
      int(3)
      ["seats"]=>
      int(2)
    }
*/



nonymous\Hydrator\DynamicHydrator;

class CustomHydrator extends DynamicHydrator
{

    protected function getGetterName(string $property): string
    {
        return 'get' . $this->prepareName($property) . 'Attribute';
    }

    protected function getSetterName(string $property): string
    {
        return 'set' . $this->prepareName($property) . 'Attribute';
    }

}



nonymous\Hydrator\Hydrator;
use Anonymous\Hydrator\DynamicHydrator;

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; }
}

$model = new Example();
$data = ['foo' => 1, 'bar' => 2, 'baz' => 3];

$iterationsCount = 10000;

$hydrator = new Hydrator();

$start = microtime(true);
for ($i = 0; $i < $iterationsCount; $i++) {
    $hydrator->hydrate($data, $model);
}
$stop = microtime(true);

echo 'Hydrate: ', ($stop - $start), PHP_EOL;

$start = microtime(true);
for ($i = 0; $i < $iterationsCount; $i++) {
    $hydrator->extract($model);
}
$stop = microtime(true);

echo 'Extract: ', ($stop - $start), PHP_EOL;

$hydrator = new DynamicHydrator();

$start = microtime(true);
for ($i = 0; $i < $iterationsCount; $i++) {
    $hydrator->hydrate($data, $model);
}
$stop = microtime(true);

echo 'Hydrate using setters: ', ($stop - $start), PHP_EOL;

$start = microtime(true);
for ($i = 0; $i < $iterationsCount; $i++) {
    $hydrator->extract($model);
}
$stop = microtime(true);

echo 'Extract using getters: ', ($stop - $start), PHP_EOL;

/*
    Hydrate: 0.0046491622924805
    Extract: 0.0065388679504395
    Hydrate using setters: 0.011441946029663
    Extract using getters: 0.013833999633789
 */

composer