Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
JCDefaultObjContentView | |
0.00% |
0 / 29 |
|
0.00% |
0 / 5 |
462 | |
0.00% |
0 / 1 |
valueToHtml | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
renderValue | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
42 | |||
renderTableRow | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getValueAttributes | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
isList | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | |
3 | namespace JsonConfig; |
4 | |
5 | use FormatJson; |
6 | use MediaWiki\Html\Html; |
7 | use MediaWiki\Page\PageReference; |
8 | use MediaWiki\Parser\ParserOutput; |
9 | use ParserOptions; |
10 | |
11 | /** |
12 | * This class is used in case when there is no custom view defined for JCContent object |
13 | * @package JsonConfig |
14 | */ |
15 | class JCDefaultObjContentView extends JCDefaultContentView { |
16 | |
17 | /** |
18 | * Render JCContent object as HTML |
19 | * Called from an override of AbstractContent::fillParserOutput() |
20 | * |
21 | * Render JCContent object as HTML - replaces valueToHtml() |
22 | * @param JCContent|JCObjContent $content |
23 | * @param PageReference $page Context title for parsing |
24 | * @param int|null $revId Revision ID (for {{REVISIONID}}) |
25 | * @param ParserOptions $options Parser options |
26 | * @param bool $generateHtml Whether or not to generate HTML |
27 | * @param ParserOutput &$output The output object to fill (reference). |
28 | * @return string |
29 | */ |
30 | public function valueToHtml( |
31 | JCContent $content, PageReference $page, $revId, ParserOptions $options, $generateHtml, |
32 | ParserOutput &$output |
33 | ) { |
34 | return $this->renderValue( $content, $content->getValidationData(), [] ); |
35 | } |
36 | |
37 | /** |
38 | * Constructs an HTML representation of a JSON object. |
39 | * @param JCObjContent|JCContent $content |
40 | * @param mixed|JCValue $data |
41 | * @param array $path path to this field |
42 | * @return string HTML. |
43 | */ |
44 | public function renderValue( JCContent $content, $data, array $path ) { |
45 | if ( $data instanceof JCValue ) { |
46 | $value = $data->getValue(); |
47 | if ( !is_array( $value ) && !is_object( $value ) ) { |
48 | $attribs = $this->getValueAttributes( $data ); |
49 | if ( $attribs ) { |
50 | return Html::element( 'span', $attribs, |
51 | is_string( $value ) ? $value : FormatJson::encode( $value ) ); |
52 | } |
53 | } |
54 | } else { |
55 | $value = $data; |
56 | } |
57 | return parent::renderValue( $content, $value, $path ); |
58 | } |
59 | |
60 | /** |
61 | * Convert array's key-value pair into a string of <tr><th>...</th><td>...</td></tr> elements |
62 | * @param JCObjContent|JCContent $content |
63 | * @param mixed|JCValue $data |
64 | * @param array $path path to this field |
65 | * @return string |
66 | */ |
67 | public function renderTableRow( JCContent $content, $data, array $path ) { |
68 | $attribs = $data instanceof JCValue ? $this->getValueAttributes( $data ) : null; |
69 | $content = $this->renderRowContent( $content, $data, $path ); |
70 | return Html::rawElement( 'tr', $attribs, $content ); |
71 | } |
72 | |
73 | /** |
74 | * Get CSS attributes appropriate for the status of the given data |
75 | * @param JCValue $jcv |
76 | * @internal param JCValue|mixed $data |
77 | * @return array|null |
78 | */ |
79 | public function getValueAttributes( JCValue $jcv ) { |
80 | if ( $jcv->error() ) { |
81 | return [ 'class' => 'mw-jsonconfig-error' ]; |
82 | } elseif ( $jcv->sameAsDefault() ) { |
83 | return [ 'class' => 'mw-jsonconfig-same' ]; |
84 | } elseif ( $jcv->defaultUsed() ) { |
85 | return [ 'class' => 'mw-jsonconfig-default' ]; |
86 | } elseif ( $jcv->isUnchecked() ) { |
87 | return [ 'class' => 'mw-jsonconfig-unknown' ]; |
88 | } |
89 | return null; |
90 | } |
91 | |
92 | /** |
93 | * Determine if data is a special container that needs to be rendered as a comma-separated list. |
94 | * By default, |
95 | * @param JCContent $content |
96 | * @param array|\stdClass $data |
97 | * @param array $path |
98 | * @return bool |
99 | */ |
100 | public function isList( |
101 | /** @noinspection PhpUnusedParameterInspection */ |
102 | JCContent $content, $data, array $path |
103 | ) { |
104 | if ( !is_array( $data ) ) { |
105 | return false; |
106 | } |
107 | /** @var JCValue|mixed $v */ |
108 | foreach ( $data as $k => $v ) { |
109 | $vv = $v instanceof JCValue ? $v->getValue() : $v; |
110 | if ( !is_int( $k ) || !( is_string( $vv ) || is_numeric( $vv ) ) ) { |
111 | return false; |
112 | } |
113 | } |
114 | return true; |
115 | } |
116 | } |