Giter Site home page Giter Site logo

hydrator's Introduction

Build Status

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.

Backend

There is only one backend for now.

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.

Implement Milo\Hydrator\IHydratorBackend for your own backend.

Caching

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

I'm using a simple cache class with Nette Caching. It looks like:

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

TODO

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.