1
|
|
|
<?php |
2
|
|
|
namespace Yoanm\JsonRpcServerDoc\Infra\Normalizer; |
3
|
|
|
|
4
|
|
|
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class MethodDocNormalizer |
8
|
|
|
*/ |
9
|
|
|
class MethodDocNormalizer |
10
|
|
|
{ |
11
|
|
|
/** @var TypeDocNormalizer */ |
12
|
|
|
private $typeDocNormalizer; |
13
|
|
|
/** @var ErrorDocNormalizer */ |
14
|
|
|
private $errorDocNormalizer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param TypeDocNormalizer $typeDocNormalizer |
18
|
|
|
* @param ErrorDocNormalizer $errorDocNormalizer |
19
|
|
|
*/ |
20
|
24 |
|
public function __construct( |
21
|
|
|
TypeDocNormalizer $typeDocNormalizer, |
22
|
|
|
ErrorDocNormalizer $errorDocNormalizer |
23
|
|
|
) { |
24
|
24 |
|
$this->typeDocNormalizer = $typeDocNormalizer; |
25
|
24 |
|
$this->errorDocNormalizer = $errorDocNormalizer; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param MethodDoc $doc |
30
|
|
|
* |
31
|
|
|
* @return array |
32
|
|
|
* |
33
|
|
|
* @throws \ReflectionException |
34
|
|
|
*/ |
35
|
16 |
|
public function normalize(MethodDoc $doc) : array |
36
|
|
|
{ |
37
|
16 |
|
$docDescription = $docTags = $paramsSchema = $responseSchema = []; |
38
|
|
|
|
39
|
16 |
|
if (null !== $doc->getDescription()) { |
40
|
6 |
|
$docDescription['description'] = $doc->getDescription(); |
41
|
|
|
} |
42
|
16 |
|
if (count($doc->getTagList())) { |
43
|
2 |
|
$docTags['tags'] = $doc->getTagList(); |
44
|
|
|
} |
45
|
16 |
|
if (null !== $doc->getParamsDoc()) { |
46
|
3 |
|
$paramsSchema = [ |
47
|
3 |
|
'params' => $this->typeDocNormalizer->normalize($doc->getParamsDoc()) |
48
|
3 |
|
]; |
49
|
|
|
} |
50
|
|
|
// Create custom result schema only if provided |
51
|
16 |
|
if (null !== $doc->getResultDoc()) { |
52
|
3 |
|
$responseSchema['result'] = $this->typeDocNormalizer->normalize($doc->getResultDoc()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
16 |
|
return [ |
57
|
16 |
|
'identifier' => $doc->getIdentifier(), |
58
|
16 |
|
'name' =>$doc->getMethodName(), |
59
|
16 |
|
] |
60
|
16 |
|
+ $docDescription |
61
|
16 |
|
+ $docTags |
62
|
16 |
|
+ $paramsSchema |
63
|
16 |
|
+ $responseSchema |
64
|
16 |
|
+ $this->appendErrorsSchema($doc) |
65
|
16 |
|
; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param MethodDoc $docObject |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
16 |
|
private function appendErrorsSchema(MethodDoc $docObject) : array |
74
|
|
|
{ |
75
|
16 |
|
$docArray = []; |
76
|
|
|
// Create custom result schema only if provided |
77
|
16 |
|
if (count($docObject->getCustomErrorList()) || count($docObject->getGlobalErrorRefList())) { |
78
|
8 |
|
$docArray['errors'] = array_merge( |
79
|
8 |
|
array_map( |
80
|
8 |
|
[$this->errorDocNormalizer, 'normalize'], |
81
|
8 |
|
$docObject->getCustomErrorList() |
82
|
8 |
|
), |
83
|
8 |
|
array_map( |
84
|
8 |
|
function ($errorIdentifier) { |
85
|
6 |
|
return ['$ref' => sprintf('#/errors/%s', $errorIdentifier)]; |
86
|
8 |
|
}, |
87
|
8 |
|
$docObject->getGlobalErrorRefList() |
88
|
8 |
|
) |
89
|
8 |
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
16 |
|
return $docArray; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|