Skip to content
This repository has been archived by the owner on Oct 20, 2022. It is now read-only.
/ hydrator Public archive

simple php hydrator that support nested objects and collections

License

Notifications You must be signed in to change notification settings

nicosolo/hydrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hydrator

Description

Simple php hydration that support nested objects and collections

Requirements

PHP >= 7.0

Installation

Using composer

composer require clea/hydrator

how it's work

The hydrator base the type of property on the getter return type:

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

This will simply cast the var to string.

The Transformer Class is used for cast or make operation on the value before call the setter.

You can add customs transformer functions see the example section.

For be able to hydrate nested object you have to define the return type of the getter

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

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

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.

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

About

simple php hydrator that support nested objects and collections

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages