Debian Codegraph @

vendor/zircote/swagger-php/src/Processors/MergeJsonContent.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 JsonContent into Schema and MediaType.
16 */
17 class MergeJsonContent
18 {
19 public function __invoke(Analysis $analysis): void
20 {
21 $annotations = $analysis->getAnnotationsOfType(OA\JsonContent::class);
22
23 foreach ($annotations as $jsonContent) {
24 $parent = $jsonContent->_context->nested;
25 if (!($parent instanceof OA\Response) && !($parent instanceof OA\RequestBody) && !($parent instanceof OA\Parameter)) {
26 if ($parent) {
27 $jsonContent->_context->logger->warning('Unexpected ' . $jsonContent->identity() . ' in ' . $parent->identity() . ' in ' . $parent->_context);
28 } else {
29 $jsonContent->_context->logger->warning('Unexpected ' . $jsonContent->identity() . ' must be nested');
30 }
31 continue;
32 }
33 if (Generator::isDefault($parent->content)) {
34 $parent->content = [];
35 }
36 $parent->content['application/json'] = $mediaType = new OA\MediaType([
37 'schema' => $jsonContent,
38 'example' => $jsonContent->example,
39 'examples' => $jsonContent->examples,
40 'encoding' => $jsonContent->encoding,
41 '_context' => new Context(['generated' => true], $jsonContent->_context),
42 ]);
43 $analysis->addAnnotation($mediaType, $mediaType->_context);
44 if (!$parent instanceof OA\Parameter) {
45 $parent->content['application/json']->mediaType = 'application/json';
46 }
47 $jsonContent->example = Generator::UNDEFINED;
48 $jsonContent->examples = Generator::UNDEFINED;
49 /* @phpstan-ignore assign.propertyType */
50 $jsonContent->encoding = Generator::UNDEFINED;
51 $jsonContent->_context = new Context(['nested' => $mediaType, 'generated' => true], $mediaType->_context);
52
53 $index = array_search($jsonContent, $parent->_unmerged, true);
54 if ($index !== false) {
55 array_splice($parent->_unmerged, $index, 1);
56 }
57 $analysis->removeAnnotation($jsonContent);
58 }
59 }
60 }