2017 © Pedro Peláez
 

library hydrator

Hydrates object from array, exports object to array

image

milo/hydrator

Hydrates object from array, exports object to array

  • Tuesday, February 20, 2018
  • by milonemilo
  • Repository
  • 5 Watchers
  • 23 Stars
  • 308 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 1 % Grown

The README.md

Build Status, (*1)

Usage

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

Personally, I'm using it for hydrate and export configuration written in NEON. Configuration stored in an array is fine, but when grows too much, it's hard to refactor. Working with type hinted objects in IDE is much more comfortable and less error prone., (*2)

Backend

There is only one backend for now., (*3)

PublicPropertiesBackend

It manipulates with public properties of class. It makes type check according a property type definition or @var annotation. Annotation type string has to be in a union format (e.g. int|string|null). Type checking is strict which means, integer cannot be cast to string or vice versa even it would be possible., (*4)

Implement Milo\Hydrator\IHydratorBackend for your own backend., (*5)

Caching

Implement ICache and use it with the backend. It saves resources associated with classes reflection and FQCN (Fully Qualified Class Name) resolving., (*6)

I'm using a simple cache class with Nette Caching. It looks like:, (*7)

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

Installation

Use Composer: composer require milo/hydrator, (*8)

TODO

  • improve error messages for deep structures because now are terrible
  • support more than one array dimension in Hydrator

The Versions

20/02 2018

dev-master

9999999-dev

Hydrates object from array, exports object to array

  Sources   Download

MIT GPLv2 GPLv3 GPL-2.0-only GPL-3.0-only

The Requires

  • php >=5.6.0

 

The Development Requires

05/12 2016

v0.3.0

0.3.0.0

Hydrates object from array, exports object to array

  Sources   Download

MIT GPLv2 GPLv3

The Requires

  • php >=5.6.0

 

The Development Requires

11/07 2016

v0.2.0

0.2.0.0

Hydrates object from array, exports object to array

  Sources   Download

MIT GPLv2 GPLv3

The Requires

  • php >=5.6.0

 

The Development Requires

01/07 2016

v0.1.0

0.1.0.0

Hydrates object from array, exports object to array

  Sources   Download

MIT GPLv2 GPLv3

The Requires

  • php >=5.6.0

 

The Development Requires