-
Notifications
You must be signed in to change notification settings - Fork 20
/
Schema.php
428 lines (366 loc) · 10.2 KB
/
Schema.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
declare(strict_types=1);
namespace SwaggerBake\Lib\OpenApi;
use AllowDynamicProperties;
use JsonSerializable;
use SwaggerBake\Lib\Attribute\OpenApiSchema;
use SwaggerBake\Lib\Utility\ArrayUtility;
/**
* Class Schema
*
* @package SwaggerBake\Lib\OpenApi
* @see https://swagger.io/docs/specification/data-models/
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
*/
#[AllowDynamicProperties]
class Schema implements JsonSerializable, SchemaInterface
{
use SchemaTrait;
/**
* @param string|null $title Title of the schema
* @param array<string> $required A list of required properties
* @param array $properties A mixed array of Schema and SchemaProperty
* @param string|null $refEntity todo: needs documentation
* @param array $items A list of items this Schema contains when this schema is an array.
* @param array $oneOf A list of $ref that this Schema is one of, e.g. [['$ref' => '#']].
* See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
* @param array $anyOf A list of $ref that this Schema is any of, e.g. [['$ref' => '#']].
* See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
* @param array $allOf A list of $ref that this Schema is all of, e.g. [['$ref' => '#']].
* See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
* @param array $not A list of $ref that this Schema is note, e.g. [['$ref' => '#']].
* See https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/
* @param \SwaggerBake\Lib\OpenApi\Xml|null $xml todo: needs documentation
* @param int $visibility See OpenApiSchema class constants
* @param string|null $refPath OpenAPI $ref such as #/components/schema/MyModel
* @param bool $isCustomSchema Denotes if this schema was added via a DTO or Response attribute.
* @param mixed|null $example Sets an example, note this is marked as deprecated in the OpenAPI spec and its
* use is discouraged. This will be replaced by examples spec in a future release.
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
private ?string $title = null,
private array $required = [],
private array $properties = [],
private ?string $refEntity = null,
private array $items = [],
private array $oneOf = [],
private array $anyOf = [],
private array $allOf = [],
private array $not = [],
private ?Xml $xml = null,
private int $visibility = OpenApiSchema::VISIBLE_DEFAULT,
private ?string $refPath = null,
private bool $isCustomSchema = false,
private mixed $example = null
) {
}
/**
* @return array
*/
public function toArray(): array
{
$vars = get_object_vars($this);
// always unset
$vars = ArrayUtility::removeKeysMatching(
$vars,
['name','refEntity','isPublic', 'refPath', 'visibility', 'isCustomSchema',]
);
// must stay in this order to prevent https://github.com/cnizzardini/cakephp-swagger-bake/issues/30
$vars['required'] = array_values(array_unique($vars['required']));
if (!empty($this->refEntity)) {
$vars['$ref'] = $this->refEntity;
}
// remove null or empty properties to avoid swagger.json clutter
$vars = ArrayUtility::removeEmptyVars(
$vars,
['title','properties','items','oneOf','anyOf','allOf','not','enum','format','type','xml','required']
);
// remove null properties only
$vars = ArrayUtility::removeNullValues($vars, ['description', 'example']);
return $vars;
}
/**
* @inheritDoc
*/
public function jsonSerialize(): mixed
{
return $this->toArray();
}
/**
* Defines a vendor property, such as `x-my-property`
*
* @param string $name name of the attribute
* @param mixed $value value
* @return $this
*/
public function setVendorProperty(string $name, mixed $value)
{
$this->{$name} = $value;
return $this;
}
/**
* @param string $name name of the attribute
* @return mixed
*/
public function getVendorProperty(string $name): mixed
{
return $this->{$name} ?? null;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string|null $title Title
* @return $this
*/
public function setTitle(?string $title)
{
$this->title = $title;
return $this;
}
/**
* @return array
*/
public function getRequired(): array
{
return $this->required;
}
/**
* @param array<string> $required Required
* @return $this
*/
public function setRequired(array $required)
{
$this->required = $required;
return $this;
}
/**
* @param string $propertyName Property name
* @return $this
*/
public function pushRequired(string $propertyName)
{
$this->required[$propertyName] = $propertyName;
return $this;
}
/**
* A mixed array of Schema and SchemaProperty
*
* @return array
*/
public function getProperties(): array
{
return $this->properties;
}
/**
* @param array<\SwaggerBake\Lib\OpenApi\Schema>|array<\SwaggerBake\Lib\OpenApi\SchemaProperty> $properties A mixed array of Schema and SchemaProperty
* @return $this
*/
public function setProperties(array $properties)
{
$this->properties = [];
foreach ($properties as $property) {
$this->pushProperty($property);
}
return $this;
}
/**
* @param \SwaggerBake\Lib\OpenApi\SchemaInterface $property instance of SchemaInterface
* @return $this
*/
public function pushProperty(SchemaInterface $property)
{
$this->properties[$property->getName()] = $property;
if ($property instanceof SchemaProperty && $property->isRequired()) {
$this->required[$property->getName()] = $property->getName();
} elseif (isset($this->required[$property->getName()])) {
unset($this->required[$property->getName()]);
}
return $this;
}
/**
* @return string|null
*/
public function getRefEntity(): ?string
{
return $this->refEntity;
}
/**
* @param string $refEntity Reference YAML schema such as #/components/schema/MyEntity
* @return $this
*/
public function setRefEntity(string $refEntity)
{
$this->refEntity = $refEntity;
return $this;
}
/**
* @return array
*/
public function getItems(): array
{
return $this->items;
}
/**
* @param array $items Items
* @return $this
*/
public function setItems(array $items)
{
$this->items = $items;
return $this;
}
/**
* @return array
*/
public function getOneOf(): array
{
return $this->oneOf;
}
/**
* @param array $oneOf One Of e.g. [['$ref' => '#']]
* @return $this
*/
public function setOneOf(array $oneOf)
{
$this->oneOf = $oneOf;
return $this;
}
/**
* @return array
*/
public function getAnyOf(): array
{
return $this->anyOf;
}
/**
* @param array $anyOf Any Of e.g. [['$ref' => '#']]
* @return $this
*/
public function setAnyOf(array $anyOf)
{
$this->anyOf = $anyOf;
return $this;
}
/**
* @return array
*/
public function getAllOf(): array
{
return $this->allOf;
}
/**
* @param array $allOf All Of e.g. [['$ref' => '#']]
* @return $this
*/
public function setAllOf(array $allOf)
{
$this->allOf = $allOf;
return $this;
}
/**
* @return array
*/
public function getNot(): array
{
return $this->not;
}
/**
* @param array $not Not e.g. [['$ref' => '#']]
* @return $this
*/
public function setNot(array $not)
{
$this->not = $not;
return $this;
}
/**
* @return \SwaggerBake\Lib\OpenApi\Xml|null
*/
public function getXml(): ?Xml
{
return $this->xml;
}
/**
* @param \SwaggerBake\Lib\OpenApi\Xml|null $xml Xml
* @return $this
*/
public function setXml(?Xml $xml)
{
$this->xml = $xml;
return $this;
}
/**
* @return int
*/
public function getVisibility(): int
{
return $this->visibility;
}
/**
* @param int $visibility See OpenApiSchema class constants
* @return $this
*/
public function setVisibility(int $visibility)
{
$this->visibility = $visibility;
return $this;
}
/**
* @return string|null
*/
public function getRefPath(): ?string
{
return $this->refPath;
}
/**
* @param string|null $refPath the openapi ref location (e.g. #/components/schemas/Model)
* @return $this
*/
public function setRefPath(?string $refPath)
{
$this->refPath = $refPath;
return $this;
}
/**
* @return bool
*/
public function isCustomSchema(): bool
{
return $this->isCustomSchema;
}
/**
* @param bool $isCustomSchema Is this a custom schema?
* @return $this
*/
public function setIsCustomSchema(bool $isCustomSchema)
{
$this->isCustomSchema = $isCustomSchema;
return $this;
}
/**
* @deprecated This will be removed from the OpenAPI spec and its use is currently discouraged
* @todo implement examples
* @return mixed|null
*/
public function getExample(): mixed
{
return $this->example;
}
/**
* @deprecated This will be removed from the OpenAPI spec and its use is currently discouraged
* @todo implement examples
* @param mixed|null $example An optional example of the schema
* @return $this
*/
public function setExample(mixed $example)
{
$this->example = $example;
return $this;
}
}