MediaWiki master
JsonSchemaTrait.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Settings\Source;
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 // Don't turn "null" into "?", nor should "null|int|string" become "?int|string"
38 $nullable = count( $jsonSchemaType ) === 2 ? array_search( 'null', $jsonSchemaType ) : false;
39 if ( $nullable !== false ) {
40 unset( $jsonSchemaType[$nullable] );
41 }
42
43 $jsonSchemaType = array_map( self::jsonToPhpDoc( ... ), $jsonSchemaType );
44 $type = implode( '|', $jsonSchemaType );
45 } else {
46 $type = $phpTypes[ strtolower( $jsonSchemaType ) ] ?? $jsonSchemaType;
47 }
48
49 return $nullable === false ? $type : "?$type";
50 }
51
57 private static function phpDocToJson( $phpDocType ) {
58 static $jsonTypes = [
59 'list' => 'array',
60 'dict' => 'object',
61 'map' => 'object',
62 'stdclass' => 'object',
63 'int' => 'integer',
64 'float' => 'number',
65 'bool' => 'boolean',
66 'false' => 'boolean',
67 ];
68
69 if ( $phpDocType === null ) {
70 throw new InvalidArgumentException( 'The type name cannot be null! Use "null" instead.' );
71 }
72
73 if ( is_string( $phpDocType ) ) {
74 $phpDocType = explode( '|', trim( $phpDocType ) );
75 }
76
78 $types = [];
79 $nullable = false;
80 foreach ( $phpDocType as $t ) {
81 if ( str_starts_with( $t, '?' ) ) {
82 $nullable = true;
83 $t = substr( $t, 1 );
84 }
85
86 $types[$jsonTypes[ strtolower( $t ) ] ?? $t] = null;
87 }
88
89 if ( $nullable ) {
90 $types['null'] = null;
91 }
92
93 if ( count( $types ) === 1 ) {
94 return array_key_first( $types );
95 }
96
97 return array_keys( $types );
98 }
99
110 private static function normalizeJsonSchema(
111 array $schema,
112 array &$defs,
113 string $source,
114 string $propertyName,
115 bool $inlineReferences = false
116 ): array {
117 $traversedReferences = [];
118 return self::doNormalizeJsonSchema(
119 $schema, $defs, $source, $propertyName, $inlineReferences, $traversedReferences
120 );
121 }
122
136 private static function doNormalizeJsonSchema(
137 array $schema,
138 array &$defs,
139 string $source,
140 string $propertyName,
141 bool $inlineReferences,
142 array $traversedReferences
143 ): array {
144 if ( isset( $schema['type'] ) ) {
145 // Support PHP Doc style types, for convenience.
146 $schema['type'] = self::phpDocToJson( $schema['type'] );
147 }
148
149 if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
150 $schema['additionalProperties'] =
151 self::doNormalizeJsonSchema(
152 $schema['additionalProperties'],
153 $defs,
154 $source,
155 $propertyName,
156 $inlineReferences,
157 $traversedReferences
158 );
159 }
160
161 if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
162 $schema['items'] = self::doNormalizeJsonSchema(
163 $schema['items'],
164 $defs,
165 $source,
166 $propertyName,
167 $inlineReferences,
168 $traversedReferences
169 );
170 }
171
172 if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
173 foreach ( $schema['properties'] as $name => $propSchema ) {
174 $schema['properties'][$name] = self::doNormalizeJsonSchema(
175 $propSchema,
176 $defs,
177 $source,
178 $propertyName,
179 $inlineReferences,
180 $traversedReferences
181 );
182 }
183 }
184
185 if ( isset( $schema['$ref'] ) ) {
186 $definitionName = JsonSchemaReferenceResolver::getDefinitionName( $schema[ '$ref' ] );
187 if ( array_key_exists( $definitionName, $traversedReferences ) ) {
188 throw new RefLoopException(
189 "Found a loop while resolving reference $definitionName in $propertyName." .
190 " Root schema location: $source"
191 );
192 }
193 $def = JsonSchemaReferenceResolver::resolveRef( $schema['$ref'], $source );
194 if ( $def ) {
195 if ( !isset( $defs[$definitionName] ) ) {
196 $traversedReferences[$definitionName] = true;
197 $normalizedDefinition = self::doNormalizeJsonSchema(
198 $def,
199 $defs,
200 $source,
201 $propertyName,
202 $inlineReferences,
203 $traversedReferences
204 );
205 if ( !$inlineReferences ) {
206 $defs[$definitionName] = $normalizedDefinition;
207 }
208 } else {
209 $normalizedDefinition = $defs[$definitionName];
210 }
211 // Normalize reference after resolving it since JsonSchemaReferenceResolver expects
212 // the $ref to be an array with: [ "class" => "Some\\Class", "field" => "someField" ]
213 if ( $inlineReferences ) {
214 $schema = $normalizedDefinition;
215 } else {
216 $schema['$ref'] = JsonSchemaReferenceResolver::normalizeRef( $schema['$ref'] );
217 }
218 }
219 }
220
221 return $schema;
222 }
223
235 private static function getDefaultFromJsonSchema( array $schema ) {
236 $default = $schema['default'] ?? null;
237
238 foreach ( $schema['properties'] ?? [] as $name => $sch ) {
239 $def = self::getDefaultFromJsonSchema( $sch );
240
241 $default[$name] = $def;
242 }
243
244 return $default;
245 }
246
247}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
static resolveRef(array $schema, string $rootClass)
Returns the value of a given reference "$ref".
static getDefinitionName(array $schema)
Builds a definition name based on the specified "$ref" definition.
static normalizeRef(array $schema)
Returns a URI relative to a JSON-schema document for a given definition name.
Thrown when resolving references in a JSONSchema results in an infinite loop.
$source