bayfrontmedia / openapi-parser
PHP OpenAPI specification parser
Requires
- php: ^8.0
- ext-yaml: *
- bayfrontmedia/php-array-helpers: ^2.0
- bayfrontmedia/php-validator: ^4.0
This package is auto-updated.
Last update: 2024-10-10 01:49:50 UTC
README
PHP OpenAPI specification parser.
This library supports OAS 3.1, and resolves internal OpenAPI specification references. The OpenAPI specification can then be parsed into PHP objects.
License
This project is open source and available under the MIT License.
Author
Requirements
- PHP >= 8.0
Installation
composer require bayfrontmedia/opanapi-parser
Usage
The OpenApiSpec
class is used to parse JSON and YAML files into an array, as well as resolving any internal references.
The resolved specification array can then be used to create an OpenApiObject
instance.
All OpenAPI object class instances implement ObjectInterface
.
The ObjectInterface
includes a validate()
method to validate against the OpenAPI specification,
but all validation functions are currently rudimentary and should not be relied upon.
It is advised to use this library with an OpenAPI specification which has already been tested as valid.
NOTE: The
resolve
method can be quite slow depending on the size of the OpenAPI specification. It is strongly suggested to save/cache the resolved specification to use in production.
Example
$spec = OpenApiSpec::parseJson(file_get_contents('openapi-specification.json')); // Parse JSON $spec = OpenApiSpec::resolve($spec); // Resolve internal references (this file should be saved/cached) $openapi = new OpenApiObject($spec); // Create new OpenApiObject using a resolved and valid OpenAPI specification // Get PathItemObject for a single path $path = $openapi->getPath('/auth/login'); $request = $path->getOperation($path::OPERATION_POST);
Additional documentation coming soon.