PHP code example of prometee / swagger-client-generator

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

    

prometee / swagger-client-generator example snippets



declare(strict_types=1);

$loader = der\ClassBuilder;
use Prometee\PhpClassGenerator\Builder\Model\ModelFactoryBuilder;
use Prometee\PhpClassGenerator\Builder\View\ViewFactoryBuilder;
use Prometee\SwaggerClientGenerator\OpenApi\Helper\ModelHelper;
use Prometee\SwaggerClientGenerator\OpenApi\Helper\OperationsHelper;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\ModelConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\OpenApiConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\Converter\OperationsConverter;
use Prometee\SwaggerClientGenerator\PhpGenerator\PhpGenerator;
use Prometee\SwaggerClientGenerator\PhpGenerator\Operation\OperationsMethodGenerator;

$baseUri = 'https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore-expanded.json';
$folder = __DIR__ . '/build';
$namespace = 'Tests\\Prometee\\SwaggerClientGenerator\\Build';
$abstractOperationClass = \MyVendor\MyApi\AbstractOperations::class;
$throwClasses = [
    \MyVendorMyApi\piException => 'ApiException',
    \Http\Client\Exception => 'HttpClientException',
    \Symfony\Component\Serializer\Exception\ExceptionInterface => 'SerializerExceptionInterface',
];

// Instantiate the PHP class generator.
// It builds PHP classes from a given array config.
$phpGenerator = new PhpGenerator(
    new ClassBuilder(
        new ModelFactoryBuilder(),
        new ViewFactoryBuilder()
    )
);

// Instantiate the Open API model converter.
// It will convert model definitions to an array of config for the PhpGenerator.
$modelConverter = new ModelConverter(
    'Model',
    $namespace,
    new ModelHelper()
);

// Instantiate the Open API operations converter.
// It will create array of config for the PhpGenerator to create Operations classes.
// They will contain for example each "GET /pets" "GET /pets/{id}" methods to query the API. 
$operationsConverter = new OperationsConverter(
    'Operations',
    $namespace . "\\" . $modelConverter->getModelNamespacePrefix(),
    new OperationsHelper(),
    new OperationsMethodGenerator()
);

// 0.1. Configure the PHP Generator
$phpGenerator->configure($path, $namespace);
// 0.2. Configure the Operations classes with some default extending class
$operationsConverter->setAbstractOperationsClass($abstractOperationClass);
// 0.3. Configure the Operations classes with some PHPDoc @throw class on each generated methods
$operationsConverter->setThrowsClasses($throwClasses);

// 1. Convert OpenApi schema to an array understandable by the PhpGenerator
$openApiConverter = new OpenApiConverter($baseUri, $modelConverter, $operationsConverter);
$classConfig = $openApiConverter->convert();

// 2. Create PHP classes thank to the config given
$phpGenerator->setClassesConfig($classConfig);
$phpGenerator->generate();