PHP code example of openapi-tools / generator-schema

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


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

$builderFactory = new BuilderFactory();

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

new Gathering\Schemas(
    allowDuplication: true,
    useAliasesForDuplication: true,
),

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

$generator = new Schema(new BuilderFactory());

foreach ($generator->generate($package, $representation->namespace($package->namespace)) as $file) {
    // $file->pathPrefix  — e.g. "src"
    // $file->fqcn        — e.g. "Schema\\Basic"
    // $file->contents    — PhpParser Node or pre-rendered string
}

namespace ApiClients\Client\Example\Contract;

/**
 * @property string $id
 * @property string $name
 */
interface Basic
{
}

namespace ApiClients\Client\Example\Schema;

final readonly class Basic implements \ApiClients\Client\Example\Contract\Basic
{
    private const SCHEMA_JSON = '{
    "type": "object",
    ...
}';
    public const SCHEMA_TITLE = 'basic';
    public const SCHEMA_DESCRIPTION = '';
    private const SCHEMA_EXAMPLE_DATA = '[ ... ]';

    public function __construct(
        public string $id,
        public string $name,
    ) {
    }
}

// from DoubleUseOfTypes.yaml — type: [null, string, number, integer] + anyOf
public function __construct(public string|int|float|null $value)
{
}

// Simplified behaviour of generated SingleCastUnionToType classes
if (is_array($value)) {
    $signature = implode('|', sort(array_unique(array_keys($value))));
    if ($signature === 'preferred|value') { /* hydrate PreferredName */ }
    if ($signature === 'value') { /* hydrate FirstName */ }
}
return $value;

namespace ApiClients\Client\Example\Schema\Error;

final class Basic extends \Error
{
    public function __construct(
        public int $status,
        public \ApiClients\Client\Example\Schema\Basic $error,
    ) {
    }
}