MergeJsonContent.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApi\Processors;
  6. use OpenApi\Annotations\MediaType;
  7. use OpenApi\Annotations\JsonContent;
  8. use OpenApi\Annotations\Response;
  9. use OpenApi\Annotations\RequestBody;
  10. use OpenApi\Annotations\Parameter;
  11. use OpenApi\Analysis;
  12. use OpenApi\Context;
  13. use OpenApi\Logger;
  14. /**
  15. * Split JsonContent into Schema and MediaType
  16. */
  17. class MergeJsonContent
  18. {
  19. public function __invoke(Analysis $analysis)
  20. {
  21. $annotations = $analysis->getAnnotationsOfType(JsonContent::class);
  22. foreach ($annotations as $jsonContent) {
  23. $parent = $jsonContent->_context->nested;
  24. if (!($parent instanceof Response) && !($parent instanceof RequestBody) && !($parent instanceof Parameter)) {
  25. if ($parent) {
  26. Logger::notice('Unexpected '.$jsonContent->identity() .' in ' . $parent->identity() . ' in ' . $this->_context);
  27. } else {
  28. Logger::notice('Unexpected '.$jsonContent->identity() .' must be nested');
  29. }
  30. continue;
  31. }
  32. if ($parent->content === UNDEFINED) {
  33. $parent->content = [];
  34. }
  35. $parent->content['application/json'] = new MediaType(
  36. [
  37. 'mediaType' => 'application/json',
  38. 'schema' => $jsonContent,
  39. 'example' => $jsonContent->example,
  40. 'examples' => $jsonContent->examples,
  41. '_context' => new Context(['generated' => true], $jsonContent->_context)
  42. ]
  43. );
  44. $jsonContent->example = UNDEFINED;
  45. $jsonContent->examples = UNDEFINED;
  46. $index = array_search($jsonContent, $parent->_unmerged, true);
  47. if ($index !== false) {
  48. array_splice($parent->_unmerged, $index, 1);
  49. }
  50. }
  51. }
  52. }