4
4
5
5
namespace Knp \JsonSchema ;
6
6
7
+ use Knp \JsonSchema \Validator \Errors ;
8
+
7
9
/**
8
10
* @template T of mixed
11
+ *
12
+ * @implements JsonSchemaInterface<T>
9
13
*/
10
- abstract class JsonSchema implements JsonSchemaInterface
14
+ class JsonSchema implements JsonSchemaInterface
11
15
{
16
+ private function __construct (
17
+ protected readonly string $ title ,
18
+ protected readonly string $ description ,
19
+ protected readonly iterable $ examples ,
20
+ protected readonly array $ schema
21
+ ) {
22
+ }
23
+
24
+ public function getTitle (): string
25
+ {
26
+ return $ this ->title ;
27
+ }
28
+
29
+ public function getDescription (): string
30
+ {
31
+ return $ this ->description ;
32
+ }
33
+
34
+ public function getExamples (): iterable
35
+ {
36
+ yield from $ this ->examples ;
37
+ }
38
+
39
+ public function getSchema (): array
40
+ {
41
+ return $ this ->schema ;
42
+ }
43
+
12
44
/**
13
- * @param JsonSchema<E> $schema
14
45
* @template E of mixed
46
+ * @param JsonSchemaInterface<E> $schema
15
47
*
16
48
* @return JsonSchema<null|E>
17
49
*/
18
- public static function nullable (self $ schema ): self
50
+ public static function nullable (JsonSchemaInterface $ schema ): self
19
51
{
20
52
return self ::create (
21
- '' ,
22
- '' ,
53
+ sprintf ( ' Nullable<%s> ' , $ schema -> getTitle ()) ,
54
+ $ schema -> getDescription () ,
23
55
[...$ schema ->getExamples (), null ],
24
56
['oneOf ' => [self ::null (), $ schema ->jsonSerialize ()]],
25
57
);
@@ -36,64 +68,23 @@ public static function create(
36
68
string $ title ,
37
69
string $ description ,
38
70
iterable $ examples ,
39
- $ schema
71
+ array $ schema
40
72
): self {
41
- return new class ($ title , $ description , $ examples , $ schema ) extends JsonSchema {
42
- /**
43
- * @var iterable<int, E>
44
- */
45
- private iterable $ examples ;
46
-
47
- /**
48
- * @param iterable<int, E> $examples
49
- * @param array<string, mixed> $schema
50
- */
51
- public function __construct (
52
- private string $ title ,
53
- private string $ description ,
54
- iterable $ examples ,
55
- private $ schema
56
- ) {
57
- $ this ->examples = [...$ examples ];
58
- }
59
-
60
- public function getTitle (): string
61
- {
62
- return $ this ->title ;
63
- }
64
-
65
- public function getDescription (): string
66
- {
67
- return $ this ->description ;
68
- }
69
-
70
- /**
71
- * @return iterable<int, E>
72
- */
73
- public function getExamples (): iterable
74
- {
75
- yield from $ this ->examples ;
76
- }
77
-
78
- public function getSchema (): array
79
- {
80
- return $ this ->schema ;
81
- }
82
- };
73
+ return new self ($ title , $ description , $ examples , $ schema );
83
74
}
84
75
85
76
/**
86
77
* @template I
87
78
*
88
- * @param JsonSchema <I> $jsonSchema
79
+ * @param JsonSchemaInterface <I> $jsonSchema
89
80
*
90
81
* @return JsonSchema<array<int, I>>
91
82
*/
92
- public static function collection (self $ jsonSchema ): self
83
+ public static function collection (JsonSchemaInterface $ jsonSchema ): self
93
84
{
94
85
return self ::create (
95
86
sprintf ('Collection<%s> ' , $ jsonSchema ->getTitle ()),
96
- '' ,
87
+ $ jsonSchema -> getDescription () ,
97
88
[[...$ jsonSchema ->getExamples ()]],
98
89
[
99
90
'type ' => 'array ' ,
@@ -103,9 +94,9 @@ public static function collection(self $jsonSchema): self
103
94
}
104
95
105
96
/**
106
- * @return array<string, mixed>&array{title: string, description: string, examples: array<T> }
97
+ * {@inheritdoc }
107
98
*/
108
- public function jsonSerialize (): mixed
99
+ public function jsonSerialize (): array
109
100
{
110
101
$ schema = $ this ->getSchema ();
111
102
@@ -122,27 +113,12 @@ public function jsonSerialize(): mixed
122
113
);
123
114
}
124
115
125
- /**
126
- * @return iterable<int, T>
127
- */
128
- abstract public function getExamples (): iterable ;
129
-
130
- public function getTitle (): string
131
- {
132
- return '' ;
133
- }
134
-
135
- public function getDescription (): string
136
- {
137
- return '' ;
138
- }
139
-
140
116
/**
141
117
* @param scalar $value
142
118
*
143
119
* @return array<string, mixed>
144
120
*/
145
- protected static function constant ($ value ): array
121
+ public static function constant ($ value ): array
146
122
{
147
123
return [
148
124
'const ' => $ value ,
@@ -152,7 +128,7 @@ protected static function constant($value): array
152
128
/**
153
129
* @return array<string, mixed>
154
130
*/
155
- protected static function null (): array
131
+ public static function null (): array
156
132
{
157
133
return [
158
134
'type ' => 'null ' ,
@@ -162,7 +138,7 @@ protected static function null(): array
162
138
/**
163
139
* @return array<string, mixed>
164
140
*/
165
- protected static function text (): array
141
+ public static function text (): array
166
142
{
167
143
return [
168
144
'type ' => 'string ' ,
@@ -173,7 +149,7 @@ protected static function text(): array
173
149
/**
174
150
* @return array<string, mixed>
175
151
*/
176
- protected static function boolean (): array
152
+ public static function boolean (): array
177
153
{
178
154
return [
179
155
'type ' => 'boolean ' ,
@@ -183,7 +159,7 @@ protected static function boolean(): array
183
159
/**
184
160
* @return array<string, mixed>
185
161
*/
186
- protected static function string (?string $ format = null ): array
162
+ public static function string (?string $ format = null ): array
187
163
{
188
164
$ result = [
189
165
...self ::text (),
@@ -200,7 +176,7 @@ protected static function string(?string $format = null): array
200
176
/**
201
177
* @return array<string, mixed>
202
178
*/
203
- protected static function integer (): array
179
+ public static function integer (): array
204
180
{
205
181
return [
206
182
'type ' => 'integer ' ,
@@ -210,7 +186,7 @@ protected static function integer(): array
210
186
/**
211
187
* @return array<string, mixed>
212
188
*/
213
- protected static function number (): array
189
+ public static function number (): array
214
190
{
215
191
return [
216
192
'type ' => 'number ' ,
@@ -220,7 +196,7 @@ protected static function number(): array
220
196
/**
221
197
* @return array<string, mixed>
222
198
*/
223
- protected static function date (): array
199
+ public static function date (): array
224
200
{
225
201
return [
226
202
'type ' => 'string ' ,
@@ -231,7 +207,7 @@ protected static function date(): array
231
207
/**
232
208
* @return array<string, mixed>
233
209
*/
234
- protected static function positiveInteger (): array
210
+ public static function positiveInteger (): array
235
211
{
236
212
return [
237
213
...self ::integer (),
@@ -244,15 +220,10 @@ protected static function positiveInteger(): array
244
220
*
245
221
* @return array{oneOf: array<array<string, mixed>>}
246
222
*/
247
- protected static function oneOf (...$ schemas ): array
223
+ public static function oneOf (...$ schemas ): array
248
224
{
249
225
return [
250
226
'oneOf ' => $ schemas ,
251
227
];
252
228
}
253
-
254
- /**
255
- * @return array<string, mixed>
256
- */
257
- abstract protected function getSchema (): array ;
258
229
}
0 commit comments