|
1
|
|
|
<?php |
|
2
|
|
|
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component; |
|
3
|
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Helper\ArrayAppendHelperTrait; |
|
5
|
|
|
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver; |
|
6
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc; |
|
7
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class ResponseDocNormalizer |
|
11
|
|
|
*/ |
|
12
|
|
|
class ResponseDocNormalizer |
|
13
|
|
|
{ |
|
14
|
|
|
use ArrayAppendHelperTrait; |
|
15
|
|
|
|
|
16
|
|
|
/** @var DefinitionRefResolver */ |
|
17
|
|
|
private $definitionRefResolver; |
|
18
|
|
|
/** @var ShapeNormalizer */ |
|
19
|
|
|
private $shapeNormalizer; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param DefinitionRefResolver $definitionRefResolver |
|
23
|
|
|
* @param ShapeNormalizer $shapeNormalizer |
|
24
|
|
|
*/ |
|
25
|
38 |
|
public function __construct( |
|
26
|
|
|
DefinitionRefResolver $definitionRefResolver, |
|
27
|
|
|
ShapeNormalizer $shapeNormalizer |
|
28
|
|
|
) { |
|
29
|
38 |
|
$this->definitionRefResolver = $definitionRefResolver; |
|
30
|
38 |
|
$this->shapeNormalizer = $shapeNormalizer; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param MethodDoc $method |
|
35
|
|
|
* @param array $extraErrorDefinitionIdRefList |
|
36
|
|
|
* |
|
37
|
|
|
* @return array |
|
38
|
|
|
*/ |
|
39
|
10 |
|
public function normalize(MethodDoc $method, array $extraErrorDefinitionIdRefList = []) : array |
|
40
|
|
|
{ |
|
41
|
10 |
|
$responseErrorShape = []; |
|
42
|
10 |
|
$errorArrayDoc = $this->getMethodErrorArrayDoc($method, $extraErrorDefinitionIdRefList); |
|
43
|
10 |
|
if (count($errorArrayDoc) > 0) { |
|
44
|
10 |
|
$responseErrorShape = [ |
|
45
|
10 |
|
'type' => 'object', |
|
46
|
10 |
|
'properties' => ['error' => $errorArrayDoc], |
|
47
|
10 |
|
]; |
|
48
|
|
|
} |
|
49
|
10 |
|
return [ |
|
50
|
10 |
|
'allOf' => [ |
|
51
|
10 |
|
$this->shapeNormalizer->getResponseShapeDefinition(), |
|
52
|
10 |
|
[ |
|
53
|
10 |
|
'type' => 'object', |
|
54
|
10 |
|
'properties' => ['result' => $this->getMethodResultArrayDoc($method)], |
|
55
|
10 |
|
], |
|
56
|
10 |
|
$responseErrorShape, |
|
57
|
10 |
|
], |
|
58
|
10 |
|
]; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param MethodDoc $method |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
10 |
|
protected function getMethodResultArrayDoc(MethodDoc $method) : array |
|
67
|
|
|
{ |
|
68
|
10 |
|
if (null !== $method->getResultDoc()) { |
|
69
|
3 |
|
$result = [ |
|
70
|
3 |
|
'$ref' => $this->definitionRefResolver->getDefinitionRef( |
|
71
|
3 |
|
$this->definitionRefResolver->getMethodDefinitionId( |
|
72
|
3 |
|
$method, |
|
73
|
3 |
|
DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE |
|
74
|
3 |
|
) |
|
75
|
3 |
|
) |
|
76
|
3 |
|
]; |
|
77
|
|
|
} else { |
|
78
|
7 |
|
$result = ['description' => 'Method result']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
10 |
|
return $result; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param MethodDoc $method |
|
86
|
|
|
* @param string[] $extraErrorDefinitionIdRefList |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
10 |
|
protected function getMethodErrorArrayDoc(MethodDoc $method, array $extraErrorDefinitionIdRefList = []) : array |
|
91
|
|
|
{ |
|
92
|
10 |
|
$self = $this; |
|
93
|
|
|
|
|
94
|
10 |
|
$errorDocList = array_merge( |
|
95
|
10 |
|
array_map( |
|
96
|
10 |
|
function ($errorRef) use ($self) { |
|
97
|
3 |
|
$errorDoc = new ErrorDoc('', 0, null, null, $errorRef); |
|
98
|
3 |
|
return [ |
|
99
|
3 |
|
'$ref' => $self->definitionRefResolver->getDefinitionRef( |
|
100
|
3 |
|
$self->definitionRefResolver->getErrorDefinitionId( |
|
101
|
3 |
|
$errorDoc, |
|
102
|
3 |
|
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
|
103
|
3 |
|
) |
|
104
|
3 |
|
) |
|
105
|
3 |
|
]; |
|
106
|
10 |
|
}, |
|
107
|
10 |
|
$method->getGlobalErrorRefList() |
|
108
|
10 |
|
), |
|
109
|
10 |
|
array_map( |
|
110
|
10 |
|
function (ErrorDoc $errorDoc) use ($self) { |
|
111
|
3 |
|
return [ |
|
112
|
3 |
|
'$ref' => $self->definitionRefResolver->getDefinitionRef( |
|
113
|
3 |
|
$self->definitionRefResolver->getErrorDefinitionId( |
|
114
|
3 |
|
$errorDoc, |
|
115
|
3 |
|
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE |
|
116
|
3 |
|
) |
|
117
|
3 |
|
) |
|
118
|
3 |
|
]; |
|
119
|
10 |
|
}, |
|
120
|
10 |
|
$method->getCustomErrorList() |
|
121
|
10 |
|
), |
|
122
|
10 |
|
$extraErrorDefinitionIdRefList |
|
123
|
10 |
|
); |
|
124
|
|
|
|
|
125
|
10 |
|
if (count($errorDocList) > 0) { |
|
126
|
5 |
|
return ['oneOf' => $errorDocList]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
5 |
|
return ['type' => 'object']; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|