PHP code example of makinacorpus / goat-hydrator

1. Go to this page and download the library: Download makinacorpus/goat-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/ */

    

makinacorpus / goat-hydrator example snippets


class A
{
    /**
     * @var B
     */
    private $b;

    public function getB(): ?B
    {
        return $this->b;
    }
}

class B
{
    private $bar;

    /**
     * @var C
     */
    private $c;

    public function getBar(): string
    {
        return $this->bar ?? '';
    }

    public function getC(): ?C
    {
        return $this->c;
    }
}

class C
{
    private $foo;

    public function getFoo(): string
    {
        return $this->foo ?? '';
    }
}

$hydratorMap = new HydratorMap(\sys_get_temp_dir().'/hydrator/cache');

// Register the A class
$hydratorMap->addClassConfiguration(new ClassConfiguration(
    A::class,
    [
        'b' => B::class,
    ],
    [],
    HydratorInterface::CONSTRUCTOR_SKIP
));

// Register the B class
$hydratorMap->addClassConfiguration(new ClassConfiguration(
    C::class,
    [
        'c' => C::class,
    ],
    [],
    HydratorInterface::CONSTRUCTOR_SKIP
));


$separator = '__';

$values = [
    'b__bar' => "Hello, "
    'b__c__foo' => "World !",
];

$hydrator = $hydratorMap->get(A::class);
$a = $hydrator->createAndHydrateInstance($values);

return [
    // ...
    Goat\Hydrator\Bridge\Symfony\GoatHydratorBundle::class => ['all' => true],
];