PHP code example of clea / hydrator

1. Go to this page and download the library: Download clea/hydrator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

clea / hydrator example snippets


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

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

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

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);
        

class User
{
    //...
    
    /**
     * @var string
     */
    private $string;

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

class User
{
    //...
    
    /**
     * @var int
     */
    private $number;

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

class User
{
    //...
    
    /**
    * @noHydrated
    * @var string
    */
    private $notHydrated;
    
    //...
} 

class User
{
    //...
    
    /**
     * @var Nested
     */
    private $nested;

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

class User
{
    //...
    
    /**
     * @var \MyProject\UserChild[]
     */
    private $collection;

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