MediaWiki 1.42.0
JsonSchemaTrait.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
6
12trait JsonSchemaTrait {
13
21 private static function jsonToPhpDoc( $jsonSchemaType ) {
22 static $phpTypes = [
23 'array' => 'array',
24 'object' => 'array', // could be optional
25 'number' => 'float',
26 'double' => 'float', // for good measure
27 'boolean' => 'bool',
28 'integer' => 'int',
29 ];
30
31 if ( $jsonSchemaType === null ) {
32 throw new InvalidArgumentException( 'The type name cannot be null! Use "null" instead.' );
33 }
34
35 $nullable = false;
36 if ( is_array( $jsonSchemaType ) ) {
37 $nullIndex = array_search( 'null', $jsonSchemaType );
38 if ( $nullIndex !== false ) {
39 $nullable = true;
40 unset( $jsonSchemaType[$nullIndex] );
41 }
42
43 $jsonSchemaType = array_map( [ self::class, 'jsonToPhpDoc' ], $jsonSchemaType );
44 $type = implode( '|', $jsonSchemaType );
45 } else {
46 $type = $phpTypes[ strtolower( $jsonSchemaType ) ] ?? $jsonSchemaType;
47 }
48
49 if ( $nullable ) {
50 $type = "?$type";
51 }
52
53 return $type;
54 }
55
61 private static function phpDocToJson( $phpDocType ) {
62 static $jsonTypes = [
63 'list' => 'array',
64 'dict' => 'object',
65 'map' => 'object',
66 'stdclass' => 'object',
67 'int' => 'integer',
68 'float' => 'number',
69 'bool' => 'boolean',
70 'false' => 'boolean',
71 ];
72
73 if ( $phpDocType === null ) {
74 throw new InvalidArgumentException( 'The type name cannot be null! Use "null" instead.' );
75 }
76
77 if ( is_array( $phpDocType ) ) {
78 $types = $phpDocType;
79 } else {
80 $types = explode( '|', trim( $phpDocType ) );
81 }
82
83 $nullable = false;
84 foreach ( $types as $i => $t ) {
85 if ( str_starts_with( $t, '?' ) ) {
86 $nullable = true;
87 $t = substr( $t, 1 );
88 }
89
90 $types[$i] = $jsonTypes[ strtolower( $t ) ] ?? $t;
91 }
92
93 if ( $nullable ) {
94 $types[] = 'null';
95 }
96
97 $types = array_unique( $types );
98
99 if ( count( $types ) === 1 ) {
100 return reset( $types );
101 }
102
103 return $types;
104 }
105
116 private static function normalizeJsonSchema(
117 array $schema,
118 array &$defs,
119 string $source,
120 string $propertyName
121 ): array {
122 if ( isset( $schema['type'] ) ) {
123 // Support PHP Doc style types, for convenience.
124 $schema['type'] = self::phpDocToJson( $schema['type'] );
125 }
126
127 if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
128 $schema['additionalProperties'] =
129 self::normalizeJsonSchema( $schema['additionalProperties'], $defs, $source, $propertyName );
130 }
131
132 if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
133 $schema['items'] = self::normalizeJsonSchema( $schema['items'], $defs, $source, $propertyName );
134 }
135
136 if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
137 foreach ( $schema['properties'] as $name => $propSchema ) {
138 $schema['properties'][$name] = self::normalizeJsonSchema( $propSchema, $defs, $source, $propertyName );
139 }
140 }
141
142 // Definitions need to be collected before normalizing the reference because
143 // JsonSchemaReferenceResolver expects the $ref to be an array with:
144 // [ "class" => "Some\\Class", "field" => "someField" ]
145 JsonSchemaReferenceResolver::getDefinitions( $schema, $defs, $source, $propertyName );
146
147 if ( isset( $schema['$ref'] ) ) {
148 $schema['$ref'] = JsonSchemaReferenceResolver::normalizeRef( $schema['$ref'] );
149 }
150
151 return $schema;
152 }
153
165 private static function getDefaultFromJsonSchema( array $schema ) {
166 $default = $schema['default'] ?? null;
167
168 foreach ( $schema['properties'] ?? [] as $name => $sch ) {
169 $def = self::getDefaultFromJsonSchema( $sch );
170
171 $default[$name] = $def;
172 }
173
174 return $default;
175 }
176
177}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
static getDefinitions(array $schema, array &$defs, string $source, string $propertyName)
Traverse a JSON-schema to resolve all its referenced schemas ($ref) and return them as an array of de...
static normalizeRef(array $schema)
Returns a URI relative to a JSON-schema document for a given definition name.
$source