PHP code example of openapi-tools / generator-hydrator

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

    

openapi-tools / generator-hydrator example snippets


use OpenAPITools\Generator\Hydrator\Hydrator;
use OpenAPITools\Generator\Schema\Schema;
use PhpParser\BuilderFactory;

$builderFactory = new BuilderFactory();

// inside Package(..., generators: [
//     new Schema($builderFactory),
//     new Hydrator($builderFactory, 

use OpenAPITools\Generator\Hydrator\Hydrator;
use OpenAPITools\Generator\Schema\Schema;
use PhpParser\BuilderFactory;

$builderFactory = new BuilderFactory();

// Schema classes must exist before Hydrator runs — eval or ntation) as $file) {
    // $file->pathPrefix  — e.g. "src"
    // $file->fqcn        — e.g. "Internal\Hydrators" or "Internal\Hydrator\Operation\Root"
    // $file->contents    — PhpParser Node or pre-rendered string (ObjectMapperCodeGenerator output)
}

namespace ApiClients\Client\Example\Internal\Hydrator\Operation;

use EventSauce\ObjectHydrator\ObjectMapper;

class Root implements ObjectMapper
{
    public function hydrateObject(string $className, array $payload): object
    {
        return match ($className) {
            \ApiClients\Client\Example\Schema\Basic::class => $this->hydrateApiClients⚡️Client⚡️Example⚡️Schema⚡️Basic($payload),
            default => throw new \RuntimeException("Cannot hydrate unknown class: {$className}"),
        };
    }

    // hydrateObjects, serializeObject, serializeObjects, and per-class helpers ...
}

namespace ApiClients\Client\Example\Internal;

final class Hydrators implements \EventSauce\ObjectHydrator\ObjectMapper
{
    private ?\ApiClients\Client\Example\Internal\Hydrator\Operation\Root $operation🌀Root = null;

    public function hydrateObject(string $className, array $payload): object
    {
        return match ($className) {
            \ApiClients\Client\Example\Schema\Basic::class
                => $this->getObjectMapperOperation🌀Root()->hydrateObject($className, $payload),
            default => throw new \RuntimeException("Cannot hydrate unknown class: {$className}"),
        };
    }

    public function getObjectMapperOperation🌀Root(): \ApiClients\Client\Example\Internal\Hydrator\Operation\Root
    {
        if (! $this->operation🌀Root instanceof \ApiClients\Client\Example\Internal\Hydrator\Operation\Root) {
            $this->operation🌀Root = new \ApiClients\Client\Example\Internal\Hydrator\Operation\Root();
        }

        return $this->operation🌀Root;
    }

    // hydrateObjects, serializeObject, serializeObjects, and getters for other scopes ...
}