2017 © Pedro Peláez
 

library hydrator

image

clea/hydrator

  • Friday, September 29, 2017
  • by nicosolo
  • Repository
  • 1 Watchers
  • 2 Stars
  • 71 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

hydrator

Description

Simple php hydration that support nested objects and collections, (*1)

Requirements

PHP >= 7.0, (*2)

Installation

Using composer, (*3)

composer require clea/hydrator

how it's work

The hydrator base the type of property on the getter return type:, (*4)

public function getName(): string
{
    ...
}

This will simply cast the var to string., (*5)

The Transformer Class is used for cast or make operation on the value before call the setter., (*6)

You can add customs transformer functions see the example section., (*7)

For be able to hydrate nested object you have to define the return type of the getter, (*8)

public function getNestedClass(): MyNestedClass
{
    ...
}

Usage

Options

        $settings = [
            "additional_type" => [
               MyNumber::class => function($value){
                    return new MyNumber($value);
               }
               //...
            ],
            "cache" => true
        ];

Example

use Clea\Hydrator\Hydrator;

$data = [
    "string" => "value 1",
    "number" => "10",
    "notHydrated" => "test",
    "nested" => [
        "field" => "2017-10-10"
    ],
    "collection" => [
        ["field" => "10.42"],
        ["field" => "10.42"]
    ] 
];

$hydrator = new Hydrator();
$data = $this->getUserData();

$user = $hydrator->hydrate(User::class, $data);

Simple field casted to string

class User
{
    //...

    /**
     * @var string
     */
    private $string;

    /**
     * @return string
     */
    public function getString(): string
    {
        return $this->string;
    }

    //...
} 

Simple field casted to number

class User
{
    //...

    /**
     * @var int
     */
    private $number;

    /**
     * @return int
     */
    public function getNumber(): int
    {
        return $this->number;
    }

    //...
} 

No hydrated field

class User
{
    //...

    /**
    * @noHydrated
    * @var string
    */
    private $notHydrated;

    //...
} 

Nested field

class User
{
    //...

    /**
     * @var Nested
     */
    private $nested;

    /**
     * @return Nested
     */
    public function getNested(): Nested
    {
        return $this->nested;
    }

    //...
} 

Collection field

In this case you need to use the same syntax as below, the comment are used for get the type of the entity in the collection, (*9)

You need to give the full name of your class: * @var \MyProject\Message[], (*10)

class User
{
    //...

    /**
     * @var \MyProject\UserChild[]
     */
    private $collection;

    /**
     * @return \MyProject\UserChild[]
     */
    public function getCollection(): array
    {
        return $this->collection;
    }

    //...
} 

If you want to contribute feel free to contact me / make a pull request., (*11)

The MIT License (MIT). Please see LICENSE for more information., (*12)

The Versions

29/09 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Development Requires

by Avatar Nico

hydrator

29/09 2017

1.1

1.1.0.0

  Sources   Download

MIT

The Development Requires

by Avatar Nico

hydrator

29/09 2017

1.0

1.0.0.0

  Sources   Download

The Development Requires

by Avatar Nico