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
|
|
|
*/ |
19
|
7 |
|
public function __construct( |
20
|
|
|
TypeDocNormalizer $typeDocNormalizer, |
21
|
|
|
ErrorDocNormalizer $errorDocNormalizer |
22
|
|
|
) { |
23
|
7 |
|
$this->typeDocNormalizer = $typeDocNormalizer; |
24
|
7 |
|
$this->errorDocNormalizer = $errorDocNormalizer; |
25
|
7 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param MethodDoc $doc |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
7 |
|
public function normalize(MethodDoc $doc) : array |
33
|
|
|
{ |
34
|
7 |
|
$docDescription = $docTags = $paramsSchema = $responseSchema = []; |
35
|
|
|
|
36
|
7 |
|
if (null !== $doc->getDescription()) { |
37
|
1 |
|
$docDescription['description'] = $doc->getDescription(); |
38
|
|
|
} |
39
|
7 |
|
if (count($doc->getTagList())) { |
40
|
1 |
|
$docTags['tags'] = $doc->getTagList(); |
41
|
|
|
} |
42
|
7 |
|
if (null !== $doc->getParamsDoc()) { |
43
|
|
|
$paramsSchema = [ |
44
|
1 |
|
'params' => $this->typeDocNormalizer->normalize($doc->getParamsDoc()) |
45
|
|
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Create custom result schema if provided |
49
|
7 |
|
if (null !== $doc->getResultDoc()) { |
50
|
1 |
|
$responseSchema['result'] = $this->typeDocNormalizer->normalize($doc->getResultDoc()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Create custom result schema if provided |
54
|
7 |
|
if (count($doc->getCustomErrorList()) || count($doc->getGlobalErrorRefList())) { |
55
|
2 |
|
$responseSchema['errors'] = array_merge( |
56
|
2 |
|
array_map( |
57
|
2 |
|
[$this->errorDocNormalizer, 'normalize'], |
58
|
2 |
|
$doc->getCustomErrorList() |
59
|
|
|
), |
60
|
2 |
|
array_map( |
61
|
|
|
function ($errorIdentifier) { |
62
|
1 |
|
return ['$ref' => sprintf('#/errors/%s', $errorIdentifier)]; |
63
|
2 |
|
}, |
64
|
2 |
|
$doc->getGlobalErrorRefList() |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return [ |
70
|
7 |
|
'identifier' => $doc->getIdentifier(), |
71
|
7 |
|
'name' =>$doc->getMethodName(), |
72
|
|
|
] |
73
|
7 |
|
+ $docDescription |
74
|
7 |
|
+ $docTags |
75
|
7 |
|
+ $paramsSchema |
76
|
7 |
|
+ $responseSchema |
77
|
|
|
; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|