validate()->isOK(); } /** * Pretty-print JSON. * * If called before validation, it may return JSON "null". * * @return string */ public function beautifyJSON() { // @todo we should normalize entries in module.pages return FormatJson::encode( $this->getAssocArray(), "\t", FormatJson::UTF8_OK ); } /** * Helper for isValid * * This placed into a separate method so that the detailed error can be communicated * to editors via GadgetDefinitionContentHandler::validateSave, instead of the generic * 'invalid-content-data' message from ContentHandler::validateSave based on isValid. * * @return Status */ public function validate() { // Cache the validation result to avoid re-computations if ( !$this->validation ) { if ( !parent::isValid() ) { // Invalid JSON, use the detailed Status from JsonContent::getData for syntax errors. $this->validation = $this->getData(); } else { $validator = new GadgetDefinitionValidator(); $this->validation = $validator->validate( $this->getAssocArray(), true ); } } return $this->validation; } /** * Get the JSON content as an associative array with * all fields filled out, populating defaults as necessary. * * @return array * @suppress PhanUndeclaredMethod */ public function getAssocArray() { $info = wfObjectToArray( $this->getData()->getValue() ); /** @var GadgetDefinitionContentHandler $handler */ $handler = $this->getContentHandler(); $info = wfArrayPlus2d( $info, $handler->getEmptyDefinition() ); return $info; } /** * @inheritDoc */ protected function objectTable( $val ) { if ( $val instanceof GadgetDefinitionContentArmor ) { return (string)$val; } return parent::objectTable( $val ); } }