Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
14 / 16
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
GadgetDefinitionContent
87.50% covered (warning)
87.50%
14 / 16
66.67% covered (warning)
66.67%
4 / 6
9.16
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isValid
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 beautifyJSON
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 getAssocArray
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 objectTable
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\Gadgets\Content;
22
23use FormatJson;
24use JsonContent;
25use MediaWiki\Status\Status;
26
27class GadgetDefinitionContent extends JsonContent {
28
29    /** @var Status|null Cached validation result */
30    private $validation;
31
32    /**
33     * @param string $text
34     */
35    public function __construct( $text ) {
36        parent::__construct( $text, 'GadgetDefinition' );
37    }
38
39    public function isValid() {
40        // parent::isValid() is called in validate()
41        return $this->validate()->isOK();
42    }
43
44    /**
45     * Pretty-print JSON.
46     *
47     * If called before validation, it may return JSON "null".
48     *
49     * @return string
50     */
51    public function beautifyJSON() {
52        // @todo we should normalize entries in module.pages
53        return FormatJson::encode( $this->getAssocArray(), "\t", FormatJson::UTF8_OK );
54    }
55
56    /**
57     * @return Status
58     */
59    public function validate() {
60        // Cache the validation result to avoid re-computations
61        if ( !$this->validation ) {
62            if ( !parent::isValid() ) {
63                $this->validation = $this->getData();
64            } else {
65                $validator = new GadgetDefinitionValidator();
66                $this->validation = $validator->validate( $this->getAssocArray(), true );
67            }
68        }
69        return $this->validation;
70    }
71
72    /**
73     * Get the JSON content as an associative array with
74     * all fields filled out, populating defaults as necessary.
75     *
76     * @return array
77     * @suppress PhanUndeclaredMethod
78     */
79    public function getAssocArray() {
80        $info = wfObjectToArray( $this->getData()->getValue() );
81        /** @var GadgetDefinitionContentHandler $handler */
82        $handler = $this->getContentHandler();
83        $info = wfArrayPlus2d( $info, $handler->getEmptyDefinition() );
84
85        return $info;
86    }
87
88    /**
89     * @inheritDoc
90     */
91    protected function objectTable( $val ) {
92        if ( $val instanceof GadgetDefinitionContentArmor ) {
93            return (string)$val;
94        }
95
96        return parent::objectTable( $val );
97    }
98}