Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
87.50% |
14 / 16 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| GadgetDefinitionContent | |
87.50% |
14 / 16 |
|
66.67% |
4 / 6 |
9.16 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isValid | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| beautifyJSON | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| getAssocArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| objectTable | |
66.67% |
2 / 3 |
|
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 | |
| 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 | /** @inheritDoc */ |
| 40 | public function isValid() { |
| 41 | // parent::isValid() is called in validate() |
| 42 | return $this->validate()->isOK(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Pretty-print JSON. |
| 47 | * |
| 48 | * If called before validation, it may return JSON "null". |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | public function beautifyJSON() { |
| 53 | // @todo we should normalize entries in module.pages |
| 54 | return FormatJson::encode( $this->getAssocArray(), "\t", FormatJson::UTF8_OK ); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Helper for isValid |
| 59 | * |
| 60 | * This placed into a separate method so that the detailed error can be communicated |
| 61 | * to editors via GadgetDefinitionContentHandler::validateSave, instead of the generic |
| 62 | * 'invalid-content-data' message from ContentHandler::validateSave based on isValid. |
| 63 | * |
| 64 | * @return Status |
| 65 | */ |
| 66 | public function validate() { |
| 67 | // Cache the validation result to avoid re-computations |
| 68 | if ( !$this->validation ) { |
| 69 | if ( !parent::isValid() ) { |
| 70 | // Invalid JSON, use the detailed Status from JsonContent::getData for syntax errors. |
| 71 | $this->validation = $this->getData(); |
| 72 | } else { |
| 73 | $validator = new GadgetDefinitionValidator(); |
| 74 | $this->validation = $validator->validate( $this->getAssocArray(), true ); |
| 75 | } |
| 76 | } |
| 77 | return $this->validation; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the JSON content as an associative array with |
| 82 | * all fields filled out, populating defaults as necessary. |
| 83 | * |
| 84 | * @return array |
| 85 | * @suppress PhanUndeclaredMethod |
| 86 | */ |
| 87 | public function getAssocArray() { |
| 88 | $info = wfObjectToArray( $this->getData()->getValue() ); |
| 89 | /** @var GadgetDefinitionContentHandler $handler */ |
| 90 | $handler = $this->getContentHandler(); |
| 91 | $info = wfArrayPlus2d( $info, $handler->getEmptyDefinition() ); |
| 92 | |
| 93 | return $info; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @inheritDoc |
| 98 | */ |
| 99 | protected function objectTable( $val ) { |
| 100 | if ( $val instanceof GadgetDefinitionContentArmor ) { |
| 101 | return (string)$val; |
| 102 | } |
| 103 | |
| 104 | return parent::objectTable( $val ); |
| 105 | } |
| 106 | } |