Skip to content

Commit f79e53b

Browse files
authored
Init (#2)
1 parent 02b4c62 commit f79e53b

29 files changed

+3244
-12
lines changed

.scrutinizer.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ build:
1515
tests:
1616
stop_on_failure: true
1717
override:
18-
- php-scrutinizer-run --enable-security-analysis
19-
-
20-
command: make codestyle
21-
analysis:
22-
file: 'build/reports/cs-data'
23-
format: 'php-cs-checkstyle'
2418
-
2519
command: make coverage
2620
idle_timeout: 1200
2721
coverage:
2822
file: 'build/coverage/clover.xml'
2923
format: 'php-clover'
24+
- php-scrutinizer-run --enable-security-analysis
25+
- make codestyle
3026
cache:
3127
directories:
3228
- ~/.composer
@@ -39,8 +35,6 @@ build:
3935
COMPOSER_OPTIONS: '--optimize-autoloader'
4036
COVERAGE_OUTPUT_STYLE: 'clover'
4137
COVERAGE_CLOVER_FILE_PATH: 'build/coverage/clover.xml'
42-
PHPCS_REPORT_STYLE: 'checkstyle'
43-
PHPCS_REPORT_FILE: 'build/reports/cs-data'
4438
php:
4539
version: "7.1"
4640
timezone: UTC

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
language: php
22

33
php:
4-
- '7.0'
54
- '7.1'
65
- '7.2'
6+
- '7.3'
77

88
env:
99
global:
@@ -19,7 +19,7 @@ matrix:
1919

2020
before_install:
2121
# remove xdebug to speed up build
22-
- phpenv config-rm xdebug.ini
22+
- phpenv config-rm xdebug.ini || true
2323

2424
install:
2525
- make build

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
}
2626
},
2727
"require": {
28-
"php": ">5.5",
29-
"yoanm/jsonrpc-server-doc-sdk": "dev-feature/improve"
28+
"php": ">=7.1",
29+
"yoanm/jsonrpc-server-doc-sdk": "dev-release/1.0.0"
3030
},
3131
"require-dev": {
3232
"behat/behat": "~3.0",
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Helper;
3+
4+
/**
5+
* Class ArrayAppendHelperTrait
6+
*/
7+
trait ArrayAppendHelperTrait
8+
{
9+
/**
10+
* @param string $key
11+
* @param mixed $value
12+
* @param array $doc
13+
*
14+
* @return array
15+
*/
16+
protected function appendIfValueHaveSiblings(string $key, array $value, array $doc = [])
17+
{
18+
return $this->appendIf((count($value) > 0), $key, $value, $doc);
19+
}
20+
21+
/**
22+
* @param string $key
23+
* @param mixed $value
24+
* @param array $doc
25+
*
26+
* @return array
27+
*/
28+
protected function appendIfValueNotNull(string $key, $value, array $doc = [])
29+
{
30+
return $this->appendIf((null !== $value), $key, $value, $doc);
31+
}
32+
33+
/**
34+
* @param bool $doAppend
35+
* @param string $key
36+
* @param mixed $value
37+
* @param array $doc
38+
*
39+
* @return array
40+
*/
41+
protected function appendIf(bool $doAppend, string $key, $value, array $doc = [])
42+
{
43+
if (true === $doAppend) {
44+
$doc[$key] = $value;
45+
}
46+
47+
return $doc;
48+
}
49+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3+
4+
use Yoanm\JsonRpcServerDoc\Domain\Model\ErrorDoc;
5+
6+
/**
7+
* Class ErrorDocNormalizer
8+
*/
9+
class ErrorDocNormalizer
10+
{
11+
/** @var TypeDocNormalizer */
12+
private $typeDocNormalizer;
13+
/** @var ShapeNormalizer */
14+
private $shapeNormalizer;
15+
16+
/**
17+
* @param TypeDocNormalizer $typeDocNormalizer
18+
* @param ShapeNormalizer $shapeNormalizer
19+
*/
20+
public function __construct(
21+
TypeDocNormalizer $typeDocNormalizer,
22+
ShapeNormalizer $shapeNormalizer
23+
) {
24+
$this->typeDocNormalizer = $typeDocNormalizer;
25+
$this->shapeNormalizer = $shapeNormalizer;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function normalize(ErrorDoc $errorDoc)
32+
{
33+
$requiredDoc = ['required' => ['code']];
34+
$properties = [
35+
'code' => ['example' => $errorDoc->getCode()]
36+
];
37+
if (null !== $errorDoc->getDataDoc()) {
38+
$properties['data'] = $this->typeDocNormalizer->normalize($errorDoc->getDataDoc());
39+
if (false !== $errorDoc->getDataDoc()->isRequired()) {
40+
$requiredDoc['required'][] = 'data';
41+
}
42+
}
43+
if (null !== $errorDoc->getMessage()) {
44+
$properties['message'] = ['example' => $errorDoc->getMessage()];
45+
}
46+
47+
return [
48+
'title' => $errorDoc->getTitle(),
49+
'allOf' => [
50+
$this->shapeNormalizer->getErrorShapeDefinition(),
51+
(['type' => 'object'] + $requiredDoc + ['properties' => $properties]),
52+
],
53+
];
54+
}
55+
}
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
<?php
2+
namespace Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Normalizer\Component;
3+
4+
use Yoanm\JsonRpcHttpServerOpenAPIDoc\App\Resolver\DefinitionRefResolver;
5+
use Yoanm\JsonRpcServerDoc\Domain\Model\MethodDoc;
6+
use Yoanm\JsonRpcServerDoc\Domain\Model\ServerDoc;
7+
8+
/**
9+
* Class ExternalSchemaListDocNormalizer
10+
*/
11+
class ExternalSchemaListDocNormalizer
12+
{
13+
/** @var TypeDocNormalizer */
14+
private $typeDocNormalizer;
15+
/** @var DefinitionRefResolver */
16+
private $definitionRefResolver;
17+
/** @var ErrorDocNormalizer */
18+
private $errorDocNormalizer;
19+
20+
/**
21+
* @param DefinitionRefResolver $definitionRefResolver
22+
* @param TypeDocNormalizer $typeDocNormalizer
23+
* @param ErrorDocNormalizer $errorDocNormalizer
24+
*/
25+
public function __construct(
26+
DefinitionRefResolver $definitionRefResolver,
27+
TypeDocNormalizer $typeDocNormalizer,
28+
ErrorDocNormalizer $errorDocNormalizer
29+
) {
30+
$this->definitionRefResolver = $definitionRefResolver;
31+
$this->typeDocNormalizer = $typeDocNormalizer;
32+
$this->errorDocNormalizer = $errorDocNormalizer;
33+
}
34+
35+
/**
36+
* @param ServerDoc $doc
37+
* @return array
38+
*/
39+
public function normalize(ServerDoc $doc)
40+
{
41+
return array_merge(
42+
$this->getMethodsExternalSchemaList($doc),
43+
$this->getMethodErrorsExternalSchemaList($doc),
44+
$this->getServerErrorsExtraSchemaList($doc)
45+
);
46+
}
47+
48+
/**
49+
* @param ServerDoc $doc
50+
*
51+
* @return array
52+
*/
53+
protected function getMethodsExternalSchemaList(ServerDoc $doc)
54+
{
55+
$list = [];
56+
foreach ($doc->getMethodList() as $method) {
57+
// Merge extra definitions
58+
$list = array_merge($list, $this->getMethodExternalSchemaList($method));
59+
}
60+
61+
return $list;
62+
}
63+
64+
/**
65+
* @param ServerDoc $doc
66+
*
67+
* @return array
68+
*/
69+
protected function getMethodErrorsExternalSchemaList(ServerDoc $doc)
70+
{
71+
$list = [];
72+
foreach ($doc->getMethodList() as $method) {
73+
foreach ($method->getCustomErrorList() as $errorDoc) {
74+
$key = $this->definitionRefResolver->getErrorDefinitionId(
75+
$errorDoc,
76+
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
77+
);
78+
$list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
79+
}
80+
}
81+
82+
return $list;
83+
}
84+
85+
86+
/**
87+
* @return array
88+
*/
89+
protected function getServerErrorsExtraSchemaList(ServerDoc $doc)
90+
{
91+
$list = [];
92+
foreach ($doc->getGlobalErrorList() as $errorDoc) {
93+
$key = $this->definitionRefResolver->getErrorDefinitionId(
94+
$errorDoc,
95+
DefinitionRefResolver::CUSTOM_ERROR_DEFINITION_TYPE
96+
);
97+
$list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
98+
}
99+
100+
foreach ($doc->getServerErrorList() as $errorDoc) {
101+
$key = $this->definitionRefResolver->getErrorDefinitionId(
102+
$errorDoc,
103+
DefinitionRefResolver::SERVER_ERROR_DEFINITION_TYPE
104+
);
105+
$list[$key] = $this->errorDocNormalizer->normalize($errorDoc);
106+
}
107+
108+
return $list;
109+
}
110+
111+
/**
112+
* @param MethodDoc $method
113+
*
114+
* @return array[]
115+
*/
116+
protected function getMethodExternalSchemaList(MethodDoc $method) : array
117+
{
118+
$list = [];
119+
// Create request params schema if provided
120+
if (null !== $method->getParamsDoc()) {
121+
$key = $this->definitionRefResolver->getMethodDefinitionId(
122+
$method,
123+
DefinitionRefResolver::METHOD_PARAMS_DEFINITION_TYPE
124+
);
125+
$list[$key] = $this->typeDocNormalizer->normalize($method->getParamsDoc());
126+
}
127+
128+
// Create custom result schema if provided
129+
if (null !== $method->getResultDoc()) {
130+
$key = $this->definitionRefResolver->getMethodDefinitionId(
131+
$method,
132+
DefinitionRefResolver::METHOD_RESULT_DEFINITION_TYPE
133+
);
134+
$list[$key] = $this->typeDocNormalizer->normalize($method->getResultDoc());
135+
}
136+
137+
return $list;
138+
}
139+
}

0 commit comments

Comments
 (0)