|
| 1 | +<?php |
| 2 | +namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component; |
| 3 | + |
| 4 | +use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver; |
| 5 | +use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc; |
| 6 | +use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc; |
| 7 | + |
| 8 | +/** |
| 9 | + * Class ExternalSchemaListDocNormalizer |
| 10 | + */ |
| 11 | +class ExternalSchemaListDocNormalizer |
| 12 | +{ |
| 13 | + /** @var TypeDocNormalizer */ |
| 14 | + private $typeDocNormalizer; |
| 15 | + /** @var DefinitionRefResolver */ |
| 16 | + private $definitionRefResolver; |
| 17 | + /** @var ErrorDocNormalizer */ |
| 18 | + private $errorDocNormalizer; |
| 19 | + |
| 20 | + /** |
| 21 | + * @param DefinitionRefResolver $definitionRefResolver |
| 22 | + * @param TypeDocNormalizer $typeDocNormalizer |
| 23 | + * @param ErrorDocNormalizer $errorDocNormalizer |
| 24 | + */ |
| 25 | + public function __construct( |
| 26 | + DefinitionRefResolver $definitionRefResolver, |
| 27 | + TypeDocNormalizer $typeDocNormalizer, |
| 28 | + ErrorDocNormalizer $errorDocNormalizer |
| 29 | + ) { |
| 30 | + $this->definitionRefResolver = $definitionRefResolver; |
| 31 | + $this->typeDocNormalizer = $typeDocNormalizer; |
| 32 | + $this->errorDocNormalizer = $errorDocNormalizer; |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * @param ServerDoc $doc |
| 37 | + * @return array |
| 38 | + */ |
| 39 | + public function normalize(ServerDoc $doc) |
| 40 | + { |
| 41 | + return array_merge( |
| 42 | + $this->getMethodsExternalSchemaList($doc), |
| 43 | + $this->getMethodErrorsExternalSchemaList($doc), |
| 44 | + $this->getServerErrorsExtraSchemaList($doc) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param ServerDoc $doc |
| 50 | + * |
| 51 | + * @return array |
| 52 | + */ |
| 53 | + protected function getMethodsExternalSchemaList(ServerDoc $doc) |
| 54 | + { |
| 55 | + $list = []; |
| 56 | + foreach ($doc->getMethodList() as $method) { |
| 57 | + // Merge extra definitions |
| 58 | + $list = array_merge($list, $this->getMethodExternalSchemaList($method)); |
| 59 | + } |
| 60 | + |
| 61 | + return $list; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param ServerDoc $doc |
| 66 | + * |
| 67 | + * @return array |
| 68 | + */ |
| 69 | + protected function getMethodErrorsExternalSchemaList(ServerDoc $doc) |
| 70 | + { |
| 71 | + $list = []; |
| 72 | + foreach ($doc->getMethodList() as $method) { |
| 73 | + foreach ($method->getCustomErrorList() as $errorDoc) { |
| 74 | + $key = $this->definitionRefResolver->getErrorDefinitionId( |
| 75 | + $errorDoc, |
| 76 | + DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
| 77 | + ); |
| 78 | + $list[$key] = $this->errorDocNormalizer->normalize($errorDoc); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + return $list; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * @return array |
| 88 | + */ |
| 89 | + protected function getServerErrorsExtraSchemaList(ServerDoc $doc) |
| 90 | + { |
| 91 | + $list = []; |
| 92 | + foreach ($doc->getGlobalErrorList() as $errorDoc) { |
| 93 | + $key = $this->definitionRefResolver->getErrorDefinitionId( |
| 94 | + $errorDoc, |
| 95 | + DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
| 96 | + ); |
| 97 | + $list[$key] = $this->errorDocNormalizer->normalize($errorDoc); |
| 98 | + } |
| 99 | + |
| 100 | + foreach ($doc->getServerErrorList() as $errorDoc) { |
| 101 | + $key = $this->definitionRefResolver->getErrorDefinitionId( |
| 102 | + $errorDoc, |
| 103 | + DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE |
| 104 | + ); |
| 105 | + $list[$key] = $this->errorDocNormalizer->normalize($errorDoc); |
| 106 | + } |
| 107 | + |
| 108 | + return $list; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param MethodDoc $method |
| 113 | + * |
| 114 | + * @return array[] |
| 115 | + */ |
| 116 | + protected function getMethodExternalSchemaList(MethodDoc $method) : array |
| 117 | + { |
| 118 | + $list = []; |
| 119 | + // Create request params schema if provided |
| 120 | + if (null !== $method->getParamsDoc()) { |
| 121 | + $key = $this->definitionRefResolver->getMethodDefinitionId( |
| 122 | + $method, |
| 123 | + DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE |
| 124 | + ); |
| 125 | + $list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc()); |
| 126 | + } |
| 127 | + |
| 128 | + // Create custom result schema if provided |
| 129 | + if (null !== $method->getResultDoc()) { |
| 130 | + $key = $this->definitionRefResolver->getMethodDefinitionId( |
| 131 | + $method, |
| 132 | + DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE |
| 133 | + ); |
| 134 | + $list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc()); |
| 135 | + } |
| 136 | + |
| 137 | + return $list; |
| 138 | + } |
| 139 | +} |
0 commit comments