/** * Unmarshall a DOMElement object corresponding to an XHTML img element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent n Img object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($src = self::getDOMElementAttributeAs($element, 'src')) !== null) { if (($alt = self::getDOMElementAttributeAs($element, 'alt')) === null) { // The XSD does not force the 'alt' attribute to be non-empty, // thus we consider the 'alt' attribute value as an empty string (''). $alt = ''; } $component = new Img($src, $alt); if (($longdesc = self::getDOMElementAttributeAs($element, 'longdesc')) !== null) { $component->setLongdesc($longdesc); } if (($height = self::getDOMElementAttributeAs($element, 'height', 'string')) !== null) { if (stripos($height, '%') === false) { $component->setHeight(intval($height)); } else { $component->setHeight($height); } } if (($width = self::getDOMElementAttributeAs($element, 'width', 'string')) !== null) { if (stripos($width, '%') === false) { $component->setWidth(intval($width)); } else { $component->setWidth($width); } } if (($xmlBase = self::getXmlBase($element)) !== false) { $component->setXmlBase($xmlBase); } $this->fillBodyElement($component, $element); return $component; } else { $msg = "The 'mandatory' attribute 'src' is missing from element 'img'."; throw new UnmarshallingException($msg, $element); } }
public function testMarshall() { $img = new Img('my/image.png', "An Image...", "my-img"); $img->setClass('beautiful'); $img->setHeight('40%'); $img->setWidth(30); $img->setLang('en-YO'); $img->setLongdesc("A Long Description..."); $marshaller = $this->getMarshallerFactory()->createMarshaller($img); $element = $marshaller->marshall($img); $dom = new DOMDocument('1.0', 'UTF-8'); $element = $dom->importNode($element, true); $this->assertEquals('<img src="my/image.png" alt="An Image..." width="30" height="40%" longdesc="A Long Description..." id="my-img" class="beautiful" xml:lang="en-YO"/>', $dom->saveXML($element)); }