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 MediaWiki\Content\JsonContent;
24use MediaWiki\Json\FormatJson;
25use MediaWiki\Status\Status;
26use Wikimedia\ArrayUtils\ArrayUtils;
27
28class GadgetDefinitionContent extends JsonContent {
29
30    /** @var Status|null Cached validation result */
31    private $validation;
32
33    /**
34     * @param string $text
35     */
36    public function __construct( $text ) {
37        parent::__construct( $text, 'GadgetDefinition' );
38    }
39
40    /** @inheritDoc */
41    public function isValid() {
42        // parent::isValid() is called in validate()
43        return $this->validate()->isOK();
44    }
45
46    /**
47     * Pretty-print JSON.
48     *
49     * If called before validation, it may return JSON "null".
50     *
51     * @return string
52     */
53    public function beautifyJSON() {
54        // @todo we should normalize entries in module.pages
55        return FormatJson::encode( $this->getAssocArray(), "\t", FormatJson::UTF8_OK );
56    }
57
58    /**
59     * Helper for isValid
60     *
61     * This placed into a separate method so that the detailed error can be communicated
62     * to editors via GadgetDefinitionContentHandler::validateSave, instead of the generic
63     * 'invalid-content-data' message from ContentHandler::validateSave based on isValid.
64     *
65     * @return Status
66     */
67    public function validate() {
68        // Cache the validation result to avoid re-computations
69        if ( !$this->validation ) {
70            if ( !parent::isValid() ) {
71                // Invalid JSON, use the detailed Status from JsonContent::getData for syntax errors.
72                $this->validation = $this->getData();
73            } else {
74                $validator = new GadgetDefinitionValidator();
75                $this->validation = $validator->validate( $this->getAssocArray(), true );
76            }
77        }
78        return $this->validation;
79    }
80
81    /**
82     * Get the JSON content as an associative array with
83     * all fields filled out, populating defaults as necessary.
84     *
85     * @return array
86     * @suppress PhanUndeclaredMethod
87     */
88    public function getAssocArray() {
89        $info = wfObjectToArray( $this->getData()->getValue() );
90        /** @var GadgetDefinitionContentHandler $handler */
91        $handler = $this->getContentHandler();
92        $info = ArrayUtils::arrayPlus2d( $info, $handler->getEmptyDefinition() );
93
94        return $info;
95    }
96
97    /**
98     * @inheritDoc
99     */
100    protected function objectTable( $val ) {
101        if ( $val instanceof GadgetDefinitionContentArmor ) {
102            return (string)$val;
103        }
104
105        return parent::objectTable( $val );
106    }
107}