/** * @return bool: Whether content is valid JSON Schema. */ function isValid() { try { return parent::isValid() && $this->validate(); } catch (JsonSchemaException $e) { return false; } }
/** * Decode the JSON contents and populate protected variables */ protected function decode() { if ($this->decoded) { return; } $jsonParse = $this->getData(); $data = $jsonParse->isGood() ? $jsonParse->getValue() : null; if ($data) { if (!$this->isValid()) { $this->displaymode = 'error'; if (!parent::isValid()) { // It's not even valid json $this->errortext = htmlspecialchars($this->getNativeData()); } else { $this->errortext = FormatJson::encode($data, true, FormatJson::ALL_OK); } } else { $this->displayName = isset($data->display_name) ? $data->display_name : ''; $this->introduction = isset($data->introduction) ? $data->introduction : ''; $this->footer = isset($data->footer) ? $data->footer : ''; $this->image = isset($data->image) ? $data->image : 'none'; // Set colour to default if empty or missing if (!isset($data->colour) || $data->colour == '') { $this->themeColour = 'blue5'; } else { $this->themeColour = $data->colour; } if (isset($data->content) && is_array($data->content)) { $this->content = []; foreach ($data->content as $itemObject) { if (!is_object($itemObject)) { // Malformed item $this->content = null; break; } $item = []; $item['title'] = isset($itemObject->title) ? $itemObject->title : null; $item['image'] = isset($itemObject->image) ? $itemObject->image : null; $item['displayTitle'] = isset($itemObject->display_title) ? $itemObject->display_title : null; $this->content[] = $item; } } } } $this->decoded = true; }
/** * @dataProvider provideValidConstruction */ public function testValidConstruct($text, $modelId, $isValid, $expected) { $obj = new JsonContent($text, $modelId); $this->assertEquals($isValid, $obj->isValid()); $this->assertEquals($expected, $obj->getJsonData()); }
/** * @dataProvider provideValidConstruction */ public function testIsValid($text, $isValid, $expected) { $obj = new JsonContent($text, CONTENT_MODEL_JSON); $this->assertEquals($isValid, $obj->isValid()); $this->assertEquals($expected, $obj->getData()->getValue()); }
/** * Decode the JSON contents and populate protected variables. */ protected function decode() { if ($this->decoded) { return; } $data = $this->getData()->value; if (!$this->isValid()) { $this->displaymode = 'error'; if (!parent::isValid()) { // It's not even valid json $this->errortext = htmlspecialchars($this->getNativeData()); } else { $this->errortext = FormatJson::encode($data, true, FormatJson::ALL_OK); } } else { $this->displaymode = $data->displaymode; $this->description = $data->description; $this->options = $data->options; $this->columns = $data->columns; } $this->decoded = true; }