PHP code example of openapi-tools / registry

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


use cebe\openapi\spec\Schema as OpenApiSchema;
use OpenAPITools\Registry\Schema;

$registry = new Schema(
    allowDuplicatedSchemas: false,
    useAliasesForDuplication: false,
);

$userSchema = new OpenApiSchema(['type' => 'object', 'title' => 'User']);
$registry->addClassName('User', $userSchema);

// Same JSON structure resolves to the pre-registered name.
$matching = new OpenApiSchema(['type' => 'object', 'title' => 'User']);
$registry->get($matching, 'Fallback'); // 'User'

// A new schema gets a class name from the fallback and is queued as unknown.
$other = new OpenApiSchema(['type' => 'string', 'format' => 'uuid']);
$registry->get($other, 'Uuid'); // 'Uuid'

while ($registry->hasUnknownSchemas()) {
    foreach ($registry->unknownSchemas() as $unknown) {
        // $unknown->name, $unknown->className, $unknown->schema
    }
}

$registry = new Schema(
    allowDuplicatedSchemas: true,
    useAliasesForDuplication: true,
);

$schema = new OpenApiSchema(['type' => 'string', 'title' => 'shared']);
$registry->addClassName('ExistingClass', $schema);

$duplicate = new OpenApiSchema(['type' => 'string', 'title' => 'shared']);
$registry->get($duplicate, 'NewFallback'); // 'NewFallback'

iterator_to_array($registry->aliasesForClassName('Schema\\ExistingClass'));
// ['Schema\\NewFallback']

use cebe\openapi\spec\Schema as OpenApiSchema;
use OpenAPITools\Registry\Contract;

$contract = new Contract();

$inline = new OpenApiSchema([
    'type'       => 'object',
    'properties' => ['id' => ['type' => 'integer']],
]);

$contract->get($inline, 'Contract\\User\\Profile'); // 'Contract\\User\\Profile'

foreach ($contract->contracts() as $registered) {
    // $registered->name, $registered->className, $registered->schema
}

use OpenAPITools\Registry\ThrowableSchema;

$throwables = new ThrowableSchema();

$throwables->add('Schema\\NotFound');
$throwables->has('Schema\\NotFound'); // true