1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JsonSchema\Schema; |
4
|
|
|
|
5
|
|
|
use JsonSchema\Keyword\AllOfKeyword; |
6
|
|
|
use JsonSchema\Keyword\AnyOfKeyword; |
7
|
|
|
use JsonSchema\Keyword\CommentKeyword; |
8
|
|
|
use JsonSchema\Keyword\ConstKeyword; |
9
|
|
|
use JsonSchema\Keyword\DefaultKeyword; |
10
|
|
|
use JsonSchema\Keyword\DeprecatedKeyword; |
11
|
|
|
use JsonSchema\Keyword\DescriptionKeyword; |
12
|
|
|
use JsonSchema\Keyword\ElseKeyword; |
13
|
|
|
use JsonSchema\Keyword\EnumKeyword; |
14
|
|
|
use JsonSchema\Keyword\ExamplesKeyword; |
15
|
|
|
use JsonSchema\Keyword\IfKeyword; |
16
|
|
|
use JsonSchema\Keyword\KeywordInterface; |
17
|
|
|
use JsonSchema\Keyword\NotKeyword; |
18
|
|
|
use JsonSchema\Keyword\OneOfKeyword; |
19
|
|
|
use JsonSchema\Keyword\ReadOnlyKeyword; |
20
|
|
|
use JsonSchema\Keyword\ThenKeyword; |
21
|
|
|
use JsonSchema\Keyword\TitleKeyword; |
22
|
|
|
use JsonSchema\Keyword\WriteOnlyKeyword; |
23
|
|
|
|
24
|
|
|
class AbstractSchema implements SchemaInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var array<string, string|bool|int|float|array|object|null> |
28
|
|
|
*/ |
29
|
|
|
private array $jsonSchema = []; |
30
|
|
|
|
31
|
204 |
|
public function __construct(KeywordInterface $keyword = null) |
32
|
|
|
{ |
33
|
204 |
|
if (null !== $keyword) { |
34
|
182 |
|
$this->addKeyword($keyword); |
35
|
|
|
} |
36
|
204 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return static |
40
|
|
|
*/ |
41
|
8 |
|
public function comment(?string $comment): self |
42
|
|
|
{ |
43
|
8 |
|
return $this->with( |
44
|
8 |
|
new CommentKeyword($comment) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
8 |
|
public function title(?string $title): self |
52
|
|
|
{ |
53
|
8 |
|
return $this->with( |
54
|
8 |
|
new TitleKeyword($title) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
8 |
|
public function description(?string $description): self |
62
|
|
|
{ |
63
|
8 |
|
return $this->with( |
64
|
8 |
|
new DescriptionKeyword($description) |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
8 |
|
public function deprecated(?bool $deprecated): self |
72
|
|
|
{ |
73
|
8 |
|
return $this->with( |
74
|
8 |
|
new DeprecatedKeyword($deprecated) |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return static |
80
|
|
|
*/ |
81
|
8 |
|
public function readOnly(?bool $readOnly): self |
82
|
|
|
{ |
83
|
8 |
|
return $this->with( |
84
|
8 |
|
new ReadOnlyKeyword($readOnly) |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return static |
90
|
|
|
*/ |
91
|
8 |
|
public function writeOnly(?bool $writeOnly): self |
92
|
|
|
{ |
93
|
8 |
|
return $this->with( |
94
|
8 |
|
new WriteOnlyKeyword($writeOnly) |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param mixed[]|bool|float|int|object|string|null $default |
100
|
|
|
* |
101
|
|
|
* @return static |
102
|
|
|
*/ |
103
|
8 |
|
public function default($default): self |
104
|
|
|
{ |
105
|
8 |
|
return $this->with( |
106
|
8 |
|
new DefaultKeyword($default) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param array<mixed[]|bool|float|int|object|string|null>|null $examples |
112
|
|
|
* |
113
|
|
|
* @return static |
114
|
|
|
*/ |
115
|
8 |
|
public function examples(?array $examples): self |
116
|
|
|
{ |
117
|
8 |
|
return $this->with( |
118
|
8 |
|
new ExamplesKeyword($examples) |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param SchemaInterface[]|null $anyOfs |
124
|
|
|
* |
125
|
|
|
* @return static |
126
|
|
|
*/ |
127
|
8 |
|
public function anyOf(?array $anyOfs): self |
128
|
|
|
{ |
129
|
8 |
|
return $this->with( |
130
|
8 |
|
new AnyOfKeyword($anyOfs) |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param SchemaInterface[]|null $allOfs |
136
|
|
|
* |
137
|
|
|
* @return static |
138
|
|
|
*/ |
139
|
8 |
|
public function allOf(?array $allOfs): self |
140
|
|
|
{ |
141
|
8 |
|
return $this->with( |
142
|
8 |
|
new AllOfKeyword($allOfs) |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param SchemaInterface[]|null $oneOfs |
148
|
|
|
* |
149
|
|
|
* @return static |
150
|
|
|
*/ |
151
|
8 |
|
public function oneOf(?array $oneOfs): self |
152
|
|
|
{ |
153
|
8 |
|
return $this->with( |
154
|
8 |
|
new OneOfKeyword($oneOfs) |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return static |
160
|
|
|
*/ |
161
|
8 |
|
public function not(?SchemaInterface $not): self |
162
|
|
|
{ |
163
|
8 |
|
return $this->with( |
164
|
8 |
|
new NotKeyword($not) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return static |
170
|
|
|
*/ |
171
|
8 |
|
public function if(?SchemaInterface $if): self |
172
|
|
|
{ |
173
|
8 |
|
return $this->with( |
174
|
8 |
|
new IfKeyword($if) |
175
|
|
|
); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return static |
180
|
|
|
*/ |
181
|
8 |
|
public function then(?SchemaInterface $then): self |
182
|
|
|
{ |
183
|
8 |
|
return $this->with( |
184
|
8 |
|
new ThenKeyword($then) |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return static |
190
|
|
|
*/ |
191
|
8 |
|
public function else(?SchemaInterface $else): self |
192
|
|
|
{ |
193
|
8 |
|
return $this->with( |
194
|
8 |
|
new ElseKeyword($else) |
195
|
|
|
); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @param mixed[]|bool|float|int|object|string|null $const |
200
|
|
|
* |
201
|
|
|
* @return static |
202
|
|
|
*/ |
203
|
8 |
|
public function const($const): self |
204
|
|
|
{ |
205
|
8 |
|
return $this->with( |
206
|
8 |
|
new ConstKeyword($const) |
207
|
|
|
); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @param array<mixed[]|bool|float|int|object|string|null>|null $enum |
212
|
|
|
* |
213
|
|
|
* @return static |
214
|
|
|
*/ |
215
|
8 |
|
public function enum(?array $enum): self |
216
|
|
|
{ |
217
|
8 |
|
return $this->with( |
218
|
8 |
|
new EnumKeyword($enum) |
219
|
|
|
); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @return static |
224
|
|
|
*/ |
225
|
188 |
|
public function with(KeywordInterface $keyword): self |
226
|
|
|
{ |
227
|
188 |
|
$instance = clone $this; |
228
|
188 |
|
$instance->addKeyword($keyword); |
229
|
|
|
|
230
|
188 |
|
return $instance; |
231
|
|
|
} |
232
|
|
|
|
233
|
202 |
|
private function addKeyword(KeywordInterface $keyword): void |
234
|
|
|
{ |
235
|
202 |
|
$key = $keyword->getKey(); |
236
|
202 |
|
unset($this->jsonSchema[$key]); |
237
|
|
|
|
238
|
202 |
|
$value = $keyword->getValue(); |
239
|
202 |
|
if ($keyword->supportsNullValue() || null !== $value) { |
240
|
202 |
|
$this->jsonSchema[$key] = $value; |
241
|
|
|
} |
242
|
202 |
|
} |
243
|
|
|
|
244
|
204 |
|
public function toJsonSchema(): object |
245
|
|
|
{ |
246
|
204 |
|
return (object) $this->jsonSchema; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return static |
251
|
|
|
*/ |
252
|
9 |
|
public static function create(): self |
253
|
|
|
{ |
254
|
9 |
|
return new static(); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|