1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component; |
3
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcHttpServerSwaggerDoc\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 DefinitionRefResolver */ |
14
|
|
|
private $definitionRefResolver; |
15
|
|
|
/** @var TypeDocNormalizer */ |
16
|
|
|
private $typeDocNormalizer; |
17
|
|
|
/** @var ErrorDocNormalizer */ |
18
|
|
|
private $errorDocNormalizer; |
19
|
|
|
/** @var ShapeNormalizer */ |
20
|
|
|
private $shapeNormalizer; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param DefinitionRefResolver $definitionRefResolver |
24
|
|
|
* @param TypeDocNormalizer $typeDocNormalizer |
25
|
|
|
* @param ErrorDocNormalizer $errorDocNormalizer |
26
|
|
|
* @param ShapeNormalizer $shapeNormalizer |
27
|
|
|
*/ |
28
|
7 |
|
public function __construct( |
29
|
|
|
DefinitionRefResolver $definitionRefResolver, |
30
|
|
|
TypeDocNormalizer $typeDocNormalizer, |
31
|
|
|
ErrorDocNormalizer $errorDocNormalizer, |
32
|
|
|
ShapeNormalizer $shapeNormalizer |
33
|
|
|
) { |
34
|
7 |
|
$this->definitionRefResolver = $definitionRefResolver; |
35
|
7 |
|
$this->typeDocNormalizer = $typeDocNormalizer; |
36
|
7 |
|
$this->errorDocNormalizer = $errorDocNormalizer; |
37
|
7 |
|
$this->shapeNormalizer = $shapeNormalizer; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ServerDoc $doc |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
7 |
|
public function normalize(ServerDoc $doc) |
45
|
|
|
{ |
46
|
7 |
|
return array_merge( |
47
|
7 |
|
$this->getMethodsExternalSchemaList($doc), |
48
|
7 |
|
$this->getMethodErrorsExternalSchemaList($doc), |
49
|
7 |
|
$this->getServerErrorsExtraSchemaList($doc), |
50
|
7 |
|
$this->getDefaultSchemaList($doc) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ServerDoc $doc |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
7 |
|
protected function getMethodsExternalSchemaList(ServerDoc $doc) |
60
|
|
|
{ |
61
|
7 |
|
$list = []; |
62
|
7 |
|
foreach ($doc->getMethodList() as $method) { |
63
|
|
|
// Merge extra definitions |
64
|
4 |
|
$list = array_merge($list, $this->getMethodExternalSchemaList($method)); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
return $list; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param ServerDoc $doc |
72
|
|
|
* |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
7 |
|
protected function getMethodErrorsExternalSchemaList(ServerDoc $doc) |
76
|
|
|
{ |
77
|
7 |
|
$list = []; |
78
|
7 |
|
foreach ($doc->getMethodList() as $method) { |
79
|
4 |
|
$list = array_merge( |
80
|
4 |
|
$list, |
81
|
4 |
|
$this->normalizeErrorList( |
82
|
4 |
|
$method->getCustomErrorList(), |
83
|
4 |
|
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
84
|
|
|
) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
7 |
|
return $list; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param ServerDoc $doc |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
7 |
|
protected function getServerErrorsExtraSchemaList(ServerDoc $doc) |
98
|
|
|
{ |
99
|
7 |
|
return array_merge( |
100
|
7 |
|
$this->normalizeErrorList( |
101
|
7 |
|
$doc->getGlobalErrorList(), |
102
|
7 |
|
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
103
|
|
|
), |
104
|
7 |
|
$this->normalizeErrorList( |
105
|
7 |
|
$doc->getServerErrorList(), |
106
|
7 |
|
DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE |
107
|
|
|
) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param MethodDoc $method |
113
|
|
|
* |
114
|
|
|
* @return array[] |
115
|
|
|
*/ |
116
|
4 |
|
protected function getMethodExternalSchemaList(MethodDoc $method) : array |
117
|
|
|
{ |
118
|
4 |
|
$list = []; |
119
|
|
|
// Create request params schema if provided |
120
|
4 |
|
if (null !== $method->getParamsDoc()) { |
121
|
1 |
|
$key = $this->definitionRefResolver->getMethodDefinitionId( |
122
|
1 |
|
$method, |
123
|
1 |
|
DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE |
124
|
|
|
); |
125
|
1 |
|
$list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Create custom result schema if provided |
129
|
4 |
|
if (null !== $method->getResultDoc()) { |
130
|
1 |
|
$key = $this->definitionRefResolver->getMethodDefinitionId( |
131
|
1 |
|
$method, |
132
|
1 |
|
DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE |
133
|
|
|
); |
134
|
1 |
|
$list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc()); |
135
|
|
|
} |
136
|
|
|
|
137
|
4 |
|
return $list; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param ServerDoc $doc |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
7 |
|
protected function getDefaultSchemaList(ServerDoc $doc) |
145
|
|
|
{ |
146
|
|
|
$propertyList = [ |
147
|
7 |
|
'code' => [ |
148
|
|
|
'type' => 'integer', |
149
|
|
|
], |
150
|
|
|
]; |
151
|
|
|
|
152
|
7 |
|
$codeList = []; |
153
|
7 |
|
foreach ($doc->getServerErrorList() as $errorDoc) { |
154
|
2 |
|
$codeList[] = $errorDoc->getCode(); |
155
|
|
|
} |
156
|
7 |
|
foreach ($doc->getGlobalErrorList() as $errorDoc) { |
157
|
2 |
|
$codeList[] = $errorDoc->getCode(); |
158
|
|
|
} |
159
|
7 |
|
foreach ($doc->getMethodList() as $method) { |
160
|
4 |
|
foreach ($method->getCustomErrorList() as $errorDoc) { |
161
|
4 |
|
$codeList[] = $errorDoc->getCode(); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
7 |
|
$codeList = array_unique($codeList); |
166
|
7 |
|
if (count($codeList) > 0) { |
167
|
4 |
|
$propertyList['code']['enum'] = $codeList; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return [ |
171
|
|
|
'Default-Error' => [ |
172
|
|
|
'allOf' => [ |
173
|
7 |
|
$this->shapeNormalizer->getErrorShapeDefinition(), |
174
|
|
|
[ |
175
|
7 |
|
'type' => 'object', |
176
|
7 |
|
'properties' => $propertyList, |
177
|
|
|
], |
178
|
|
|
], |
179
|
|
|
], |
180
|
|
|
]; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param array $errorDocList |
185
|
|
|
* @param string $definitionType |
186
|
|
|
* |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
7 |
|
private function normalizeErrorList(array $errorDocList, $definitionType) |
190
|
|
|
{ |
191
|
7 |
|
$list = []; |
192
|
7 |
|
foreach ($errorDocList as $errorDoc) { |
193
|
4 |
|
$key = $this->definitionRefResolver->getErrorDefinitionId($errorDoc, $definitionType); |
194
|
|
|
|
195
|
4 |
|
$list[$key] = $this->errorDocNormalizer->normalize($errorDoc); |
196
|
|
|
} |
197
|
|
|
|
198
|
7 |
|
return $list; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|