1818use Cortex \JsonSchema \Types \BooleanSchema ;
1919use Cortex \JsonSchema \Types \IntegerSchema ;
2020use Cortex \JsonSchema \Contracts \JsonSchema ;
21+ use Cortex \JsonSchema \Types \AbstractSchema ;
2122use Cortex \JsonSchema \Exceptions \SchemaException ;
2223
2324class JsonConverter implements Converter
@@ -149,6 +150,16 @@ private function getConstValue(string $key): bool|float|int|string|null
149150 return null ;
150151 }
151152
153+ /**
154+ * Apply shared fields to the schema.
155+ */
156+ private function applyId (AbstractSchema $ schema ): void
157+ {
158+ if (($ id = $ this ->getString ('$id ' )) !== null ) {
159+ $ schema ->id ($ id );
160+ }
161+ }
162+
152163 /**
153164 * Detect schema version from $schema URI.
154165 */
@@ -166,6 +177,7 @@ private function detectSchemaVersion(string $schemaUri): SchemaVersion
166177 private function createStringSchema (?string $ title ): StringSchema
167178 {
168179 $ stringSchema = new StringSchema ($ title , $ this ->schemaVersion );
180+ $ this ->applyId ($ stringSchema );
169181
170182 if (($ minLength = $ this ->getInt ('minLength ' )) !== null ) {
171183 $ stringSchema ->minLength ($ minLength );
@@ -179,6 +191,23 @@ private function createStringSchema(?string $title): StringSchema
179191 $ stringSchema ->pattern ($ pattern );
180192 }
181193
194+ if (($ contentEncoding = $ this ->getString ('contentEncoding ' )) !== null ) {
195+ $ stringSchema ->contentEncoding ($ contentEncoding );
196+ }
197+
198+ if (($ contentMediaType = $ this ->getString ('contentMediaType ' )) !== null ) {
199+ $ stringSchema ->contentMediaType ($ contentMediaType );
200+ }
201+
202+ $ contentSchema = $ this ->getValue ('contentSchema ' );
203+
204+ if (is_array ($ contentSchema )) {
205+ $ converter = new self ($ contentSchema , $ this ->schemaVersion );
206+ $ stringSchema ->contentSchema ($ converter ->convert ());
207+ } elseif (is_bool ($ contentSchema )) {
208+ $ stringSchema ->contentSchema ($ contentSchema );
209+ }
210+
182211 if (($ format = $ this ->getString ('format ' )) !== null ) {
183212 $ stringSchema ->format ($ format );
184213 }
@@ -218,6 +247,7 @@ private function createStringSchema(?string $title): StringSchema
218247 private function createNumberSchema (?string $ title ): NumberSchema
219248 {
220249 $ numberSchema = new NumberSchema ($ title , $ this ->schemaVersion );
250+ $ this ->applyId ($ numberSchema );
221251
222252 if (($ minimum = $ this ->getFloat ('minimum ' )) !== null ) {
223253 $ numberSchema ->minimum ($ minimum );
@@ -262,6 +292,7 @@ private function createNumberSchema(?string $title): NumberSchema
262292 private function createIntegerSchema (?string $ title ): IntegerSchema
263293 {
264294 $ integerSchema = new IntegerSchema ($ title , $ this ->schemaVersion );
295+ $ this ->applyId ($ integerSchema );
265296
266297 if (($ minimum = $ this ->getInt ('minimum ' )) !== null ) {
267298 $ integerSchema ->minimum ($ minimum );
@@ -306,6 +337,7 @@ private function createIntegerSchema(?string $title): IntegerSchema
306337 private function createBooleanSchema (?string $ title ): BooleanSchema
307338 {
308339 $ booleanSchema = new BooleanSchema ($ title , $ this ->schemaVersion );
340+ $ this ->applyId ($ booleanSchema );
309341
310342 if (($ const = $ this ->getConstValue ('const ' )) !== null ) {
311343 $ booleanSchema ->const ($ const );
@@ -329,6 +361,7 @@ private function createBooleanSchema(?string $title): BooleanSchema
329361 private function createArraySchema (?string $ title ): ArraySchema
330362 {
331363 $ arraySchema = new ArraySchema ($ title , $ this ->schemaVersion );
364+ $ this ->applyId ($ arraySchema );
332365
333366 if (($ items = $ this ->getArray ('items ' )) !== null ) {
334367 $ converter = new self ($ items , $ this ->schemaVersion );
@@ -372,6 +405,7 @@ private function createArraySchema(?string $title): ArraySchema
372405 private function createObjectSchema (?string $ title ): ObjectSchema
373406 {
374407 $ objectSchema = new ObjectSchema ($ title , $ this ->schemaVersion );
408+ $ this ->applyId ($ objectSchema );
375409 $ required = $ this ->getArray ('required ' ) ?? [];
376410
377411 if (($ properties = $ this ->getArray ('properties ' )) !== null ) {
@@ -434,6 +468,7 @@ private function createObjectSchema(?string $title): ObjectSchema
434468 private function createNullSchema (?string $ title ): NullSchema
435469 {
436470 $ nullSchema = new NullSchema ($ title , $ this ->schemaVersion );
471+ $ this ->applyId ($ nullSchema );
437472
438473 if (($ description = $ this ->getString ('description ' )) !== null ) {
439474 $ nullSchema ->description ($ description );
@@ -461,6 +496,8 @@ private function createUnionSchema(?string $title): UnionSchema
461496 $ schema = new UnionSchema (SchemaType::cases (), $ title , $ this ->schemaVersion );
462497 }
463498
499+ $ this ->applyId ($ schema );
500+
464501 if (($ enum = $ this ->getArray ('enum ' )) !== null && $ enum !== []) {
465502 /** @var non-empty-array<bool|float|int|string|null> $enum */
466503 $ schema ->enum ($ enum );
0 commit comments