6
6
7
7
/**
8
8
* @template T of mixed
9
+ *
10
+ * @implements JsonSchemaInterface<T>
9
11
*/
10
- abstract class JsonSchema implements JsonSchemaInterface
12
+ class JsonSchema implements JsonSchemaInterface
11
13
{
14
+ private function __construct (
15
+ protected readonly string $ title ,
16
+ protected readonly string $ description ,
17
+ protected readonly iterable $ examples ,
18
+ protected readonly array $ schema
19
+ ) {
20
+ }
21
+
22
+ public function getTitle (): string
23
+ {
24
+ return $ this ->title ;
25
+ }
26
+
27
+ public function getDescription (): string
28
+ {
29
+ return $ this ->description ;
30
+ }
31
+
32
+ public function getExamples (): iterable
33
+ {
34
+ yield from $ this ->examples ;
35
+ }
36
+
37
+ public function getSchema (): array
38
+ {
39
+ return $ this ->schema ;
40
+ }
41
+
12
42
/**
13
- * @param JsonSchema<E> $schema
14
43
* @template E of mixed
44
+ * @param JsonSchemaInterface<E> $schema
15
45
*
16
46
* @return JsonSchema<null|E>
17
47
*/
18
- public static function nullable (self $ schema ): self
48
+ public static function nullable (JsonSchemaInterface $ schema ): self
19
49
{
20
50
return self ::create (
21
- '' ,
22
- '' ,
51
+ sprintf ( ' Nullable<%s> ' , $ schema -> getTitle ()) ,
52
+ $ schema -> getDescription () ,
23
53
[...$ schema ->getExamples (), null ],
24
54
['oneOf ' => [self ::null (), $ schema ->jsonSerialize ()]],
25
55
);
@@ -36,64 +66,23 @@ public static function create(
36
66
string $ title ,
37
67
string $ description ,
38
68
iterable $ examples ,
39
- $ schema
69
+ array $ schema
40
70
): 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
- };
71
+ return new self ($ title , $ description , $ examples , $ schema );
83
72
}
84
73
85
74
/**
86
75
* @template I
87
76
*
88
- * @param JsonSchema <I> $jsonSchema
77
+ * @param JsonSchemaInterface <I> $jsonSchema
89
78
*
90
79
* @return JsonSchema<array<int, I>>
91
80
*/
92
- public static function collection (self $ jsonSchema ): self
81
+ public static function collection (JsonSchemaInterface $ jsonSchema ): self
93
82
{
94
83
return self ::create (
95
84
sprintf ('Collection<%s> ' , $ jsonSchema ->getTitle ()),
96
- '' ,
85
+ $ jsonSchema -> getDescription () ,
97
86
[[...$ jsonSchema ->getExamples ()]],
98
87
[
99
88
'type ' => 'array ' ,
@@ -103,9 +92,9 @@ public static function collection(self $jsonSchema): self
103
92
}
104
93
105
94
/**
106
- * @return array<string, mixed>&array{title: string, description: string, examples: array<T> }
95
+ * {@inheritdoc }
107
96
*/
108
- public function jsonSerialize (): mixed
97
+ public function jsonSerialize (): array
109
98
{
110
99
$ schema = $ this ->getSchema ();
111
100
@@ -122,21 +111,6 @@ public function jsonSerialize(): mixed
122
111
);
123
112
}
124
113
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
114
/**
141
115
* @param scalar $value
142
116
*
@@ -250,9 +224,4 @@ protected static function oneOf(...$schemas): array
250
224
'oneOf ' => $ schemas ,
251
225
];
252
226
}
253
-
254
- /**
255
- * @return array<string, mixed>
256
- */
257
- abstract protected function getSchema (): array ;
258
227
}
0 commit comments