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