grzesie2k/hydrator

This package is abandoned and no longer maintained. No replacement package was suggested.

PHP PSR-7 compatible cacheable hydrator for modern application.

0.0.1 2018-03-10 22:24 UTC

This package is auto-updated.

Last update: 2020-02-26 10:33:50 UTC


README

Build Status Coverage Status Maintainability

PHP7 PSR-5 compatible cacheable hydrator for modern application.

Installation

composer install grzesie2k/hydrator

Examples

  • Validate primitive type
<?php

$hydrator = $hydratorFactory->createHydrator('int[]'); // any valid type

$intList = $hydrator->hydrator([1, 2, 3]); // ✓ OK
$intList = $hydrator->hydrator([1, 'a', 2]); // ☹ exception
  • Hydrate Your class by constructor
<?php

class Example
{
    /**
     * @param int $id
     * @param string $name we can read types from PHPDoc
     */
    public function __construct(int $id, $name) // or from type hints
    {
        // ... some operation
    }
}

$hydrator = $hydratorFactory->createHydrator(Example::class);

$hydrator->hydrate(\json_decode('{"id":2,"name":"Adam"}')); // ✓ OK
$hydrator->hydrate(\json_decode('{"id":"Nope","name":"Janek"}')); // ☹ exception

To do

  • create hydrator strategy to handle compound type (eq. int|string)
  • create alternative object hydration strategy (without constructor)
  • create hydrator strategies for some built-in types (eg. DateTime)
  • handle hydration exceptions with internal class and better messages
  • create symfony/laravel bundle
  • add examples and better docs