|
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
|
46 |
|
public function __construct( |
|
27
|
|
|
ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer, |
|
28
|
|
|
OperationDocNormalizer $operationDocNormalizer |
|
29
|
|
|
) { |
|
30
|
46 |
|
$this->externalSchemaListDocNormalizer = $externalSchemaListDocNormalizer; |
|
31
|
46 |
|
$this->operationDocNormalizer = $operationDocNormalizer; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param HttpServerDoc $doc |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
* |
|
39
|
|
|
* @throws \ReflectionException |
|
40
|
|
|
*/ |
|
41
|
46 |
|
public function normalize(HttpServerDoc $doc) : array |
|
42
|
|
|
{ |
|
43
|
46 |
|
return [ |
|
44
|
46 |
|
'swagger' => '2.0', |
|
45
|
46 |
|
] |
|
46
|
46 |
|
+ $this->infoArray($doc) |
|
47
|
46 |
|
+ $this->serverArray($doc) |
|
48
|
46 |
|
+ $this->tagsArray($doc) |
|
49
|
46 |
|
+ $this->pathsArray($doc) |
|
50
|
46 |
|
+ $this->externalSchemaListArray($doc) |
|
51
|
46 |
|
; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param HttpServerDoc $doc |
|
56
|
|
|
* |
|
57
|
|
|
* @return array |
|
58
|
|
|
*/ |
|
59
|
46 |
|
protected function infoArray(HttpServerDoc $doc) : array |
|
60
|
|
|
{ |
|
61
|
46 |
|
$infoArray = []; |
|
62
|
46 |
|
$infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray); |
|
63
|
46 |
|
$infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray); |
|
64
|
|
|
|
|
65
|
46 |
|
return $this->appendIfValueHaveSiblings('info', $infoArray); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param HttpServerDoc $doc |
|
70
|
|
|
* |
|
71
|
|
|
* @return array |
|
72
|
|
|
*/ |
|
73
|
46 |
|
protected function serverArray(HttpServerDoc $doc) : array |
|
74
|
|
|
{ |
|
75
|
46 |
|
$docArray = []; |
|
76
|
46 |
|
$docArray = $this->appendIfValueNotNull('host', $doc->getHost(), $docArray); |
|
77
|
46 |
|
$docArray = $this->appendIfValueNotNull('basePath', $doc->getBasePath(), $docArray); |
|
78
|
46 |
|
$docArray = $this->appendIfValueHaveSiblings('schemes', $doc->getSchemeList(), $docArray); |
|
79
|
|
|
|
|
80
|
46 |
|
return $docArray; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param HttpServerDoc $doc |
|
85
|
|
|
* |
|
86
|
|
|
* @return array |
|
87
|
|
|
*/ |
|
88
|
46 |
|
protected function tagsArray(HttpServerDoc $doc) : array |
|
89
|
|
|
{ |
|
90
|
46 |
|
$self = $this; |
|
91
|
|
|
|
|
92
|
46 |
|
return $this->appendIfValueHaveSiblings( |
|
93
|
46 |
|
'tags', |
|
94
|
46 |
|
array_map( |
|
95
|
46 |
|
function (TagDoc $tagDoc) use ($self) { |
|
96
|
4 |
|
return $self->convertToTagDoc($tagDoc); |
|
97
|
46 |
|
}, |
|
98
|
46 |
|
$doc->getTagList() |
|
99
|
46 |
|
) |
|
100
|
46 |
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param HttpServerDoc $doc |
|
105
|
|
|
* |
|
106
|
|
|
* @return array |
|
107
|
|
|
*/ |
|
108
|
46 |
|
protected function pathsArray(HttpServerDoc $doc) : array |
|
109
|
|
|
{ |
|
110
|
46 |
|
$paths = []; |
|
111
|
46 |
|
foreach ($doc->getMethodList() as $method) { |
|
112
|
9 |
|
$operationDoc = $this->operationDocNormalizer->normalize($method); |
|
113
|
|
|
|
|
114
|
|
|
// As JSON-RPC use only one endpoint |
|
115
|
|
|
// and openApi does not handle multiple request definition for the same endpoint |
|
116
|
|
|
// => create a fake (but usable) endpoint by using method id and '/../' |
|
117
|
9 |
|
$openApiHttpEndpoint = sprintf( |
|
118
|
9 |
|
'/%s/..%s', |
|
119
|
9 |
|
str_replace('/', '-', $method->getIdentifier()), |
|
120
|
9 |
|
$doc->getEndpoint() ?? '' |
|
121
|
9 |
|
); |
|
122
|
|
|
|
|
123
|
9 |
|
$paths[$openApiHttpEndpoint] = ['post' => $operationDoc]; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
46 |
|
return $this->appendIfValueHaveSiblings('paths', $paths); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param HttpServerDoc $doc |
|
131
|
|
|
* |
|
132
|
|
|
* @return array |
|
133
|
|
|
* |
|
134
|
|
|
* @throws \ReflectionException |
|
135
|
|
|
*/ |
|
136
|
46 |
|
protected function externalSchemaListArray(HttpServerDoc $doc) : array |
|
137
|
|
|
{ |
|
138
|
46 |
|
return $this->appendIfValueHaveSiblings( |
|
139
|
46 |
|
'definitions', |
|
140
|
46 |
|
$this->externalSchemaListDocNormalizer->normalize($doc) |
|
141
|
46 |
|
); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param TagDoc $tag |
|
146
|
|
|
* |
|
147
|
|
|
* @return array |
|
148
|
|
|
*/ |
|
149
|
4 |
|
private function convertToTagDoc(TagDoc $tag) : array |
|
150
|
|
|
{ |
|
151
|
4 |
|
$tagArray = ['name' => $tag->getName()]; |
|
152
|
|
|
|
|
153
|
4 |
|
$tagArray = $this->appendIfValueNotNull('description', $tag->getDescription(), $tagArray); |
|
154
|
|
|
|
|
155
|
4 |
|
return $tagArray; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|