1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* It's free open-source software released under the MIT License. |
5
|
|
|
* |
6
|
|
|
* @author Anatoly Fenric <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Fenric |
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Router\Annotation\OpenApi; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Annotation |
16
|
|
|
* |
17
|
|
|
* @Target({"ALL"}) |
18
|
|
|
* |
19
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schema-object |
20
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html |
21
|
|
|
*/ |
22
|
|
|
final class Schema extends AbstractAnnotation implements SchemaInterface |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface |
27
|
|
|
*/ |
28
|
|
|
public $additionalProperties; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array<\Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface> |
32
|
|
|
*/ |
33
|
|
|
public $allOf; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array<\Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface> |
37
|
|
|
*/ |
38
|
|
|
public $anyOf; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var mixed |
42
|
|
|
*/ |
43
|
|
|
public $default; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool |
47
|
|
|
* |
48
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-schemadeprecated |
49
|
|
|
*/ |
50
|
|
|
public $deprecated; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
public $description; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
* |
60
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.1.2 |
61
|
|
|
*/ |
62
|
|
|
public $enum; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var mixed |
66
|
|
|
* |
67
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-schemaexample |
68
|
|
|
*/ |
69
|
|
|
public $example; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @var int |
73
|
|
|
* |
74
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.3 |
75
|
|
|
*/ |
76
|
|
|
public $exclusiveMaximum; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var int |
80
|
|
|
* |
81
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.5 |
82
|
|
|
*/ |
83
|
|
|
public $exclusiveMinimum; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
public $format; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var \Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface |
92
|
|
|
*/ |
93
|
|
|
public $items; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var int |
97
|
|
|
* |
98
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.2 |
99
|
|
|
*/ |
100
|
|
|
public $maximum; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @var int |
104
|
|
|
* |
105
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4.1 |
106
|
|
|
*/ |
107
|
|
|
public $maxItems; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var int |
111
|
|
|
* |
112
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.3.1 |
113
|
|
|
*/ |
114
|
|
|
public $maxLength; |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @var int |
118
|
|
|
* |
119
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.1 |
120
|
|
|
*/ |
121
|
|
|
public $maxProperties; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @var int |
125
|
|
|
* |
126
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.4 |
127
|
|
|
*/ |
128
|
|
|
public $minimum; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @var int |
132
|
|
|
* |
133
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4.2 |
134
|
|
|
*/ |
135
|
|
|
public $minItems; |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @var int |
139
|
|
|
* |
140
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.3.2 |
141
|
|
|
*/ |
142
|
|
|
public $minLength; |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @var int |
146
|
|
|
* |
147
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.2 |
148
|
|
|
*/ |
149
|
|
|
public $minProperties; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @var int |
153
|
|
|
* |
154
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.2.1 |
155
|
|
|
*/ |
156
|
|
|
public $multipleOf; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @var \Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface |
160
|
|
|
*/ |
161
|
|
|
public $not; |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @var bool |
165
|
|
|
* |
166
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-schemanullable |
167
|
|
|
*/ |
168
|
|
|
public $nullable; |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @var array<\Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface> |
172
|
|
|
*/ |
173
|
|
|
public $oneOf; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @var string |
177
|
|
|
* |
178
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.3.3 |
179
|
|
|
*/ |
180
|
|
|
public $pattern; |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @var array<\Sunrise\Http\Router\Annotation\OpenApi\SchemaInterface> |
184
|
|
|
*/ |
185
|
|
|
public $properties; |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @var bool |
189
|
|
|
* |
190
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-schemareadonly |
191
|
|
|
*/ |
192
|
|
|
public $readOnly; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @var array<string> |
196
|
|
|
* |
197
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.5.3 |
198
|
|
|
*/ |
199
|
|
|
public $required; |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @var string |
203
|
|
|
* |
204
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.9.1 |
205
|
|
|
*/ |
206
|
|
|
public $title; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @var string |
210
|
|
|
*/ |
211
|
|
|
public $type; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @var bool |
215
|
|
|
* |
216
|
|
|
* @link https://json-schema.org/draft/2019-09/json-schema-validation.html#rfc.section.6.4.3 |
217
|
|
|
*/ |
218
|
|
|
public $uniqueItems; |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @var bool |
222
|
|
|
* |
223
|
|
|
* @link https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#user-content-schemawriteonly |
224
|
|
|
*/ |
225
|
|
|
public $writeOnly; |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @var array |
229
|
|
|
*/ |
230
|
|
|
private $simpleFields = [ |
231
|
|
|
'default', |
232
|
|
|
'deprecated', |
233
|
|
|
'description', |
234
|
|
|
'enum', |
235
|
|
|
'example', |
236
|
|
|
'exclusiveMaximum', |
237
|
|
|
'exclusiveMinimum', |
238
|
|
|
'format', |
239
|
|
|
'maximum', |
240
|
|
|
'maxItems', |
241
|
|
|
'maxLength', |
242
|
|
|
'maxProperties', |
243
|
|
|
'minimum', |
244
|
|
|
'minItems', |
245
|
|
|
'minLength', |
246
|
|
|
'minProperties', |
247
|
|
|
'multipleOf', |
248
|
|
|
'nullable', |
249
|
|
|
'pattern', |
250
|
|
|
'readOnly', |
251
|
|
|
'required', |
252
|
|
|
'title', |
253
|
|
|
'type', |
254
|
|
|
'uniqueItems', |
255
|
|
|
'writeOnly', |
256
|
|
|
]; |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @var array |
260
|
|
|
*/ |
261
|
|
|
private $annotatedFields = [ |
262
|
|
|
'additionalProperties', |
263
|
|
|
'items', |
264
|
|
|
'not', |
265
|
|
|
]; |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* @var array |
269
|
|
|
*/ |
270
|
|
|
private $mappedFields = [ |
271
|
|
|
'allOf', |
272
|
|
|
'anyOf', |
273
|
|
|
'oneOf', |
274
|
|
|
'properties', |
275
|
|
|
]; |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* {@inheritDoc} |
279
|
|
|
*/ |
280
|
|
|
public function toArray() : array |
281
|
|
|
{ |
282
|
|
|
return $this->getSimpleFields() + $this->getAnnotatedFields() + $this->getMappedFields(); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
private function getSimpleFields() : array |
289
|
|
|
{ |
290
|
|
|
$result = []; |
291
|
|
|
foreach ($this->simpleFields as $fieldName) { |
292
|
|
|
if (isset($this->{$fieldName})) { |
293
|
|
|
$result[$fieldName] = $this->{$fieldName}; |
294
|
|
|
} |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
return $result; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* @return array |
302
|
|
|
*/ |
303
|
|
|
private function getAnnotatedFields() : array |
304
|
|
|
{ |
305
|
|
|
$result = []; |
306
|
|
|
foreach ($this->annotatedFields as $fieldName) { |
307
|
|
|
if (isset($this->{$fieldName})) { |
308
|
|
|
$result[$fieldName] = $this->{$fieldName}->toArray(); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $result; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @return array |
317
|
|
|
*/ |
318
|
|
|
private function getMappedFields() : array |
319
|
|
|
{ |
320
|
|
|
$result = []; |
321
|
|
|
foreach ($this->mappedFields as $fieldName) { |
322
|
|
|
if (isset($this->{$fieldName})) { |
323
|
|
|
foreach ($this->{$fieldName} as $i => $value) { |
324
|
|
|
$result[$fieldName][$i] = $value->toArray(); |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $result; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|