prolab-api/vendor/zircote/swagger-php/src/Processors/MergeIntoOpenApi.php

57 lines
1.8 KiB
PHP
Raw Normal View History

2025-09-24 06:24:52 +00:00
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
2025-10-04 09:16:19 +00:00
use OpenApi\Annotations\AbstractAnnotation;
use OpenApi\Annotations\OpenApi;
2025-09-24 06:24:52 +00:00
use OpenApi\Context;
use OpenApi\Generator;
/**
* Merge all @OA\OpenApi annotations into one.
*/
2025-10-04 09:16:19 +00:00
class MergeIntoOpenApi
2025-09-24 06:24:52 +00:00
{
public function __invoke(Analysis $analysis)
{
// Auto-create the OpenApi annotation.
if (!$analysis->openapi) {
$context = new Context([], $analysis->context);
2025-10-04 09:16:19 +00:00
$analysis->addAnnotation(new OpenApi(['_context' => $context]), $context);
2025-09-24 06:24:52 +00:00
}
$openapi = $analysis->openapi;
$openapi->_analysis = $analysis;
// Merge annotations into the target openapi
$merge = [];
2025-10-04 09:16:19 +00:00
/** @var AbstractAnnotation $annotation */
2025-09-24 06:24:52 +00:00
foreach ($analysis->annotations as $annotation) {
if ($annotation === $openapi) {
continue;
}
2025-10-04 09:16:19 +00:00
if ($annotation instanceof OpenApi) {
2025-09-24 06:24:52 +00:00
$paths = $annotation->paths;
unset($annotation->paths);
$openapi->mergeProperties($annotation);
2025-10-04 09:16:19 +00:00
if ($paths !== Generator::UNDEFINED) {
2025-09-24 06:24:52 +00:00
foreach ($paths as $path) {
2025-10-04 09:16:19 +00:00
if ($openapi->paths === Generator::UNDEFINED) {
2025-09-24 06:24:52 +00:00
$openapi->paths = [];
}
$openapi->paths[] = $path;
}
}
2025-10-04 09:16:19 +00:00
} elseif (OpenApi::matchNested(get_class($annotation)) && property_exists($annotation, '_context') && $annotation->_context->is('nested') === false) {
2025-09-24 06:24:52 +00:00
// A top level annotation.
$merge[] = $annotation;
}
}
$openapi->merge($merge, true);
}
}