forked from zircote/swagger-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMergeIntoOpenApi.php
53 lines (48 loc) · 1.64 KB
/
MergeIntoOpenApi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php declare(strict_types=1);
/**
* @license Apache 2.0
*/
namespace OpenApi\Processors;
use OpenApi\Analysis;
use OpenApi\Annotations\OpenApi;
use OpenApi\Context;
/**
* Merge all @OA\OpenApi annotations into one.
*/
class MergeIntoOpenApi
{
public function __invoke(Analysis $analysis)
{
// Auto-create the OpenApi annotation.
if (!$analysis->openapi) {
$context = new Context(['analysis' => $analysis]);
$analysis->addAnnotation(new OpenApi(['_context' => $context]), $context);
}
$openapi = $analysis->openapi;
$openapi->_analysis = $analysis;
// Merge annotations into the target openapi
$merge = [];
foreach ($analysis->annotations as $annotation) {
if ($annotation === $openapi) {
continue;
}
if ($annotation instanceof OpenApi) {
$paths = $annotation->paths;
unset($annotation->paths);
$openapi->mergeProperties($annotation);
if ($paths !== UNDEFINED) {
foreach ($paths as $path) {
if ($openapi->paths === UNDEFINED) {
$openapi->paths = [];
}
$openapi->paths[] = $path;
}
}
} elseif (OpenApi::matchNested(get_class($annotation)) && property_exists($annotation, '_context') && $annotation->_context->is('nested') === false) {
// A top level annotation.
$merge[] = $annotation;
}
}
$openapi->merge($merge, true);
}
}