3namespace MediaWiki\Settings\Source;
5use InvalidArgumentException;
12trait JsonSchemaTrait {
21 private static function jsonToPhpDoc( $jsonSchemaType ) {
31 if ( $jsonSchemaType ===
null ) {
32 throw new InvalidArgumentException(
'The type name cannot be null! Use "null" instead.' );
36 if ( is_array( $jsonSchemaType ) ) {
38 $nullable = count( $jsonSchemaType ) === 2 ? array_search(
'null', $jsonSchemaType ) : false;
39 if ( $nullable !==
false ) {
40 unset( $jsonSchemaType[$nullable] );
43 $jsonSchemaType = array_map( self::jsonToPhpDoc( ... ), $jsonSchemaType );
44 $type = implode(
'|', $jsonSchemaType );
46 $type = $phpTypes[ strtolower( $jsonSchemaType ) ] ?? $jsonSchemaType;
49 return $nullable ===
false ? $type :
"?$type";
57 private static function phpDocToJson( $phpDocType ) {
62 'stdclass' =>
'object',
69 if ( $phpDocType ===
null ) {
70 throw new InvalidArgumentException(
'The type name cannot be null! Use "null" instead.' );
73 if ( is_string( $phpDocType ) ) {
74 $phpDocType = explode(
'|', trim( $phpDocType ) );
80 foreach ( $phpDocType as $t ) {
81 if ( str_starts_with( $t,
'?' ) ) {
86 $types[$jsonTypes[ strtolower( $t ) ] ??
$t] =
null;
90 $types[
'null'] =
null;
93 if ( count( $types ) === 1 ) {
94 return array_key_first( $types );
97 return array_keys( $types );
110 private static function normalizeJsonSchema(
114 string $propertyName,
115 bool $inlineReferences =
false
117 $traversedReferences = [];
118 return self::doNormalizeJsonSchema(
119 $schema, $defs,
$source, $propertyName, $inlineReferences, $traversedReferences
136 private static function doNormalizeJsonSchema(
140 string $propertyName,
141 bool $inlineReferences,
142 array $traversedReferences
144 if ( isset( $schema[
'type'] ) ) {
146 $schema[
'type'] = self::phpDocToJson( $schema[
'type'] );
149 if ( isset( $schema[
'additionalProperties'] ) && is_array( $schema[
'additionalProperties'] ) ) {
150 $schema[
'additionalProperties'] =
151 self::doNormalizeJsonSchema(
152 $schema[
'additionalProperties'],
161 if ( isset( $schema[
'items'] ) && is_array( $schema[
'items'] ) ) {
162 $schema[
'items'] = self::doNormalizeJsonSchema(
172 if ( isset( $schema[
'properties'] ) && is_array( $schema[
'properties'] ) ) {
173 foreach ( $schema[
'properties'] as $name => $propSchema ) {
174 $schema[
'properties'][$name] = self::doNormalizeJsonSchema(
185 if ( isset( $schema[
'$ref'] ) ) {
187 if ( array_key_exists( $definitionName, $traversedReferences ) ) {
189 "Found a loop while resolving reference $definitionName in $propertyName." .
190 " Root schema location: $source"
195 if ( !isset( $defs[$definitionName] ) ) {
196 $traversedReferences[$definitionName] =
true;
197 $normalizedDefinition = self::doNormalizeJsonSchema(
205 if ( !$inlineReferences ) {
206 $defs[$definitionName] = $normalizedDefinition;
209 $normalizedDefinition = $defs[$definitionName];
213 if ( $inlineReferences ) {
214 $schema = $normalizedDefinition;
235 private static function getDefaultFromJsonSchema( array $schema ) {
236 $default = $schema[
'default'] ??
null;
238 foreach ( $schema[
'properties'] ?? [] as $name => $sch ) {
239 $def = self::getDefaultFromJsonSchema( $sch );
241 $default[$name] = $def;
if(!defined('MW_SETUP_CALLBACK'))