-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFromCebe.php
More file actions
271 lines (234 loc) · 9.2 KB
/
FromCebe.php
File metadata and controls
271 lines (234 loc) · 9.2 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
declare(strict_types=1);
namespace Membrane\OpenAPIReader\Factory\V30;
use cebe\openapi\spec as Cebe;
use Membrane\OpenAPIReader\ValueObject\Partial\Header;
use Membrane\OpenAPIReader\ValueObject\Partial\MediaType;
use Membrane\OpenAPIReader\ValueObject\Partial\OpenAPI;
use Membrane\OpenAPIReader\ValueObject\Partial\Operation;
use Membrane\OpenAPIReader\ValueObject\Partial\Parameter;
use Membrane\OpenAPIReader\ValueObject\Partial\PathItem;
use Membrane\OpenAPIReader\ValueObject\Partial\RequestBody;
use Membrane\OpenAPIReader\ValueObject\Partial\Response;
use Membrane\OpenAPIReader\ValueObject\Partial\Schema;
use Membrane\OpenAPIReader\ValueObject\Partial\Server;
use Membrane\OpenAPIReader\ValueObject\Partial\ServerVariable;
use Membrane\OpenAPIReader\ValueObject\Valid\V30;
use Membrane\OpenAPIReader\ValueObject\Value;
final class FromCebe
{
public static function createOpenAPI(
Cebe\OpenApi $openApi
): V30\OpenAPI {
$servers = count($openApi->servers) === 1 && $openApi->servers[0]->url === '/' ?
[] :
$openApi->servers;
/**
* todo when phpstan 1.11 stable is released
* replace the below lines with phpstan-ignore nullsafe.neverNull
* The reason for this is the cebe library does not specify that info is nullable
* However it is not always set, so it can be null
*/
return V30\OpenAPI::fromPartial(new OpenAPI(
$openApi->openapi,
$openApi->info?->title, // @phpstan-ignore-line
$openApi->info?->version, // @phpstan-ignore-line
self::createServers($servers),
self::createPaths($openApi->paths)
));
}
/**
* @param Cebe\Server[] $servers
* @return Server[]
*/
private static function createServers(array $servers): array
{
$result = [];
foreach ($servers as $server) {
$result[] = new Server(
$server->url,
self::createServerVariables($server->variables)
);
}
return $result;
}
/**
* @param Cebe\ServerVariable[] $serverVariables
* @return ServerVariable[]
*/
private static function createServerVariables(array $serverVariables): array
{
$result = [];
foreach ($serverVariables as $name => $serverVariable) {
$result[] = new ServerVariable(
$name,
$serverVariable->default,
$serverVariable->enum,
);
}
return $result;
}
/**
* @param null|Cebe\Paths<string,Cebe\PathItem> $paths
* @return PathItem[]
*/
private static function createPaths(?Cebe\Paths $paths): array
{
$result = [];
foreach ($paths ?? [] as $path => $pathItem) {
$result[] = new PathItem(
path: $path,
servers: self::createServers($pathItem->servers),
parameters: self::createParameters($pathItem->parameters),
get: self::createOperation($pathItem->get),
put: self::createOperation($pathItem->put),
post: self::createOperation($pathItem->post),
delete: self::createOperation($pathItem->delete),
options: self::createOperation($pathItem->options),
head: self::createOperation($pathItem->head),
patch: self::createOperation($pathItem->patch),
trace: self::createOperation($pathItem->trace),
);
}
return $result;
}
/**
* @param Cebe\Parameter[]|Cebe\Reference[] $parameters
* @return Parameter[]
*/
private static function createParameters(array $parameters): array
{
$result = [];
foreach ($parameters as $parameter) {
assert(!$parameter instanceof Cebe\Reference);
$result[] = new Parameter(
$parameter->name,
$parameter->in,
$parameter->required,
$parameter->style,
$parameter->explode,
self::createSchema($parameter->schema),
self::createContent($parameter->content),
);
}
return $result;
}
private static function createSchema(
Cebe\Reference|Cebe\Schema|null $schema
): Schema|null {
assert(!$schema instanceof Cebe\Reference);
if ($schema === null) {
return null;
}
$createSchemas = fn($schemas) => array_filter(
array_map(fn($s) => self::createSchema($s), $schemas),
fn($s) => $s !== null,
);
return new Schema(
type: $schema->type,
enum: isset($schema->enum) ?
array_map(fn($e) => new Value($e), $schema->enum) :
null,
default: isset($schema->default) ? new Value($schema->default) : null,
nullable: $schema->nullable ?? false,
multipleOf: $schema->multipleOf ?? null,
exclusiveMaximum: $schema->exclusiveMaximum ?? false,
exclusiveMinimum: $schema->exclusiveMinimum ?? false,
maximum: $schema->maximum ?? null,
minimum: $schema->minimum ?? null,
maxLength: $schema->maxLength ?? null,
minLength: $schema->minLength ?? 0,
pattern: $schema->pattern ?? null,
maxItems: $schema->maxItems ?? null,
minItems: $schema->minItems ?? 0,
uniqueItems: $schema->uniqueItems ?? false,
maxProperties: $schema->maxProperties ?? null,
minProperties: $schema->minProperties ?? 0,
required: $schema->required ?? null,
allOf: isset($schema->allOf) ? $createSchemas($schema->allOf) : null,
anyOf: isset($schema->anyOf) ? $createSchemas($schema->anyOf) : null,
oneOf: isset($schema->oneOf) ? $createSchemas($schema->oneOf) : null,
not: isset($schema->not) ? self::createSchema($schema->not) : null,
items: isset($schema->items) ? self::createSchema($schema->items) : null,
properties: isset($schema->properties) ? $createSchemas($schema->properties) : [],
additionalProperties: isset($schema->additionalProperties) ? (is_bool($schema->additionalProperties) ?
$schema->additionalProperties :
self::createSchema($schema->additionalProperties) ?? true) :
true,
format: $schema->format ?? null,
title: $schema->title ?? null,
description: $schema->description ?? null,
);
}
/**
* @param Cebe\MediaType[] $mediaTypes
* @return MediaType[]
*/
private static function createContent(array $mediaTypes): array
{
$result = [];
foreach ($mediaTypes as $mediaType => $mediaTypeObject) {
assert(!$mediaTypeObject->schema instanceof Cebe\Reference);
$result[] = new MediaType(
is_string($mediaType) ? $mediaType : null,
!is_null($mediaTypeObject->schema) ?
self::createSchema($mediaTypeObject->schema) :
null
);
}
return $result;
}
private static function createOperation(
?Cebe\Operation $operation
): ?Operation {
if (is_null($operation)) {
return null;
}
$responses = [];
foreach ($operation->responses ?? [] as $code => $response) {
$responses[$code] = self::createResponse($response);
}
return new Operation(
operationId: $operation->operationId,
servers: self::createServers($operation->servers),
parameters: self::createParameters($operation->parameters),
requestBody: self::createRequestBody($operation->requestBody),
responses: $responses,
);
}
private static function createRequestBody(
Cebe\Reference|Cebe\RequestBody|null $requestBody
): ?RequestBody {
assert(! $requestBody instanceof Cebe\Reference);
if (is_null($requestBody)) {
return null;
}
return new RequestBody(
$requestBody->description ?? null,
self::createContent($requestBody->content ?? []),
$requestBody->required ?? false,
);
}
public static function createHeader(
Cebe\Reference|Cebe\Header $header
): Header {
return new Header(
description: $header->description ?? null,
style: $header->style ?? 'simple',
explode: $header->explode ?? false,
required: $header->required ?? false,
schema: isset($header->schema) ? self::createSchema($header->schema) : null,
content: isset($header->content) ? self::createContent($header->content) : [],
);
}
private static function createResponse(
Cebe\Reference|Cebe\Response $response,
): Response {
assert(! $response instanceof Cebe\Reference);
return new Response(
description: $response->description,
headers: array_map(fn($h) => self::createHeader($h), $response->headers),
content: self::createContent($response->content ?? []),
);
}
}