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