/** * Unmarshall a DOMElement object corresponding to a QTI variableDeclaration element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A VariableDeclaration object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { try { // identifier is a mandatory value for the variableDeclaration element. if (($identifier = static::getDOMElementAttributeAs($element, 'identifier')) !== null) { // cardinality is a mandatory value too. if (($cardinality = static::getDOMElementAttributeAs($element, 'cardinality')) !== null) { $object = new VariableDeclaration($identifier, -1, Cardinality::getConstantByName($cardinality)); // deal with baseType. $baseType = static::getDOMElementAttributeAs($element, 'baseType'); if (!empty($baseType)) { $object->setBaseType(BaseType::getConstantByName($baseType)); } // set up optional default value. $defaultValueElements = $element->getElementsByTagName('defaultValue'); if ($defaultValueElements->length == 1) { $defaultValueElement = $defaultValueElements->item(0); $defaultValueMarshaller = $this->getMarshallerFactory()->createMarshaller($defaultValueElements->item(0), array($object->getBaseType())); $object->setDefaultValue($defaultValueMarshaller->unmarshall($defaultValueElement)); } return $object; } else { $msg = "The mandatory attribute 'cardinality' is missing from element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } } else { $msg = "The mandatory attribute 'identifier' is missing from element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } } catch (InvalidArgumentException $e) { $msg = "An unexpected error occured while unmarshalling the variableDeclaration."; throw new UnmarshallingException($msg, $element, $e); } }
public function testMarshall() { $component = new VariableDeclaration('myVar', BaseType::INTEGER, Cardinality::SINGLE); $values = new ValueCollection(); $values[] = new Value(10, BaseType::INTEGER); $component->setDefaultValue(new DefaultValue($values)); $defaultValue = new DefaultValue($values); $marshaller = $this->getMarshallerFactory('2.1.0')->createMarshaller($component); $element = $marshaller->marshall($component); $this->assertInstanceOf('\\DOMElement', $element); $this->assertEquals('variableDeclaration', $element->nodeName); $this->assertEquals('myVar', $element->getAttribute('identifier')); $this->assertEquals('integer', $element->getAttribute('baseType')); $defaultValueElts = $element->getElementsByTagName('defaultValue'); $this->assertEquals(1, $defaultValueElts->length); }