Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 59 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| JsonUtil | |
0.00% |
0 / 59 |
|
0.00% |
0 / 5 |
812 | |
0.00% |
0 / 1 |
| stringToId | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| encodeForMsg | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getNewValueForType | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
72 | |||
| getType | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
110 | |||
| getSchemaArray | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * JSON Schema Validation Library |
| 4 | * |
| 5 | * Copyright (c) 2005-2012, Rob Lanphier |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are |
| 10 | * met: |
| 11 | * |
| 12 | * * Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * |
| 15 | * * Redistributions in binary form must reproduce the above |
| 16 | * copyright notice, this list of conditions and the following |
| 17 | * disclaimer in the documentation and/or other materials provided |
| 18 | * with the distribution. |
| 19 | * |
| 20 | * * Neither my name nor the names of my contributors may be used to |
| 21 | * endorse or promote products derived from this software without |
| 22 | * specific prior written permission. |
| 23 | * |
| 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 25 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 26 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 27 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 28 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 29 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 30 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 | * |
| 36 | * @author Rob Lanphier <robla@wikimedia.org> |
| 37 | * @copyright © 2011-2012 Rob Lanphier |
| 38 | * @license http://jsonwidget.org/LICENSE BSD-3-Clause |
| 39 | */ |
| 40 | |
| 41 | namespace MediaWiki\Extension\EventLogging\Libs\JsonSchemaValidation; |
| 42 | |
| 43 | use MediaWiki\Json\FormatJson; |
| 44 | |
| 45 | class JsonUtil { |
| 46 | /** |
| 47 | * Converts the string into something safe for an HTML id. |
| 48 | * performs the easiest transformation to safe id, but is lossy |
| 49 | * @param int|string $var |
| 50 | * @return string |
| 51 | * @throws JsonSchemaException |
| 52 | */ |
| 53 | public static function stringToId( $var ) { |
| 54 | if ( is_int( $var ) ) { |
| 55 | return (string)$var; |
| 56 | } |
| 57 | if ( is_string( $var ) ) { |
| 58 | return preg_replace( '/[^a-z0-9\-_:\.]/i', '', $var ); |
| 59 | } |
| 60 | |
| 61 | throw new JsonSchemaException( 'jsonschema-idconvert', self::encodeForMsg( $var ) ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Converts data to JSON format with pretty-formatting, but limited to a single line and escaped |
| 66 | * to be suitable for wikitext message parameters. |
| 67 | * @param array $data |
| 68 | * @return string |
| 69 | */ |
| 70 | public static function encodeForMsg( $data ) { |
| 71 | if ( class_exists( FormatJson::class ) && function_exists( 'wfEscapeWikiText' ) ) { |
| 72 | $json = FormatJson::encode( $data, "\t", FormatJson::ALL_OK ); |
| 73 | // Literal newlines can't appear in JSON string values, so this neatly folds the formatting |
| 74 | $json = preg_replace( "/\n\t+/", ' ', $json ); |
| 75 | return wfEscapeWikiText( $json ); |
| 76 | } |
| 77 | |
| 78 | return json_encode( $data ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Given a type (e.g. 'object', 'integer', 'string'), return the default/empty |
| 83 | * value for that type. |
| 84 | * @param string $thistype |
| 85 | * @return mixed |
| 86 | */ |
| 87 | public static function getNewValueForType( $thistype ) { |
| 88 | switch ( $thistype ) { |
| 89 | case 'object': |
| 90 | $newvalue = []; |
| 91 | break; |
| 92 | case 'array': |
| 93 | $newvalue = []; |
| 94 | break; |
| 95 | case 'number': |
| 96 | case 'integer': |
| 97 | $newvalue = 0; |
| 98 | break; |
| 99 | case 'string': |
| 100 | $newvalue = ''; |
| 101 | break; |
| 102 | case 'boolean': |
| 103 | $newvalue = false; |
| 104 | break; |
| 105 | default: |
| 106 | $newvalue = null; |
| 107 | break; |
| 108 | } |
| 109 | |
| 110 | return $newvalue; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Return a JSON-schema type for arbitrary data $foo |
| 115 | * @param mixed $foo |
| 116 | * @return mixed |
| 117 | */ |
| 118 | public static function getType( $foo ) { |
| 119 | if ( $foo === null ) { |
| 120 | return null; |
| 121 | } |
| 122 | |
| 123 | switch ( gettype( $foo ) ) { |
| 124 | case 'array': |
| 125 | $retval = 'array'; |
| 126 | foreach ( array_keys( $foo ) as $key ) { |
| 127 | if ( !is_int( $key ) ) { |
| 128 | $retval = 'object'; |
| 129 | } |
| 130 | } |
| 131 | return $retval; |
| 132 | case 'integer': |
| 133 | case 'double': |
| 134 | return 'number'; |
| 135 | case 'boolean': |
| 136 | return 'boolean'; |
| 137 | case 'string': |
| 138 | return 'string'; |
| 139 | default: |
| 140 | return null; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Generate a schema from a data example ($parent) |
| 146 | * @param mixed $parent |
| 147 | * @return array |
| 148 | */ |
| 149 | public static function getSchemaArray( $parent ) { |
| 150 | $schema = []; |
| 151 | $schema['type'] = self::getType( $parent ); |
| 152 | switch ( $schema['type'] ) { |
| 153 | case 'object': |
| 154 | $schema['properties'] = []; |
| 155 | foreach ( $parent as $name ) { |
| 156 | $schema['properties'][$name] = self::getSchemaArray( $parent[$name] ); |
| 157 | } |
| 158 | |
| 159 | break; |
| 160 | case 'array': |
| 161 | $schema['items'] = []; |
| 162 | $schema['items'][0] = self::getSchemaArray( $parent[0] ); |
| 163 | break; |
| 164 | } |
| 165 | |
| 166 | return $schema; |
| 167 | } |
| 168 | } |