MediaWiki master
JsonContent.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Content;
8
12
23class JsonContent extends TextContent {
24
29 protected $jsonParse;
30
36 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
37 parent::__construct( $text, $modelId );
38 }
39
48 public function getData() {
49 $this->jsonParse ??= FormatJson::parse( $this->getText() );
50 return $this->jsonParse;
51 }
52
56 public function isValid() {
57 return $this->getData()->isGood();
58 }
59
67 public function beautifyJSON() {
68 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
69 }
70
79 public function rootValueTable( $val ) {
80 if ( is_object( $val ) ) {
81 $table = $this->objectTable( $val );
82
83 } elseif ( is_array( $val ) ) {
84 // Wrap arrays in another array so that they're visually boxed in a container.
85 // Otherwise they are visually indistinguishable from a single value.
86 $table = $this->arrayTable( [ $val ] );
87
88 } else {
89 $table = Html::rawElement( 'table', [ 'class' => [ 'mw-json', 'mw-json-single-value' ] ],
90 Html::rawElement( 'tbody', [],
91 Html::rawElement( 'tr', [],
92 Html::element( 'td', [], $this->primitiveValue( $val ) )
93 )
94 )
95 );
96 }
97
98 return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table );
99 }
100
107 protected function objectTable( $mapping ) {
108 $rows = [];
109 $empty = true;
110
111 foreach ( $mapping as $key => $val ) {
112 $rows[] = $this->objectRow( $key, $val );
113 $empty = false;
114 }
115 if ( $empty ) {
116 $rows[] = Html::rawElement( 'tr', [],
117 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
118 wfMessage( 'content-json-empty-object' )->text()
119 )
120 );
121 }
122 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
123 Html::rawElement( 'tbody', [], implode( '', $rows ) )
124 );
125 }
126
134 protected function objectRow( $key, $val ) {
135 $thContent = Html::element( 'span', [], $key );
136 $th = Html::rawElement( 'th', [], $thContent );
137 $td = $this->valueCell( $val );
138 return Html::rawElement( 'tr', [], $th . $td );
139 }
140
147 protected function arrayTable( $mapping ) {
148 $rows = [];
149 $empty = true;
150
151 foreach ( $mapping as $val ) {
152 $rows[] = $this->arrayRow( $val );
153 $empty = false;
154 }
155 if ( $empty ) {
156 $rows[] = Html::rawElement( 'tr', [],
157 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
158 wfMessage( 'content-json-empty-array' )->text()
159 )
160 );
161 }
162 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
163 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
164 );
165 }
166
173 protected function arrayRow( $val ) {
174 $td = $this->valueCell( $val );
175 return Html::rawElement( 'tr', [], $td );
176 }
177
184 protected function valueCell( $val ) {
185 if ( is_object( $val ) ) {
186 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
187 }
188
189 if ( is_array( $val ) ) {
190 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
191 }
192
193 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
194 }
195
202 protected function primitiveValue( $val ) {
203 if ( is_string( $val ) ) {
204 // Don't FormatJson::encode for strings since we want quotes
205 // and new lines to render visually instead of escaped.
206 return '"' . $val . '"';
207 }
208 return FormatJson::encode( $val );
209 }
210}
212class_alias( JsonContent::class, 'JsonContent' );
const CONTENT_MODEL_JSON
Definition Defines.php:239
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:43
JSON formatter wrapper class.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
element(SerializerNode $parent, SerializerNode $node, $contents)