| 1 |
<?php declare(strict_types=1); |
| 2 |
|
| 3 |
/** |
| 4 |
* @license Apache 2.0 |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace OpenApi\Processors\Concerns; |
| 8 |
|
| 9 |
use OpenApi\Analysis; |
| 10 |
use OpenApi\Annotations as OA; |
| 11 |
use OpenApi\Context; |
| 12 |
use OpenApi\Generator; |
| 13 |
|
| 14 |
/** |
| 15 |
* Steps: |
| 16 |
* 1. Determine direct parent / interfaces / traits |
| 17 |
* 2. With each: |
| 18 |
* - traverse up inheritance tree |
| 19 |
* - inherit from first with schema; all other with scheme can be ignored |
| 20 |
* - merge from all without schema |
| 21 |
* => update all $ref that might reference a property merged. |
| 22 |
*/ |
| 23 |
trait MergePropertiesTrait |
| 24 |
{ |
| 25 |
protected function inheritFrom(Analysis $analysis, OA\Schema $schema, OA\Schema $from, string $refPath, Context $context): void |
| 26 |
{ |
| 27 |
if (Generator::isDefault($schema->allOf)) { |
| 28 |
$schema->allOf = []; |
| 29 |
} |
| 30 |
// merging other properties into allOf is done in the AugmentSchemas processor |
| 31 |
$schema->allOf[] = $refSchema = new OA\Schema([ |
| 32 |
'ref' => OA\Components::ref($refPath), |
| 33 |
'_context' => new Context(['generated' => true], $context), |
| 34 |
]); |
| 35 |
$analysis->addAnnotation($refSchema, $refSchema->_context); |
| 36 |
} |
| 37 |
|
| 38 |
protected function mergeProperties(OA\Schema $schema, array $from, array &$existing): void |
| 39 |
{ |
| 40 |
foreach ($from['properties'] as $context) { |
| 41 |
if (is_iterable($context->annotations)) { |
| 42 |
foreach ($context->annotations as $annotation) { |
| 43 |
if ($annotation instanceof OA\Property && !in_array($annotation->_context->property, $existing, strict: true)) { |
| 44 |
$existing[] = $annotation->_context->property; |
| 45 |
$schema->merge([$annotation], true); |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
protected function mergeMethods(OA\Schema $schema, array $from, array &$existing): void |
| 53 |
{ |
| 54 |
foreach ($from['methods'] as $context) { |
| 55 |
if (is_iterable($context->annotations)) { |
| 56 |
foreach ($context->annotations as $annotation) { |
| 57 |
if ($annotation instanceof OA\Property && !in_array($annotation->_context->property, $existing, strict: true)) { |
| 58 |
$existing[] = $annotation->_context->property; |
| 59 |
$schema->merge([$annotation], true); |
| 60 |
} |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
} |