/** * Unmarshall a DOMElement object corresponding to a QTI baseValue element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A BaseValue object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($baseType = static::getDOMElementAttributeAs($element, 'baseType', 'string')) !== null) { $value = $element->nodeValue; $baseTypeCst = BaseType::getConstantByName($baseType); $object = new BaseValue($baseTypeCst, Utils::stringToDatatype($value, $baseTypeCst)); return $object; } else { $msg = "The mandatory attribute 'baseType' is missing from element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } }
/** * Unmarshall a DOMElement object corresponding to a QTI timeLimits element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A TimeLimits object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException If the attribute 'allowLateSubmission' is not a valid boolean value. */ protected function unmarshall(DOMElement $element) { $object = new TimeLimits(); if (($value = static::getDOMElementAttributeAs($element, 'minTime', 'string')) !== null) { $object->setMinTime(StorageUtils::stringToDatatype("PT{$value}S", BaseType::DURATION)); } if (($value = static::getDOMElementAttributeAs($element, 'maxTime', 'string')) !== null) { $object->setMaxTime(StorageUtils::stringToDatatype("PT{$value}S", BaseType::DURATION)); } if (($value = static::getDOMElementAttributeAs($element, 'allowLateSubmission', 'boolean')) !== null) { $object->setAllowLateSubmission($value); } return $object; }
/** * Unmarshall a DOMElement object corresponding to a QTI MatchTable element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent A MatchTable object. * @throws UnmarshallingException If the $element to unmarshall has no matchTableEntry children. */ protected function unmarshall(DOMElement $element) { $matchTableEntryElements = $element->getElementsByTagName('matchTableEntry'); if ($matchTableEntryElements->length > 0) { $matchTableEntries = new MatchTableEntryCollection(); for ($i = 0; $i < $matchTableEntryElements->length; $i++) { $marshaller = $this->getMarshallerFactory()->createMarshaller($matchTableEntryElements->item($i), array($this->getBaseType())); $matchTableEntries[] = $marshaller->unmarshall($matchTableEntryElements->item($i)); } $object = new MatchTable($matchTableEntries); if (($defaultValue = static::getDOMElementAttributeAs($element, 'defaultValue')) !== null) { try { $defaultValue = Utils::stringToDatatype($defaultValue, $this->getBaseType()); $object->setDefaultValue($defaultValue); } catch (InvalidArgumentException $e) { $strType = BaseType::getNameByConstant($this->getBaseType()); $msg = "Unable to transform '{$defaultValue}' in a {$strType}."; throw new UnmarshallingException($msg, $element, $e); } } return $object; } else { $msg = "A QTI matchTable element must contain at least one matchTableEntry element."; throw new UnmarshallingException($msg, $element); } }
/** * Unmarshall a DOMElement object corresponding to a QTI MatchTableEntry element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent A MatchTableEntry object. * @throws UnmarshallingException If the mandatory attributes 'sourceValue' or 'targetValue' are missing from $element. */ protected function unmarshall(DOMElement $element) { if (($sourceValue = static::getDOMElementAttributeAs($element, 'sourceValue', 'integer')) !== null) { if (($targetValue = static::getDOMElementAttributeAs($element, 'targetValue', 'string')) !== null) { $object = new MatchTableEntry($sourceValue, Utils::stringToDatatype($targetValue, $this->getBaseType()), $this->getBaseType()); return $object; } else { $msg = "The mandatory attribute 'targetValue' is missing."; throw new InvalidArgumentException($msg, $element); } } else { $msg = "The mandatory attribute 'sourceValue' is missing."; throw new UnmarshallingException($msg, $element); } }
/** * Unmarshall a DOMElement object corresponding to a QTI Value element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A Value object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException If the 'baseType' attribute is not a valid QTI baseType. */ protected function unmarshall(DOMElement $element) { $object = null; if (($baseType = static::getDOMElementAttributeAs($element, 'baseType', 'string')) !== null) { // baseType attribute is set -> part of a record. $baseTypeCst = BaseType::getConstantByName($baseType); if ($baseTypeCst !== false) { $object = new Value(Utils::stringToDatatype(trim($element->nodeValue), $baseTypeCst), $baseTypeCst); $object->setPartOfRecord(true); } else { $msg = "The 'baseType' attribute value ('{$value}') is not a valid QTI baseType in element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } } else { // baseType attribute not set -> not part of a record. $nodeValue = trim($element->nodeValue); if ($nodeValue !== '') { // Try to use the marshaller as parametric to know how to unserialize the value. if ($this->getBaseType() != -1) { $object = new Value(Utils::stringToDatatype($nodeValue, $this->getBaseType()), $this->getBaseType()); } else { // value used as plain string (at your own risks). $object = new Value($nodeValue); } } else { $msg = "The element '" . $element->localName . "' has no value."; throw new UnmarshallingException($msg, $element); } } if (($value = static::getDOMElementAttributeAs($element, 'fieldIdentifier', 'string')) !== null) { $object->setFieldIdentifier($value); } return $object; }
/** * @dataProvider invalidPairProvider */ public function testStringToDirectedPairInvalid($string) { $this->setExpectedException('\\UnexpectedValueException'); $value = Utils::stringToDatatype($string, BaseType::PAIR); }
/** * Unmarshall a DOMElement object corresponding to a QTI InterpolationEntry element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent An InterpolationTableEntry object. * @throws UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($sourceValue = static::getDOMElementAttributeAs($element, 'sourceValue', 'float')) !== null) { if (($targetValue = static::getDOMElementAttributeAs($element, 'targetValue', 'string')) !== null) { $object = new InterpolationTableEntry($sourceValue, Utils::stringToDatatype($targetValue, $this->getBaseType())); if (($includeBoundary = static::getDOMElementAttributeAs($element, 'includeBoundary', 'boolean')) !== null) { $object->setIncludeBoundary($includeBoundary); } return $object; } } else { $msg = "The mandatory attribute 'sourceValue' is missing from element '" . $element->localName . "'."; throw new UnmarshallingException($msg, $element); } }
/** * Unmarshall a DOMElement object corresponding to a QTI mapEntry element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent A MapEntry object. * @throws UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($mapKey = static::getDOMElementAttributeAs($element, 'mapKey')) !== null) { try { $mapKey = Utils::stringToDatatype($mapKey, $this->getBaseType()); if (($mappedValue = static::getDOMElementAttributeAs($element, 'mappedValue', 'float')) !== null) { $object = new MapEntry($mapKey, $mappedValue); if (($caseSensitive = static::getDOMElementAttributeAs($element, 'caseSensitive', 'boolean')) !== null) { $object->setCaseSensitive($caseSensitive); } return $object; } else { $msg = "The mandatory 'mappedValue' attribute is missing from element '" . $element->nodName . "'."; throw new UnmarshallingException($msg, $element); } } catch (UnexpectedValueException $e) { $msg = "The value of the 'mapKey' attribute '{$mapKey}' could not be converted to qti:valueType."; throw new UnmarshallingException($msg, $element, $e); } } else { $msg = "The mandatory 'mapKey' attribute is missing from the '" . $element->localName . "' element"; throw new UnmarshallingException($msg, $element); } }