MediaWiki master
JsonContent.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Content;
12
16
25class JsonContent extends TextContent {
26
31 protected $jsonParse;
32
38 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
39 parent::__construct( $text, $modelId );
40 }
41
50 public function getData() {
51 $this->jsonParse ??= FormatJson::parse( $this->getText() );
52 return $this->jsonParse;
53 }
54
58 public function isValid() {
59 return $this->getData()->isGood();
60 }
61
69 public function beautifyJSON() {
70 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
71 }
72
81 public function rootValueTable( $val ) {
82 if ( is_object( $val ) ) {
83 $table = $this->objectTable( $val );
84
85 } elseif ( is_array( $val ) ) {
86 // Wrap arrays in another array so that they're visually boxed in a container.
87 // Otherwise they are visually indistinguishable from a single value.
88 $table = $this->arrayTable( [ $val ] );
89
90 } else {
91 $table = Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
92 Html::rawElement( 'tbody', [],
93 Html::rawElement( 'tr', [],
94 Html::element( 'td', [], $this->primitiveValue( $val ) )
95 )
96 )
97 );
98 }
99
100 return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table );
101 }
102
109 protected function objectTable( $mapping ) {
110 $rows = [];
111 $empty = true;
112
113 foreach ( $mapping as $key => $val ) {
114 $rows[] = $this->objectRow( $key, $val );
115 $empty = false;
116 }
117 if ( $empty ) {
118 $rows[] = Html::rawElement( 'tr', [],
119 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
120 wfMessage( 'content-json-empty-object' )->text()
121 )
122 );
123 }
124 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
125 Html::rawElement( 'tbody', [], implode( '', $rows ) )
126 );
127 }
128
136 protected function objectRow( $key, $val ) {
137 $thContent = Html::element( 'span', [], $key );
138 $th = Html::rawElement( 'th', [], $thContent );
139 $td = $this->valueCell( $val );
140 return Html::rawElement( 'tr', [], $th . $td );
141 }
142
149 protected function arrayTable( $mapping ) {
150 $rows = [];
151 $empty = true;
152
153 foreach ( $mapping as $val ) {
154 $rows[] = $this->arrayRow( $val );
155 $empty = false;
156 }
157 if ( $empty ) {
158 $rows[] = Html::rawElement( 'tr', [],
159 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
160 wfMessage( 'content-json-empty-array' )->text()
161 )
162 );
163 }
164 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
165 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
166 );
167 }
168
175 protected function arrayRow( $val ) {
176 $td = $this->valueCell( $val );
177 return Html::rawElement( 'tr', [], $td );
178 }
179
186 protected function valueCell( $val ) {
187 if ( is_object( $val ) ) {
188 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
189 }
190
191 if ( is_array( $val ) ) {
192 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
193 }
194
195 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
196 }
197
204 protected function primitiveValue( $val ) {
205 if ( is_string( $val ) ) {
206 // Don't FormatJson::encode for strings since we want quotes
207 // and new lines to render visually instead of escaped.
208 return '"' . $val . '"';
209 }
210 return FormatJson::encode( $val );
211 }
212}
214class_alias( JsonContent::class, 'JsonContent' );
const CONTENT_MODEL_JSON
Definition Defines.php:232
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
JSON text content that can be viewed and edit directly by users.
primitiveValue( $val)
Construct text representing a JSON primitive value.
beautifyJSON()
Pretty-print JSON.
rootValueTable( $val)
Construct HTML table representation of any JSON value.
arrayTable( $mapping)
Create HTML table representing a JSON array.
getData()
Decodes the JSON string.
__construct( $text, $modelId=CONTENT_MODEL_JSON)
objectTable( $mapping)
Create HTML table representing a JSON object.
arrayRow( $val)
Create HTML table row representing the value in an array.
valueCell( $val)
Construct HTML table cell representing any JSON value.
objectRow( $key, $val)
Create HTML table row representing one object property.
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
JSON formatter wrapper class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
element(SerializerNode $parent, SerializerNode $node, $contents)