MediaWiki  master
JsonContent.php
Go to the documentation of this file.
1 <?php
13 
22 class JsonContent extends TextContent {
23 
28  protected $jsonParse;
29 
35  public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
36  parent::__construct( $text, $modelId );
37  }
38 
47  public function getData() {
48  $this->jsonParse ??= FormatJson::parse( $this->getText() );
49  return $this->jsonParse;
50  }
51 
55  public function isValid() {
56  return $this->getData()->isGood();
57  }
58 
66  public function beautifyJSON() {
67  return FormatJson::encode( $this->getData()->getValue(), true, FormatJson::UTF8_OK );
68  }
69 
78  public function rootValueTable( $val ) {
79  if ( is_object( $val ) ) {
80  return $this->objectTable( $val );
81  }
82 
83  if ( 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  return $this->arrayTable( [ $val ] );
87  }
88 
89  return 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 
104  protected function objectTable( $mapping ) {
105  $rows = [];
106  $empty = true;
107 
108  foreach ( $mapping as $key => $val ) {
109  $rows[] = $this->objectRow( $key, $val );
110  $empty = false;
111  }
112  if ( $empty ) {
113  $rows[] = Html::rawElement( 'tr', [],
114  Html::element( 'td', [ 'class' => 'mw-json-empty' ],
115  wfMessage( 'content-json-empty-object' )->text()
116  )
117  );
118  }
119  return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
120  Html::rawElement( 'tbody', [], implode( '', $rows ) )
121  );
122  }
123 
131  protected function objectRow( $key, $val ) {
132  $thContent = Html::element( 'span', [], $key );
133  $th = Html::rawElement( 'th', [], $thContent );
134  $td = $this->valueCell( $val );
135  return Html::rawElement( 'tr', [], $th . $td );
136  }
137 
144  protected function arrayTable( $mapping ) {
145  $rows = [];
146  $empty = true;
147 
148  foreach ( $mapping as $val ) {
149  $rows[] = $this->arrayRow( $val );
150  $empty = false;
151  }
152  if ( $empty ) {
153  $rows[] = Html::rawElement( 'tr', [],
154  Html::element( 'td', [ 'class' => 'mw-json-empty' ],
155  wfMessage( 'content-json-empty-array' )->text()
156  )
157  );
158  }
159  return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
160  Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
161  );
162  }
163 
170  protected function arrayRow( $val ) {
171  $td = $this->valueCell( $val );
172  return Html::rawElement( 'tr', [], $td );
173  }
174 
181  protected function valueCell( $val ) {
182  if ( is_object( $val ) ) {
183  return Html::rawElement( 'td', [], $this->objectTable( $val ) );
184  }
185 
186  if ( is_array( $val ) ) {
187  return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
188  }
189 
190  return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
191  }
192 
199  protected function primitiveValue( $val ) {
200  if ( is_string( $val ) ) {
201  // Don't FormatJson::encode for strings since we want quotes
202  // and new lines to render visually instead of escaped.
203  return '"' . $val . '"';
204  }
205  return FormatJson::encode( $val );
206  }
207 }
const CONTENT_MODEL_JSON
Definition: Defines.php:213
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
const UTF8_OK
Skip escaping most characters above U+007F for readability and compactness.
Definition: FormatJson.php:36
static parse( $value, $options=0)
Decodes a JSON string.
Definition: FormatJson.php:162
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:98
JSON text content that can be viewed and edit directly by users.
Definition: JsonContent.php:22
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
Definition: JsonContent.php:28
primitiveValue( $val)
Construct text representing a JSON primitive value.
__construct( $text, $modelId=CONTENT_MODEL_JSON)
Definition: JsonContent.php:35
beautifyJSON()
Pretty-print JSON.
Definition: JsonContent.php:66
rootValueTable( $val)
Construct HTML table representation of any JSON value.
Definition: JsonContent.php:78
getData()
Decodes the JSON string.
Definition: JsonContent.php:47
arrayTable( $mapping)
Create HTML table representing a JSON array.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:57
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:58
Content object implementation for representing flat text.
Definition: TextContent.php:41
getText()
Returns the text represented by this Content object, as a string.