Debian Codegraph @

extensions/Gadgets/includes/Content/GadgetDefinitionContent.php

raw symbols SCIP
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
21 namespace MediaWiki\Extension\Gadgets\Content;
22
23 use MediaWiki\Content\JsonContent;
24 use MediaWiki\Json\FormatJson;
25 use MediaWiki\Status\Status;
26
27 class 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 * Helper for isValid
58 *
59 * This placed into a separate method so that the detailed error can be communicated
60 * to editors via GadgetDefinitionContentHandler::validateSave, instead of the generic
61 * 'invalid-content-data' message from ContentHandler::validateSave based on isValid.
62 *
63 * @return Status
64 */
65 public function validate() {
66 // Cache the validation result to avoid re-computations
67 if ( !$this->validation ) {
68 if ( !parent::isValid() ) {
69 // Invalid JSON, use the detailed Status from JsonContent::getData for syntax errors.
70 $this->validation = $this->getData();
71 } else {
72 $validator = new GadgetDefinitionValidator();
73 $this->validation = $validator->validate( $this->getAssocArray(), true );
74 }
75 }
76 return $this->validation;
77 }
78
79 /**
80 * Get the JSON content as an associative array with
81 * all fields filled out, populating defaults as necessary.
82 *
83 * @return array
84 * @suppress PhanUndeclaredMethod
85 */
86 public function getAssocArray() {
87 $info = wfObjectToArray( $this->getData()->getValue() );
88 /** @var GadgetDefinitionContentHandler $handler */
89 $handler = $this->getContentHandler();
90 $info = wfArrayPlus2d( $info, $handler->getEmptyDefinition() );
91
92 return $info;
93 }
94
95 /**
96 * @inheritDoc
97 */
98 protected function objectTable( $val ) {
99 if ( $val instanceof GadgetDefinitionContentArmor ) {
100 return (string)$val;
101 }
102
103 return parent::objectTable( $val );
104 }
105 }