MediaWiki REL1_40
JsonContent.php
Go to the documentation of this file.
1<?php
12
21class JsonContent extends TextContent {
22
27 protected $jsonParse;
28
34 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
35 parent::__construct( $text, $modelId );
36 }
37
46 public function getData() {
47 $this->jsonParse ??= FormatJson::parse( $this->getText() );
48 return $this->jsonParse;
49 }
50
54 public function isValid() {
55 return $this->getData()->isGood();
56 }
57
65 public function beautifyJSON() {
66 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
67 }
68
77 public function rootValueTable( $val ) {
78 if ( is_object( $val ) ) {
79 return $this->objectTable( $val );
80 }
81
82 if ( is_array( $val ) ) {
83 // Wrap arrays in another array so that they're visually boxed in a container.
84 // Otherwise they are visually indistinguishable from a single value.
85 return $this->arrayTable( [ $val ] );
86 }
87
88 return Html::rawElement( 'table', [ 'class' => 'mw-json mw-json-single-value' ],
89 Html::rawElement( 'tbody', [],
90 Html::rawElement( 'tr', [],
91 Html::element( 'td', [], $this->primitiveValue( $val ) )
92 )
93 )
94 );
95 }
96
103 protected function objectTable( $mapping ) {
104 $rows = [];
105 $empty = true;
106
107 foreach ( $mapping as $key => $val ) {
108 $rows[] = $this->objectRow( $key, $val );
109 $empty = false;
110 }
111 if ( $empty ) {
112 $rows[] = Html::rawElement( 'tr', [],
113 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
114 wfMessage( 'content-json-empty-object' )->text()
115 )
116 );
117 }
118 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
119 Html::rawElement( 'tbody', [], implode( '', $rows ) )
120 );
121 }
122
130 protected function objectRow( $key, $val ) {
131 $thContent = Html::element( 'span', [], $key );
132 $th = Html::rawElement( 'th', [], $thContent );
133 $td = $this->valueCell( $val );
134 return Html::rawElement( 'tr', [], $th . $td );
135 }
136
143 protected function arrayTable( $mapping ) {
144 $rows = [];
145 $empty = true;
146
147 foreach ( $mapping as $val ) {
148 $rows[] = $this->arrayRow( $val );
149 $empty = false;
150 }
151 if ( $empty ) {
152 $rows[] = Html::rawElement( 'tr', [],
153 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
154 wfMessage( 'content-json-empty-array' )->text()
155 )
156 );
157 }
158 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
159 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
160 );
161 }
162
169 protected function arrayRow( $val ) {
170 $td = $this->valueCell( $val );
171 return Html::rawElement( 'tr', [], $td );
172 }
173
180 protected function valueCell( $val ) {
181 if ( is_object( $val ) ) {
182 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
183 }
184
185 if ( is_array( $val ) ) {
186 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
187 }
188
189 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
190 }
191
198 protected function primitiveValue( $val ) {
199 if ( is_string( $val ) ) {
200 // Don't FormatJson::encode for strings since we want quotes
201 // and new lines to render visually instead of escaped.
202 return '"' . $val . '"';
203 }
204 return FormatJson::encode( $val );
205 }
206}
const CONTENT_MODEL_JSON
Definition Defines.php:215
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
JSON text content that can be viewed and edit directly by users.
objectRow( $key, $val)
Create HTML table row representing one object property.
objectTable( $mapping)
Create HTML table representing a JSON object.
valueCell( $val)
Construct HTML table cell representing any JSON value.
arrayRow( $val)
Create HTML table row representing the value in an array.
Status $jsonParse
primitiveValue( $val)
Construct text representing a JSON primitive value.
__construct( $text, $modelId=CONTENT_MODEL_JSON)
beautifyJSON()
Pretty-print JSON.
rootValueTable( $val)
Construct HTML table representation of any JSON value.
getData()
Decodes the JSON string.
arrayTable( $mapping)
Create HTML table representing a JSON array.
This class is a collection of static functions that serve two purposes:
Definition Html.php:55
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:46
Content object implementation for representing flat text.
getText()
Returns the text represented by this Content object, as a string.