OpenAPI
Raxos\OpenAPI\OpenAPI represents the root of the specification document: the info block, servers, paths, components and tags. It renders that structure to JSON or YAML.
final class OpenAPI implements DefinitionInterfaceConstants OpenAPI::VERSION (3.1.1) and OpenAPI::SCHEMA (the OpenAPI JSON schema URL) are exposed for reference.
Methods
__construct
public function __construct(
public readonly Info $info,
public readonly array $servers = [],
array $paths = [],
public readonly ?Components $components = null,
public readonly array $tags = []
)Creates the document. The paths array is sorted by key before it is stored, so the output is deterministic regardless of route order. servers is a list of Server definitions, and tags is a list of Tag definitions.
getJSON
public function getJSON(): stringReturns the specification encoded as JSON. Empty sections are omitted.
getYAML
public function getYAML(): stringReturns the specification encoded as YAML, rendered through symfony/yaml.
Example
<?php
declare(strict_types=1);
use Raxos\OpenAPI\OpenAPI;
use Raxos\OpenAPI\Definition\{Components, Info, Server};
$openapi = new OpenAPI(
info: new Info(title: 'My API', version: '1.0.0'),
servers: [new Server('https://api.example.com', 'Production')],
paths: $builder->paths->toArray(),
components: new Components(
responses: $builder->responses->toArray(),
schemas: $builder->schemas->toArray()
)
);
echo $openapi->getJSON();See Generating a specification for the full pipeline and Document metadata and security for the surrounding definitions.