https://github.com/apiboard/php-openapi
OpenAPI Specification parser for PHP 8. Supports both OAS 3.0 and 3.1.
https://github.com/apiboard/php-openapi
openapi openapi-spec openapi-specification openapi3 php8
Last synced: 10 months ago
JSON representation
OpenAPI Specification parser for PHP 8. Supports both OAS 3.0 and 3.1.
- Host: GitHub
- URL: https://github.com/apiboard/php-openapi
- Owner: Apiboard
- License: mit
- Created: 2022-10-18T20:46:53.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-21T20:56:09.000Z (almost 2 years ago)
- Last Synced: 2025-03-25T18:45:26.255Z (10 months ago)
- Topics: openapi, openapi-spec, openapi-specification, openapi3, php8
- Language: PHP
- Homepage:
- Size: 309 KB
- Stars: 6
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PHP OpenAPI
OpenAPI Specification parser for PHP 8. Supports both OAS 3.0 and 3.1.
[](https://packagist.org/packages/apiboard/php-openapi)


## Features
- Parse OpenAPI files into a PHP object to interact with in code
- Validate OpenAPI files against the official JSON-schema descriptions
- Resolve external and internal references
## Installation
```bash
composer require apiboard/php-openapi
```
## Usage
You can interact with this library through the `OpenAPI::class` directly.
```php
$openAPI = new OpenAPI();
```
This class optionally accepts an implementation of `Apiboard\OpenAPI\Contents\Retriever::class` which will be used to retrieve the file contents. By default the local filesystem will be used to retrieve file contents.
### Parse
You can parse the contents of a file by passing its path to `parse()`. This will attempt to retrieve the file's contents and resolve any external references.
It returns a PHP object that represents the OAS document structure that can be used in code.
```php
$document = $openAPI->parse('/path/to/my-oas.json');
$document->openapi(); // 3.1.0
```
### Validate
You can directly validate the contents of a file against the official OpenAPI JSON-schema descriptions. It returns an array of possible errors that occured during the validation.
```php
$errors = $openAPI->validate('/path/to/my-oas.yaml');
```
> ⚠️ Validation for OAS 3.1 does not check any JSON Schemas in your OpenAPI document because it allows you to use any JSON Schema dialect you choose!
### Resolve
You can resolve external and internal references. It returns a PHP object with the resolved contents.
```php
$contents = $openAPI->resolve('/path/to/my-oas.json');
$document = new Apiboard\OpenAPI\Structure\Document($contents);
```
When resolving references the contents will be retrieved from the local fileystem by default. You can override the way file contents is retrieved by passing a custom class that implements the `Apiboard\OpenAPI\Contents\Retriever` interface.
```php
$customRetriever = new MyCustomRetriever();
$openAPI = new OpenAPI($customRetriever);
$openAPI->resolve('/path/to/my-oas.json');
```
Circular references are resolved as an internal reference after recursing twice, this is to prevent infinite recursion.
## License
The MIT License (MIT). Please see [License File](LICENSE) for more information.