PHP code example of openapi-tools / representation

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


use OpenAPITools\Representation\Representation;
use OpenAPITools\Utils\Namespace_;

$representation = new Representation(
    client: $client,
    webHooks: $webHooks,
    schemas: $schemas,
);

$namespaced = $representation->namespace(
    new Namespace_('Vendor\\Api', 'Vendor\\Tests\\Api'),
);

use OpenAPITools\Representation\Contract;
use OpenAPITools\Representation\Property;
use OpenAPITools\Representation\Schema;

$schema = new Schema(
    className: 'Schema\\User',
    contracts: [
        new Contract('Contract\\User', [$property]),
    ],
    errorClassName: 'Schema\\Error\\User',
    errorClassNameAliased: 'Schema\\ErrorAlias\\User',
    title: 'User',
    description: 'A user resource',
    example: [],
    properties: [$property],
    schema: $openApiSchema,
    isArray: false,
    type: [],
    alias: [],
);

$namespaced = $schema->namespace($namespace);

$namespaced->className->fullyQualified->source;               // \Vendor\Api\Schema\User
$namespaced->errorClassName->fullyQualified->source;          // \Vendor\Api\Schema\Error\User
$namespaced->contracts[0]->className->fullyQualified->source; // \Vendor\Api\Contract\User

use OpenAPITools\Representation\Operation;
use OpenAPITools\Representation\Operation\RequestBody;
use OpenAPITools\Representation\Operation\Response;

$operation = new Operation(
    className: 'Operation\\CreateUser',
    classNameSanitized: 'Operation\\CreateUser',
    operatorClassName: 'Operator\\CreateUser',
    operatorLookUpMethod: 'createUser',
    name: 'create user',
    nameCamel: 'createUser',
    group: null,
    groupCamel: null,
    operationId: 'createUser',
    matchMethod: 'createUser',
    method: 'POST',
    summary: 'Create a user',
    externalDocs: null,
    path: '/users',
    metaData: [],
    returnType: ['application/json'],
    parameters: [],
    requestBody: [
        new RequestBody('application/json', $schema),
    ],
    response: [
        new Response(200, 'application/json', 'OK', $schema),
    ],
    empty: [],
);

$namespaced = $operation->namespace($namespace);

$namespaced->className->fullyQualified->source;                         // \Vendor\Api\Operation\CreateUser
$namespaced->operatorClassName->fullyQualified->source;                 // \Vendor\Api\Operator\CreateUser
$namespaced->requestBody[0]->schema->className->fullyQualified->source; // \Vendor\Api\Schema\User

use OpenAPITools\Representation\Hydrator;
use OpenAPITools\Representation\Path;

$path = new Path(
    className: 'Path\\Users',
    hydrator: new Hydrator(
        className: 'Hydrators\\Users',
        methodName: 'users',
        schemas: [$schema],
    ),
    operations: [$operation],
);

$namespaced = $path->namespace($namespace);

$namespaced->className->fullyQualified->source;                       // \Vendor\Api\Path\Users
$namespaced->hydrator->className->fullyQualified->source;             // \Vendor\Api\Hydrators\Users
$namespaced->hydrator->schemas[0]->className->fullyQualified->source; // \Vendor\Api\Schema\User

use OpenAPITools\Representation\WebHook;

$webHook = new WebHook(
    event: 'user.created',
    summary: 'User created',
    description: 'Sent when a user is created',
    operationId: 'userCreated',
    documentationUrl: 'https://example.com/docs/webhooks/user-created',
    headers: [$header],
    schema: ['application/json' => $schema],
);