Skip to content

Commit 0e1864d

Browse files
authored
Add DocNormalizer (#15)
1 parent ec2067f commit 0e1864d

File tree

3 files changed

+353
-41
lines changed

3 files changed

+353
-41
lines changed

src/App/Normalizer/Component/TypeDocNormalizer.php

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component;
33

44
use phpDocumentor\Reflection\Type;
5+
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Helper\ArrayAppendHelperTrait;
56
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\ArrayDoc;
67
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\CollectionDoc;
78
use Yoanm\JsonRpcServerDoc\Domain\Model\Type\NumberDoc;
@@ -14,6 +15,8 @@
1415
*/
1516
class TypeDocNormalizer
1617
{
18+
use ArrayAppendHelperTrait;
19+
1720
/** @var SchemaTypeNormalizer */
1821
private $schemaTypeNormalizer;
1922

@@ -200,45 +203,4 @@ protected function appendNumberMinMax(NumberDoc $doc, array $paramDocMinMax)
200203

201204
return $paramDocMinMax;
202205
}
203-
204-
/**
205-
* @param string $key
206-
* @param mixed $value
207-
* @param array $doc
208-
*
209-
* @return array
210-
*/
211-
private function appendIfValueHaveSiblings(string $key, array $value, array $doc = [])
212-
{
213-
return $this->appendIf((count($value) > 0), $key, $value, $doc);
214-
}
215-
216-
/**
217-
* @param string $key
218-
* @param mixed $value
219-
* @param array $doc
220-
*
221-
* @return array
222-
*/
223-
private function appendIfValueNotNull(string $key, $value, array $doc = [])
224-
{
225-
return $this->appendIf((null !== $value), $key, $value, $doc);
226-
}
227-
228-
/**
229-
* @param bool $doAppend
230-
* @param string $key
231-
* @param mixed $value
232-
* @param array $doc
233-
*
234-
* @return array
235-
*/
236-
private function appendIf(bool $doAppend, string $key, $value, array $doc = [])
237-
{
238-
if (true === $doAppend) {
239-
$doc[$key] = $value;
240-
}
241-
242-
return $doc;
243-
}
244206
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
namespace Yoanm\JsonRpcHttpServerSwaggerDoc\Infra\Normalizer;
3+
4+
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Helper\ArrayAppendHelperTrait;
5+
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\ExternalSchemaListDocNormalizer;
6+
use Yoanm\JsonRpcHttpServerSwaggerDoc\App\Normalizer\Component\OperationDocNormalizer;
7+
use Yoanm\JsonRpcServerDoc\Domain\Model\HttpServerDoc;
8+
use Yoanm\JsonRpcServerDoc\Domain\Model\TagDoc;
9+
10+
/**
11+
* Class DocNormalizer
12+
*/
13+
class DocNormalizer
14+
{
15+
use ArrayAppendHelperTrait;
16+
17+
/** @var ExternalSchemaListDocNormalizer */
18+
private $externalSchemaListDocNormalizer;
19+
/** @var OperationDocNormalizer */
20+
private $operationDocNormalizer;
21+
22+
/**
23+
* @param ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer
24+
* @param OperationDocNormalizer $operationDocNormalizer
25+
*/
26+
public function __construct(
27+
ExternalSchemaListDocNormalizer $externalSchemaListDocNormalizer,
28+
OperationDocNormalizer $operationDocNormalizer
29+
) {
30+
$this->externalSchemaListDocNormalizer = $externalSchemaListDocNormalizer;
31+
$this->operationDocNormalizer = $operationDocNormalizer;
32+
}
33+
34+
/**
35+
* @param HttpServerDoc $doc
36+
*
37+
* @return array
38+
*/
39+
public function normalize(HttpServerDoc $doc)
40+
{
41+
return [
42+
'swagger' => '2.0',
43+
]
44+
+ $this->infoArray($doc)
45+
+ $this->serverArray($doc)
46+
+ $this->tagsArray($doc)
47+
+ $this->pathsArray($doc)
48+
+ $this->externalSchemaListArray($doc)
49+
;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
protected function infoArray(HttpServerDoc $doc)
56+
{
57+
$infoArray = [];
58+
$infoArray = $this->appendIfValueNotNull('title', $doc->getName(), $infoArray);
59+
$infoArray = $this->appendIfValueNotNull('version', $doc->getVersion(), $infoArray);
60+
61+
return $this->appendIfValueHaveSiblings('info', $infoArray);
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
protected function serverArray(HttpServerDoc $doc)
68+
{
69+
$docArray = [];
70+
$docArray = $this->appendIfValueNotNull('host', $doc->getHost(), $docArray);
71+
$docArray = $this->appendIfValueNotNull('basePath', $doc->getBasePath(), $docArray);
72+
$docArray = $this->appendIfValueHaveSiblings('schemes', $doc->getSchemeList(), $docArray);
73+
74+
return $docArray;
75+
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
protected function tagsArray(HttpServerDoc $doc)
81+
{
82+
$self = $this;
83+
84+
return $this->appendIfValueHaveSiblings(
85+
'tags',
86+
array_map(
87+
function (TagDoc $tagDoc) use ($self) {
88+
return $self->convertToTagDoc($tagDoc);
89+
},
90+
$doc->getTagList()
91+
)
92+
);
93+
}
94+
95+
/**
96+
* {@inheritdoc}
97+
*/
98+
protected function pathsArray(HttpServerDoc $doc)
99+
{
100+
$paths = [];
101+
foreach ($doc->getMethodList() as $method) {
102+
$operationDoc = $this->operationDocNormalizer->normalize($method);
103+
104+
// As JSON-RPC use only one endpoint
105+
// and openApi does not handle multiple request definition for the same endpoint
106+
// => create a fake (but usable) endpoint by using method id and '/../'
107+
$openApiHttpEndpoint = sprintf(
108+
'/%s/..%s',
109+
str_replace('/', '-', $method->getIdentifier()),
110+
$doc->getEndpoint() ?? ''
111+
);
112+
113+
$paths[$openApiHttpEndpoint] = ['post' => $operationDoc];
114+
}
115+
116+
return $this->appendIfValueHaveSiblings('paths', $paths);
117+
}
118+
119+
/**
120+
* @param HttpServerDoc $doc
121+
*
122+
* @return array
123+
*/
124+
protected function externalSchemaListArray(HttpServerDoc $doc)
125+
{
126+
return $this->appendIfValueHaveSiblings(
127+
'definitions',
128+
$this->externalSchemaListDocNormalizer->normalize($doc)
129+
);
130+
}
131+
132+
133+
/**
134+
* @param TagDoc $tag
135+
*
136+
* @return array
137+
*/
138+
private function convertToTagDoc(TagDoc $tag)
139+
{
140+
$tagArray = ['name' => $tag->getName()];
141+
142+
$tagArray = $this->appendIfValueNotNull('description', $tag->getDescription(), $tagArray);
143+
144+
return $tagArray;
145+
}
146+
}

0 commit comments

Comments
 (0)