Merge "Use explicit nullable type on parameter arguments"
[mediawiki.git] / includes / content / JsonContent.php
blobf449b2e0e0808a616add866917d30a85b5ba8e10
1 <?php
2 /**
3 * JSON Content Model
5 * @file
7 * @author Ori Livneh <ori@wikimedia.org>
8 * @author Kunal Mehta <legoktm@gmail.com>
9 */
11 namespace MediaWiki\Content;
13 use MediaWiki\Html\Html;
14 use MediaWiki\Json\FormatJson;
15 use MediaWiki\Status\Status;
17 /**
18 * JSON text content that can be viewed and edit directly by users.
20 * @since 1.24
21 * @newable
22 * @stable to extend
23 * @ingroup Content
25 class JsonContent extends TextContent {
27 /**
28 * @since 1.25
29 * @var Status
31 protected $jsonParse;
33 /**
34 * @param string $text JSON
35 * @param string $modelId
36 * @stable to call
38 public function __construct( $text, $modelId = CONTENT_MODEL_JSON ) {
39 parent::__construct( $text, $modelId );
42 /**
43 * Decodes the JSON string.
45 * Note that this parses it without casting objects to associative arrays.
46 * Objects and arrays are kept as distinguishable types in the PHP values.
48 * @return Status
50 public function getData() {
51 $this->jsonParse ??= FormatJson::parse( $this->getText() );
52 return $this->jsonParse;
55 /**
56 * @return bool Whether content is valid.
58 public function isValid() {
59 return $this->getData()->isGood();
62 /**
63 * Pretty-print JSON.
65 * If called before validation, it may return JSON "null".
67 * @return string
69 public function beautifyJSON() {
70 return FormatJson::encode( $this->getData()->getValue(), "\t", FormatJson::UTF8_OK );
73 /**
74 * Construct HTML table representation of any JSON value.
76 * See also valueCell, which is similar.
78 * @param mixed $val
79 * @return string HTML.
81 public function rootValueTable( $val ) {
82 if ( is_object( $val ) ) {
83 $table = $this->objectTable( $val );
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 ] );
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 ) )
100 return Html::rawElement( 'div', [ 'class' => 'noresize' ], $table );
104 * Create HTML table representing a JSON object.
106 * @param \stdClass $mapping
107 * @return string HTML
109 protected function objectTable( $mapping ) {
110 $rows = [];
111 $empty = true;
113 foreach ( $mapping as $key => $val ) {
114 $rows[] = $this->objectRow( $key, $val );
115 $empty = false;
117 if ( $empty ) {
118 $rows[] = Html::rawElement( 'tr', [],
119 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
120 wfMessage( 'content-json-empty-object' )->text()
124 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
125 Html::rawElement( 'tbody', [], implode( '', $rows ) )
130 * Create HTML table row representing one object property.
132 * @param string $key
133 * @param mixed $val
134 * @return string HTML.
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 );
144 * Create HTML table representing a JSON array.
146 * @param array $mapping
147 * @return string HTML
149 protected function arrayTable( $mapping ) {
150 $rows = [];
151 $empty = true;
153 foreach ( $mapping as $val ) {
154 $rows[] = $this->arrayRow( $val );
155 $empty = false;
157 if ( $empty ) {
158 $rows[] = Html::rawElement( 'tr', [],
159 Html::element( 'td', [ 'class' => 'mw-json-empty' ],
160 wfMessage( 'content-json-empty-array' )->text()
164 return Html::rawElement( 'table', [ 'class' => 'mw-json' ],
165 Html::rawElement( 'tbody', [], implode( "\n", $rows ) )
170 * Create HTML table row representing the value in an array.
172 * @param mixed $val
173 * @return string HTML.
175 protected function arrayRow( $val ) {
176 $td = $this->valueCell( $val );
177 return Html::rawElement( 'tr', [], $td );
181 * Construct HTML table cell representing any JSON value.
183 * @param mixed $val
184 * @return string HTML.
186 protected function valueCell( $val ) {
187 if ( is_object( $val ) ) {
188 return Html::rawElement( 'td', [], $this->objectTable( $val ) );
191 if ( is_array( $val ) ) {
192 return Html::rawElement( 'td', [], $this->arrayTable( $val ) );
195 return Html::element( 'td', [ 'class' => 'mw-json-value' ], $this->primitiveValue( $val ) );
199 * Construct text representing a JSON primitive value.
201 * @param mixed $val
202 * @return string Text.
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 . '"';
210 return FormatJson::encode( $val );
213 /** @deprecated class alias since 1.43 */
214 class_alias( JsonContent::class, 'JsonContent' );