MediaWiki master
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
113 private static function normalizeJsonSchema( array $schema ): array {
114 if ( isset( $schema['type'] ) ) {
115 // Support PHP Doc style types, for convenience.
116 $schema['type'] = self::phpDocToJson( $schema['type'] );
117 }
118
119 if ( isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ) ) {
120 $schema['additionalProperties'] =
121 self::normalizeJsonSchema( $schema['additionalProperties'] );
122 }
123
124 if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
125 $schema['items'] = self::normalizeJsonSchema( $schema['items'] );
126 }
127
128 if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
129 foreach ( $schema['properties'] as $name => $propSchema ) {
130 $schema['properties'][$name] = self::normalizeJsonSchema( $propSchema );
131 }
132 }
133
134 return $schema;
135 }
136
148 private static function getDefaultFromJsonSchema( array $schema ) {
149 $default = $schema['default'] ?? null;
150
151 foreach ( $schema['properties'] ?? [] as $name => $sch ) {
152 $def = self::getDefaultFromJsonSchema( $sch );
153
154 $default[$name] = $def;
155 }
156
157 return $default;
158 }
159
160}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81