PHP code example of n1215 / openapi-http-foundation-validator

1. Go to this page and download the library: Download n1215/openapi-http-foundation-validator 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/ */

    

n1215 / openapi-http-foundation-validator example snippets


$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
/** @var \Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface $httpMessageFactory */
$httpMessageFactory = new \Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory(
    serverRequestFactory: $psr17Factory,
    streamFactory: $psr17Factory,
    uploadedFileFactory: $psr17Factory,
    responseFactory: $psr17Factory
);

/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */
$validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory))
    ->fromYamlFile('/path/to/openapi.yaml')
    ->setCache(new YourPsr6Cache(), 86400);

/** @var \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder $validatorBuilder */
$validatorBuilder = (new \N1215\OpenApiValidation\HttpFoundation\ValidatorBuilder($psr17Factory))
    ->fromJsonFile('/path/to/openapi.json')
    ->setSimpleCache(new YourPsr16Cache(), 3600);

/** @var \N1215\OpenApiValidation\HttpFoundation\Validators $validators */
$validators = $validatorBuilder->getValidators();

/** @var \Symfony\Component\HttpFoundation\Request $request */
/** @var \N1215\OpenApiValidation\HttpFoundation\RequestValidatorInterface $requestValidator */
$requestValidator = $validators->getRequestValidator();
$requestValidator->validate($request);

/** @var \Symfony\Component\HttpFoundation\Response $response */
/** @var \N1215\OpenApiValidation\HttpFoundation\ResponseValidatorInterface $responseValidator */
$responseValidator = $validators->getResponseValidator();
$responseValidator->validate(
    new \N1215\OpenApiValidation\OperationAddress('/path', 'GET'),
    $response
);