MediaWiki master
JsonContent.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Content;
22
26
37class JsonContent extends TextContent {
38
43 protected $jsonParse;
44
50 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
51 parent::__construct( $text, $modelId );
52 }
53
62 public function getData() {
63 $this->jsonParse ??= FormatJson::parse( $this->getText() );
64 return $this->jsonParse;
65 }
66
70 public function isValid() {
71 return $this->getData()->isGood();
72 }
73
81 public function beautifyJSON() {
82 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
83 }
84
93 public function rootValueTable( $val ) {
94 if ( is_object( $val ) ) {
95 $table = $this->objectTable( $val );
96
97 } elseif ( is_array( $val ) ) {
98 // Wrap arrays in another array so that they're visually boxed in a container.
99 // Otherwise they are visually indistinguishable from a single value.
100 $table = $this->arrayTable( [ $val ] );
101
102 } else {
103 $table = Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
104 Html::rawElement( 'tbody', [],
105 Html::rawElement( 'tr', [],
106 Html::element( 'td', [], $this->primitiveValue( $val ) )
107 )
108 )
109 );
110 }
111
112 return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table );
113 }
114
121 protected function objectTable( $mapping ) {
122 $rows = [];
123 $empty = true;
124
125 foreach ( $mapping as $key => $val ) {
126 $rows[] = $this->objectRow( $key, $val );
127 $empty = false;
128 }
129 if ( $empty ) {
130 $rows[] = Html::rawElement( 'tr', [],
131 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
132 wfMessage( 'content-json-empty-object' )->text()
133 )
134 );
135 }
136 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
137 Html::rawElement( 'tbody', [], implode( '', $rows ) )
138 );
139 }
140
148 protected function objectRow( $key, $val ) {
149 $thContent = Html::element( 'span', [], $key );
150 $th = Html::rawElement( 'th', [], $thContent );
151 $td = $this->valueCell( $val );
152 return Html::rawElement( 'tr', [], $th . $td );
153 }
154
161 protected function arrayTable( $mapping ) {
162 $rows = [];
163 $empty = true;
164
165 foreach ( $mapping as $val ) {
166 $rows[] = $this->arrayRow( $val );
167 $empty = false;
168 }
169 if ( $empty ) {
170 $rows[] = Html::rawElement( 'tr', [],
171 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
172 wfMessage( 'content-json-empty-array' )->text()
173 )
174 );
175 }
176 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
177 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
178 );
179 }
180
187 protected function arrayRow( $val ) {
188 $td = $this->valueCell( $val );
189 return Html::rawElement( 'tr', [], $td );
190 }
191
198 protected function valueCell( $val ) {
199 if ( is_object( $val ) ) {
200 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
201 }
202
203 if ( is_array( $val ) ) {
204 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
205 }
206
207 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
208 }
209
216 protected function primitiveValue( $val ) {
217 if ( is_string( $val ) ) {
218 // Don't FormatJson::encode for strings since we want quotes
219 // and new lines to render visually instead of escaped.
220 return '"' . $val . '"';
221 }
222 return FormatJson::encode( $val );
223 }
224}
226class_alias( JsonContent::class, 'JsonContent' );
const CONTENT_MODEL_JSON
Definition Defines.php:253
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)