SchemaBuilder
Raxos\OpenAPI\SchemaBuilder turns PHP classes and properties into OpenAPI schema or reference definitions. It caches built schemas and named responses, so repeated references to the same class reuse a single component.
final readonly class SchemaBuilderThe two public maps responses and schemas accumulate the named components and are exposed by RouterBuilder as its own responses and schemas properties.
Methods
__construct
public function __construct(
public private(set) MapInterface $responses = new Map(),
public private(set) MapInterface $schemas = new Map()
)Creates the builder with empty or pre-seeded response and schema maps.
build
public function build(string $class, bool $nullable = false): voidBuilds and stores the schema for a class that carries a #[Model] attribute (or a compatible #[Schema] attribute). Classes without such an attribute are ignored.
reference
public function reference(string $class, bool $nullable = false): Reference|Schema|nullReturns a $ref pointing at the class's schema, building it on first use. When nullable is true the reference is wrapped in an anyOf with a nullable schema. Returns null when the class cannot be resolved.
response
public function response(Attr\Response $responseAttr): Reference|Response|Schema|nullResolves a #[Response] attribute into a response definition. Builtin collection types are expanded inline, and a JsonSerializable model is stored once as a named response and referenced afterwards.
Example
<?php
declare(strict_types=1);
use Raxos\OpenAPI\SchemaBuilder;
$builder = new SchemaBuilder();
$reference = $builder->reference(Product::class);
$schemas = $builder->schemas->toArray();See Documenting schemas for how types are detected.