Debian Codegraph @

vendor/zircote/swagger-php/src/Processors/MergeXmlContent.php

raw
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\Context;
12 use OpenApi\Generator;
13
14 /**
15 * Split XmlContent into Schema and MediaType.
16 */
17 class MergeXmlContent
18 {
19 public function __invoke(Analysis $analysis): void
20 {
21 $annotations = $analysis->getAnnotationsOfType(OA\XmlContent::class);
22
23 foreach ($annotations as $xmlContent) {
24 $parent = $xmlContent->_context->nested;
25 if (!($parent instanceof OA\Response) && !($parent instanceof OA\RequestBody) && !($parent instanceof OA\Parameter)) {
26 if ($parent) {
27 $xmlContent->_context->logger->warning('Unexpected ' . $xmlContent->identity() . ' in ' . $parent->identity() . ' in ' . $parent->_context);
28 } else {
29 $xmlContent->_context->logger->warning('Unexpected ' . $xmlContent->identity() . ' must be nested');
30 }
31 continue;
32 }
33 if (Generator::isDefault($parent->content)) {
34 $parent->content = [];
35 }
36 $parent->content['application/xml'] = $mediaType = new OA\MediaType([
37 'schema' => $xmlContent,
38 'example' => $xmlContent->example,
39 'examples' => $xmlContent->examples,
40 '_context' => new Context(['generated' => true], $xmlContent->_context),
41 ]);
42 $analysis->addAnnotation($mediaType, $mediaType->_context);
43 if (!$parent instanceof OA\Parameter) {
44 $parent->content['application/xml']->mediaType = 'application/xml';
45 }
46 $xmlContent->example = Generator::UNDEFINED;
47 $xmlContent->examples = Generator::UNDEFINED;
48 $xmlContent->_context = new Context(['nested' => $mediaType, 'generated' => true], $mediaType->_context);
49
50 $index = array_search($xmlContent, $parent->_unmerged, true);
51 if ($index !== false) {
52 array_splice($parent->_unmerged, $index, 1);
53 }
54 $analysis->removeAnnotation($xmlContent);
55 }
56 }
57 }