PHP code example of milo / hydrator

1. Go to this page and download the library: Download milo/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/ */

    

milo / hydrator example snippets


class Person
{
	/** @var string */
	public $name;

	public string $surname;

	public string|null $email;

	/** @var Address[] */
	public array $addresses;
}

class Address
{
	/** @var string */
	public $city;
}

$data = [
	'name' => 'Miloslav',
	'surname' => 'Hůla',
	'addresses' => [
		['city' => 'Prague'],
		['city' => 'Roztoky'],
	],
];


$backend = new Milo\Hydrator\Backend\PublicPropertiesBackend;
$hydrator = new Milo\Hydrator\Hydrator($backend);


/** @var Person $person  created from array */
$person = $hydrator->hydrate(Person::class, $data);

use Milo\Hydrator;
use Nette\Caching\Cache;
use Nette\Caching\Storage;


class HydratorCache implements Hydrator\Backend\ICache
{
	private Cache $cache;


	public function __construct(Storage $storage)
	{
		$this->cache = new Cache($storage, 'milo.hydrator');
	}


	public function save($key, $value, array $dependentFiles = null)
	{
		$dependencies = $dependentFiles
			? [Cache::FILES => $dependentFiles]
			: null;

		$this->cache->save($key, $value, $dependencies);
	}


	public function load($key)
	{
		return $this->cache->load($key);
	}
}