The object model
Every object of the OpenAPI specification has a PHP class in Ark4ne\OpenApiWriter\Objects, and every class follows
the same rules. Learn them once and the whole API follows.
Objects are immutable
Every method returns a modified copy; the object it is called on does not change.
use Ark4ne\OpenApiWriter\Objects\Schema;
$name = Schema::string();
$short = $name->maxLength(10);
$name->maxLength; // null
$short->maxLength; // 10
Always use the return value. A call whose result is dropped does nothing:
$document->addPaths($path); // lost
$document = $document->addPaths($path); // kept
In return, objects can be shared and derived freely: one Response::notFound() can be used by every operation, and
a base schema can be extended without touching the original.
Methods are named after the specification
One method per field, with exactly the name of the field: operationId(), requestBody(), exclusiveMinimum(),
additionalProperties(). If a field exists in the specification, the method exists, with no synonym.
Each class has a create() constructor and shortcuts for the common cases:
| Shortcut | Same as |
|---|---|
Schema::string('email') | Schema::create('email')->type('string') |
Operation::get() | the operation placed under get of its path item |
Parameter::path('id') | in: path, required: true, name: id |
Response::created() | status 201, description Created |
MediaType::json() | application/json |
SecurityScheme::http('bearer') | type: http, under the name bearer |
Fields are read as properties: $schema->maxLength, $info->title. An unset field reads null; has('maxLength')
tells whether it is set.
The first argument is the key in the parent
Many objects live in a map of their parent: a property in properties, a schema in components.schemas, a path in
paths, a response under its status code. The first argument of create() and of the shortcuts is that key, called
the objectId:
Schema::string('email'); // the property "email"
Schema::object('User'); // the component "User"
PathItem::create('/users/{id}'); // the path "/users/{id}"
Header::create('X-Rate-Limit'); // the header "X-Rate-Limit"
Example::create('admin'); // the example "admin"
The shortcuts set it for you (Response::ok() is 200, MediaType::json() is application/json), and
objectId() changes it, for example to store a response as a component:
Response::notFound()->objectId('NotFound');
An entry of a map needs an objectId, and two entries of the same map cannot share one: both throw, at once, a
MissingObjectIdException or a DuplicateObjectIdException.
Tag::create('pets') sets only the objectId. Tags live in a list, and their name is a field: write
Tag::create()->name('pets').Lists and maps have two methods
Every list or map field has two variadic methods: the field name replaces the entries, add + the field name
appends to them.
Schema::object('Pet')
->properties(Schema::string('name'))
->addProperties(Schema::integer('age'));
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" }
}
}
The names are not singularized: addPaths(), addProperties(), addTags(), addServers(). Both take objects, never
associative arrays, except five fields whose values are plain data and take an array<string, …>:
Link::parameters(), Discriminator::mapping(), OAuthFlow::scopes(), Schema::dependentRequired() and
SecurityRequirement::schemes().
null resets, except for free values
Passing null to a typed setter removes the field. default(), example() and const() take any value, so null
there writes a real null. without() removes any field.
Schema::string('note')
->description('Free text')
->description(null) // removed
->default(null) // "default": null
->example('Any value');
{
"default": null,
"type": "string",
"example": "Any value"
}
$schema->without('default'); // removes the default, whatever its value
Impossible values throw at once
A value no version accepts throws an InvalidArgumentException when the method is called, not when the document is
written:
Response::status(999);
// Response::status: "999" is not a status code (100 to 599)
// or range (1XX to 5XX)
License::create()->url('https://example.com')->identifier('MIT');
// License::identifier: url and identifier are mutually exclusive
What depends on the whole document (a unique operationId, a $ref that resolves) is checked by the
validator. What depends on the version is checked when
writing.
Extensions
Every object but Reference and SecurityRequirement accepts extensions. The x- prefix is added when missing:
Info::create()
->title('Pet Store')
->version('1.0.0')
->x('logo', ['url' => 'https://example.com/logo.png']);
{
"title": "Pet Store",
"version": "1.0.0",
"x-logo": { "url": "https://example.com/logo.png" }
}
withoutX('logo') removes it.