| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Processors; |
| 8 |
|
| 9 |
use OpenApi\Analysis; |
| 10 |
use OpenApi\Annotations as OA; |
| 11 |
use OpenApi\Generator; |
| 12 |
|
| 13 |
/** |
| 14 |
* Iterate over the chain of ancestors of a schema and: |
| 15 |
* - if the ancestor has a schema |
| 16 |
* => inherit from the ancestor if it has a schema (allOf) and stop. |
| 17 |
* - else |
| 18 |
* => merge ancestor properties into the schema. |
| 19 |
*/ |
| 20 |
class ExpandClasses |
| 21 |
{ |
| 22 |
use Concerns\MergePropertiesTrait; |
| 23 |
|
| 24 |
public function __invoke(Analysis $analysis): void |
| 25 |
{ |
| 26 |
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); |
| 27 |
|
| 28 |
foreach ($schemas as $schema) { |
| 29 |
if ($schema->_context->is('class')) { |
| 30 |
$ancestors = $analysis->getSuperClasses($schema->_context->fullyQualifiedName($schema->_context->class)); |
| 31 |
$existing = []; |
| 32 |
foreach ($ancestors as $ancestor) { |
| 33 |
$ancestorSchema = $analysis->getAnnotationForSource($ancestor['context']->fullyQualifiedName($ancestor['class'])); |
| 34 |
if ($ancestorSchema) { |
| 35 |
$refPath = Generator::isDefault($ancestorSchema->schema) ? $ancestor['class'] : $ancestorSchema->schema; |
| 36 |
$this->inheritFrom($analysis, $schema, $ancestorSchema, $refPath, $ancestor['context']); |
| 37 |
|
| 38 |
// one ancestor is enough |
| 39 |
break; |
| 40 |
} else { |
| 41 |
$this->mergeMethods($schema, $ancestor, $existing); |
| 42 |
$this->mergeProperties($schema, $ancestor, $existing); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |