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 ) ) {
37 $nullIndex = array_search(
'null', $jsonSchemaType );
38 if ( $nullIndex !==
false ) {
40 unset( $jsonSchemaType[$nullIndex] );
43 $jsonSchemaType = array_map( [ self::class,
'jsonToPhpDoc' ], $jsonSchemaType );
44 $type = implode(
'|', $jsonSchemaType );
46 $type = $phpTypes[ strtolower( $jsonSchemaType ) ] ?? $jsonSchemaType;
61 private static function phpDocToJson( $phpDocType ) {
66 'stdclass' =>
'object',
73 if ( $phpDocType ===
null ) {
74 throw new InvalidArgumentException(
'The type name cannot be null! Use "null" instead.' );
77 if ( is_array( $phpDocType ) ) {
80 $types = explode(
'|', trim( $phpDocType ) );
84 foreach ( $types as $i => $t ) {
85 if ( str_starts_with( $t,
'?' ) ) {
90 $types[$i] = $jsonTypes[ strtolower( $t ) ] ??
$t;
97 $types = array_unique( $types );
99 if ( count( $types ) === 1 ) {
100 return reset( $types );
116 private static function normalizeJsonSchema(
122 if ( isset( $schema[
'type'] ) ) {
124 $schema[
'type'] = self::phpDocToJson( $schema[
'type'] );
127 if ( isset( $schema[
'additionalProperties'] ) && is_array( $schema[
'additionalProperties'] ) ) {
128 $schema[
'additionalProperties'] =
129 self::normalizeJsonSchema( $schema[
'additionalProperties'], $defs,
$source, $propertyName );
132 if ( isset( $schema[
'items'] ) && is_array( $schema[
'items'] ) ) {
133 $schema[
'items'] = self::normalizeJsonSchema( $schema[
'items'], $defs,
$source, $propertyName );
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 );
147 if ( isset( $schema[
'$ref'] ) ) {
165 private static function getDefaultFromJsonSchema( array $schema ) {
166 $default = $schema[
'default'] ??
null;
168 foreach ( $schema[
'properties'] ?? [] as $name => $sch ) {
169 $def = self::getDefaultFromJsonSchema( $sch );
171 $default[$name] = $def;
if(!defined('MW_SETUP_CALLBACK'))