Skip to content

Sufir/hydrator

Repository files navigation

sufir/hydrator

Latest Version on Packagist Build Status Quality Score Coverage Status Issues Software License

Install

Via Composer

$ composer require sufir/hydrator

Usage

Using Instantiator.

<?php
use Sufir\Hydrator\Instantiator;

$instantiator = new Instantiator();
$newObject = $instantiator->newInstance('\Some\ClassName');

Using Hydrators.

<?php
use Sufir\Hydrator\PlainArrayHydrator as Hydrator;

$className = '\Namespace\Domain\Entity';
$hydrateData = [
    // simple properties
    'firstName' => 'John',
    'lastName' => 'Doe',
    // id property of sub class \Namespace\Domain\Entity\Identity
    '__identity:Identity_id' => 100500,
    // some properties in subclass from another namespace
    '__uncloneableClass:\vendor\Some\Super\Class_someProperty1' => 'value 1',
    '__uncloneableClass:\vendor\Some\Super\Class_someProperty2' => null,
    // unexpected properties of subclass will be ignored
    '__uncloneableClass:\vendor\Some\Super\Class_unknownProperty' => '!undefined',
    // internal classes cannot be created and injected (in current version)
    //'__stdClass:\stdClass_unknownProperty' => '!undefined',
];

/* @var $object PrivateConstructor */
$object = $this->hydrator->hydrate($data, $className);

var_dump($object);
class \Namespace\Domain\Entity (5) {
    protected $firstName => string(4) "John"
    protected $lastName => string(3) "Doe"
    protected $identity =>
        class \Namespace\Domain\Entity\Identity {
            protected $id => int(100500)
        }
    protected $uncloneableClass =>
        class \vendor\Some\Super\Class (2) {
            public $someProperty1 => string(7) "value 1"
            public $someProperty2 => NULL
        }
}

Testing

$ composer test

Credits

License

The MIT License (MIT).