|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver; |
|
5
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc; |
|
6
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc; |
|
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class OperationDocNormalizer |
|
11
|
|
|
*/ |
|
12
|
|
|
class OperationDocNormalizer |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var RequestDocNormalizer */ |
|
15
|
|
|
private $requestDocTransformer; |
|
16
|
|
|
/** @var ResponseDocNormalizer */ |
|
17
|
|
|
private $responseDocNormalizer; |
|
18
|
|
|
/** @var DefinitionRefResolver */ |
|
19
|
|
|
private $definitionRefResolver; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param DefinitionRefResolver $definitionRefResolver |
|
23
|
|
|
* @param RequestDocNormalizer $requestDocTransformer |
|
24
|
|
|
* @param ResponseDocNormalizer $responseDocNormalizer |
|
25
|
|
|
*/ |
|
26
|
39 |
|
public function __construct( |
|
27
|
|
|
DefinitionRefResolver $definitionRefResolver, |
|
28
|
|
|
RequestDocNormalizer $requestDocTransformer, |
|
29
|
|
|
ResponseDocNormalizer $responseDocNormalizer |
|
30
|
|
|
) { |
|
31
|
39 |
|
$this->requestDocTransformer = $requestDocTransformer; |
|
32
|
39 |
|
$this->responseDocNormalizer = $responseDocNormalizer; |
|
33
|
39 |
|
$this->definitionRefResolver = $definitionRefResolver; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param MethodDoc $method |
|
38
|
|
|
* @param ServerDoc $serverDoc |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
11 |
|
public function normalize(MethodDoc $method, ServerDoc $serverDoc) : array |
|
43
|
|
|
{ |
|
44
|
11 |
|
$self = $this; |
|
45
|
|
|
|
|
46
|
11 |
|
$extraErrorDefinitionIdRefList = array_map( |
|
47
|
11 |
|
function (ErrorDoc $errorDoc) use ($self) { |
|
48
|
1 |
|
return [ |
|
49
|
1 |
|
'$ref' => $self->definitionRefResolver->getDefinitionRef( |
|
50
|
1 |
|
$self->definitionRefResolver->getErrorDefinitionId( |
|
51
|
1 |
|
$errorDoc, |
|
52
|
1 |
|
DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE |
|
53
|
1 |
|
) |
|
54
|
1 |
|
) |
|
55
|
1 |
|
]; |
|
56
|
11 |
|
}, |
|
57
|
11 |
|
$serverDoc->getServerErrorList() |
|
58
|
11 |
|
); |
|
59
|
|
|
|
|
60
|
11 |
|
$docDescription = $docTags = []; |
|
61
|
|
|
|
|
62
|
11 |
|
if (null !== $method->getDescription()) { |
|
63
|
3 |
|
$docDescription['description'] = $method->getDescription(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
11 |
|
if (count($method->getTagList())) { |
|
67
|
3 |
|
$docTags['tags'] = $method->getTagList(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
11 |
|
return [ |
|
71
|
11 |
|
'summary' => sprintf('"%s" json-rpc method', $method->getMethodName()), |
|
72
|
11 |
|
] |
|
73
|
11 |
|
+ $docDescription |
|
74
|
11 |
|
+ $docTags |
|
75
|
11 |
|
+ [ |
|
76
|
11 |
|
'operationId' => $method->getIdentifier(), |
|
77
|
11 |
|
'requestBody' => [ |
|
78
|
11 |
|
'required' => true, |
|
79
|
11 |
|
'content' => [ |
|
80
|
11 |
|
'application/json' => [ |
|
81
|
11 |
|
'schema' => $this->requestDocTransformer->normalize($method) |
|
82
|
11 |
|
] |
|
83
|
11 |
|
] |
|
84
|
11 |
|
], |
|
85
|
11 |
|
'responses' => [ |
|
86
|
11 |
|
'200' => [ |
|
87
|
11 |
|
'description' => 'JSON-RPC response', |
|
88
|
11 |
|
'content' => [ |
|
89
|
11 |
|
'application/json' => [ |
|
90
|
11 |
|
'schema' => $this->responseDocNormalizer->normalize( |
|
91
|
11 |
|
$method, |
|
92
|
11 |
|
$extraErrorDefinitionIdRefList |
|
93
|
11 |
|
) |
|
94
|
11 |
|
], |
|
95
|
11 |
|
] |
|
96
|
11 |
|
] |
|
97
|
11 |
|
] |
|
98
|
11 |
|
] |
|
99
|
11 |
|
; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|