PHP code example of on1kel / oas-builder
1. Go to this page and download the library: Download on1kel/oas-builder library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
on1kel / oas-builder example snippets
OpenApi::create()
->oas31()
->info(
Info::of('My API', '1.0.0')
)
->paths(
Paths::of()
->get('/users/{id}', $getUserOp)
)
->components(
Components::create()
->schema('User', $userSchema)
)
->toModel(); // -> Core\OpenApiDocument
$infoV1 = Info::of('Shop API', '1.0.0');
$infoV2 = $infoV1->version('2.0.0'); // $infoV1 не изменился
use On1kel\OAS\Builder\Support\ProfileProvider;
// Пример: профиль OpenAPI 3.1 или 3.2
// ProfileProvider::setDefault(new OAS31Profile());
// ProfileProvider::setDefault(new OAS32Profile());
use On1kel\OAS\Builder\OpenApi;
use On1kel\OAS\Builder\Info;
use On1kel\OAS\Builder\Paths\Paths;
use On1kel\OAS\Builder\Paths\Operation;
use On1kel\OAS\Builder\Paths\PathItem;
use On1kel\OAS\Builder\Parameters\Parameter;
use On1kel\OAS\Builder\Parameters\ParameterIn;
use On1kel\OAS\Builder\Responses\Responses;
use On1kel\OAS\Builder\Responses\Response;
use On1kel\OAS\Builder\Media\MediaType;
use On1kel\OAS\Builder\Schema\Schema;
use On1kel\OAS\Builder\Components\Components;
use On1kel\OAS\Builder\Support\ProfileProvider;
// ШАГ 0. Устанавливаем активный профиль спецификации.
// Пример для OpenAPI 3.1 (класс профиля поставляется пакетом on1kel/oas-profile-31):
// ProfileProvider::setDefault(new OAS31Profile());
// ШАГ 1. Описываем схему пользователя
$userSchema = Schema::object('User')
->tion('Внутренняя ошибка')
);
// ШАГ 5. Операция GET /users/{id}
$getUserOp = Operation::create()
->operationId('getUser')
->summary('Получить пользователя по ID')
->description('Возвращает объект пользователя')
->parameters($userIdParam) // можно несколько параметров через повторные вызовы ->parameters(...)
->responses($responses);
// ШАГ 6. Paths
$paths = Paths::of()
->get('/users/{id}', $getUserOp);
// ШАГ 7. Components (реестр переиспользуемых сущностей)
$components = Components::create()
->schema('User', $userSchema);
// ШАГ 8. Документ OpenAPI
$docBuilder = OpenApi::create()
->oas31() // или ->oas32()
->info(
Info::of('User Service API', '1.0.0')
->description('Сервис пользователей')
)
->paths($paths)
->components($components);
// ШАГ 9. Получаем core-модель
$openapiDocument = $docBuilder->toModel(); // Core\OpenApiDocument
// Дальше:
// - сериализуем $openapiDocument в JSON/YAML (через on1kel/oas-core или свою обёртку),
// - кладём в Swagger UI / ReDoc / кодогенератор клиента.
Schema::ref('#/components/schemas/User')
interface BuildsCoreModel {
public function toModel(): /* Core\Model\... */;
}
$tag = Tag::of('admin')
->extension('x-internal', true);
$api = OpenApi::create()
->oas31()
->info(Info::of('My API', '1.0.0'))
->tags($tag);