forked from byte-it/openapi-spec-generator
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathComponentsContainer.php
118 lines (96 loc) · 3.44 KB
/
ComponentsContainer.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace LaravelJsonApi\OpenApiSpec;
use GoldSpecDigital\ObjectOrientedOAS\Contracts\SchemaContract;
use GoldSpecDigital\ObjectOrientedOAS\Objects\BaseObject;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Components;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\RequestBody;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Response;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use GoldSpecDigital\ObjectOrientedOAS\Objects\SecurityScheme;
class ComponentsContainer
{
protected array $schemas = [];
protected array $requestBodies = [];
protected array $parameters = [];
protected array $responses = [];
/**
* @param Schema $schema
* @return Schema
*/
public function addSchema(SchemaContract $schema): SchemaContract
{
$this->schemas[$schema->objectId] = $schema;
return $this->ref($schema);
}
public function getSchema(string $objectId): ?Schema
{
return isset($this->schemas[$objectId]) ? $this->ref($this->schemas[$objectId]) : null;
}
public function addRequestBody(RequestBody $requestBody): BaseObject
{
$this->requestBodies[$requestBody->objectId] = $requestBody;
return $this->ref($requestBody);
}
public function getRequestBody(string $objectId): ?BaseObject
{
return $this->requestBodies[$objectId] ?? null;
}
public function addParameter(Parameter $parameter): BaseObject
{
$this->parameters[$parameter->objectId] = $parameter;
return $this->ref($parameter);
}
public function getParameter(string $objectId): ?BaseObject
{
return $this->parameters[$objectId] ?? null;
}
public function addResponse(Response $response): BaseObject
{
$this->responses[$response->objectId] = $response;
return Response::ref('#/components/responses/'.$response->objectId,
$response->objectId)->statusCode($response->statusCode);
}
public function getResponse(string $objectId): ?BaseObject
{
return $this->responses[$objectId] ?? null;
}
public function components(): Components
{
$schemas = collect($this->schemas)
->sortBy(fn (BaseObject $schema) => $schema->objectId)
->toArray();
return Components::create()
->responses(...$this->responses)
->parameters(...$this->parameters)
->requestBodies(...$this->requestBodies)
->schemas(...array_values($schemas));
}
/**
* @return mixed
*/
protected function ref(BaseObject $object): BaseObject
{
switch (true) {
case $object instanceof Parameter:
$baseRef = '#/components/parameters/';
break;
case $object instanceof RequestBody:
$baseRef = '#/components/requestBodies/';
break;
case $object instanceof Response:
$baseRef = '#/components/responses/';
break;
case $object instanceof SchemaContract:
$baseRef = '#/components/schemas/';
break;
case $object instanceof SecurityScheme:
$baseRef = '#/components/securitySchemes/';
break;
default:
exit(get_class($object));
}
return $object::ref($baseRef.$object->objectId,
$object->objectId);
}
}