|
| 1 | +<?php |
| 2 | +namespace Yoanm\JsonRpcHttpServerSwaggerDoc\Infra\Normalizer; |
| 3 | + |
| 4 | +use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Helper\ArrayAppendHelperTrait; |
| 5 | +use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\ExternalSchemaListDocNormalizer; |
| 6 | +use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\OperationDocNormalizer; |
| 7 | +use Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc; |
| 8 | +use Yoanm\JsonRpcServerDoc\Domain\Model\TagDoc; |
| 9 | + |
| 10 | +/** |
| 11 | + * Class DocNormalizer |
| 12 | + */ |
| 13 | +class DocNormalizer |
| 14 | +{ |
| 15 | + use ArrayAppendHelperTrait; |
| 16 | + |
| 17 | + /** @var ExternalSchemaListDocNormalizer */ |
| 18 | + private $externalSchemaListDocNormalizer; |
| 19 | + /** @var OperationDocNormalizer */ |
| 20 | + private $operationDocNormalizer; |
| 21 | + |
| 22 | + /** |
| 23 | + * @param ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer |
| 24 | + * @param OperationDocNormalizer $operationDocNormalizer |
| 25 | + */ |
| 26 | + public function __construct( |
| 27 | + ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer, |
| 28 | + OperationDocNormalizer $operationDocNormalizer |
| 29 | + ) { |
| 30 | + $this->externalSchemaListDocNormalizer = $externalSchemaListDocNormalizer; |
| 31 | + $this->operationDocNormalizer = $operationDocNormalizer; |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param HttpServerDoc $doc |
| 36 | + * |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + public function normalize(HttpServerDoc $doc) |
| 40 | + { |
| 41 | + return [ |
| 42 | + 'swagger' => '2.0', |
| 43 | + ] |
| 44 | + + $this->infoArray($doc) |
| 45 | + + $this->serverArray($doc) |
| 46 | + + $this->tagsArray($doc) |
| 47 | + + $this->pathsArray($doc) |
| 48 | + + $this->externalSchemaListArray($doc) |
| 49 | + ; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * {@inheritdoc} |
| 54 | + */ |
| 55 | + protected function infoArray(HttpServerDoc $doc) |
| 56 | + { |
| 57 | + $infoArray = []; |
| 58 | + $infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray); |
| 59 | + $infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray); |
| 60 | + |
| 61 | + return $this->appendIfValueHaveSiblings('info', $infoArray); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritdoc} |
| 66 | + */ |
| 67 | + protected function serverArray(HttpServerDoc $doc) |
| 68 | + { |
| 69 | + $docArray = []; |
| 70 | + $docArray = $this->appendIfValueNotNull('host', $doc->getHost(), $docArray); |
| 71 | + $docArray = $this->appendIfValueNotNull('basePath', $doc->getBasePath(), $docArray); |
| 72 | + $docArray = $this->appendIfValueHaveSiblings('schemes', $doc->getSchemeList(), $docArray); |
| 73 | + |
| 74 | + return $docArray; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * {@inheritdoc} |
| 79 | + */ |
| 80 | + protected function tagsArray(HttpServerDoc $doc) |
| 81 | + { |
| 82 | + $self = $this; |
| 83 | + |
| 84 | + return $this->appendIfValueHaveSiblings( |
| 85 | + 'tags', |
| 86 | + array_map( |
| 87 | + function (TagDoc $tagDoc) use ($self) { |
| 88 | + return $self->convertToTagDoc($tagDoc); |
| 89 | + }, |
| 90 | + $doc->getTagList() |
| 91 | + ) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * {@inheritdoc} |
| 97 | + */ |
| 98 | + protected function pathsArray(HttpServerDoc $doc) |
| 99 | + { |
| 100 | + $paths = []; |
| 101 | + foreach ($doc->getMethodList() as $method) { |
| 102 | + $operationDoc = $this->operationDocNormalizer->normalize($method); |
| 103 | + |
| 104 | + // As JSON-RPC use only one endpoint |
| 105 | + // and openApi does not handle multiple request definition for the same endpoint |
| 106 | + // => create a fake (but usable) endpoint by using method id and '/../' |
| 107 | + $openApiHttpEndpoint = sprintf( |
| 108 | + '/%s/..%s', |
| 109 | + str_replace('/', '-', $method->getIdentifier()), |
| 110 | + $doc->getEndpoint() ?? '' |
| 111 | + ); |
| 112 | + |
| 113 | + $paths[$openApiHttpEndpoint] = ['post' => $operationDoc]; |
| 114 | + } |
| 115 | + |
| 116 | + return $this->appendIfValueHaveSiblings('paths', $paths); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @param HttpServerDoc $doc |
| 121 | + * |
| 122 | + * @return array |
| 123 | + */ |
| 124 | + protected function externalSchemaListArray(HttpServerDoc $doc) |
| 125 | + { |
| 126 | + return $this->appendIfValueHaveSiblings( |
| 127 | + 'definitions', |
| 128 | + $this->externalSchemaListDocNormalizer->normalize($doc) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + /** |
| 134 | + * @param TagDoc $tag |
| 135 | + * |
| 136 | + * @return array |
| 137 | + */ |
| 138 | + private function convertToTagDoc(TagDoc $tag) |
| 139 | + { |
| 140 | + $tagArray = ['name' => $tag->getName()]; |
| 141 | + |
| 142 | + $tagArray = $this->appendIfValueNotNull('description', $tag->getDescription(), $tagArray); |
| 143 | + |
| 144 | + return $tagArray; |
| 145 | + } |
| 146 | +} |
0 commit comments