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 );
113 private static function normalizeJsonSchema( array $schema ): array {
114 if ( isset( $schema[
'type'] ) ) {
116 $schema[
'type'] = self::phpDocToJson( $schema[
'type'] );
119 if ( isset( $schema[
'additionalProperties'] ) && is_array( $schema[
'additionalProperties'] ) ) {
120 $schema[
'additionalProperties'] =
121 self::normalizeJsonSchema( $schema[
'additionalProperties'] );
124 if ( isset( $schema[
'items'] ) && is_array( $schema[
'items'] ) ) {
125 $schema[
'items'] = self::normalizeJsonSchema( $schema[
'items'] );
128 if ( isset( $schema[
'properties'] ) && is_array( $schema[
'properties'] ) ) {
129 foreach ( $schema[
'properties'] as $name => $propSchema ) {
130 $schema[
'properties'][$name] = self::normalizeJsonSchema( $propSchema );
148 private static function getDefaultFromJsonSchema( array $schema ) {
149 $default = $schema[
'default'] ??
null;
151 foreach ( $schema[
'properties'] ?? [] as $name => $sch ) {
152 $def = self::getDefaultFromJsonSchema( $sch );
154 $default[$name] = $def;
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.