/**
  * Unmarshall a DOMElement object corresponding to a QTI areaMapEntry element.
  *
  * @param \DOMElement $element A DOMElement object.
  * @return \qtism\data\QtiComponent An AreaMapEntry object.
  * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException
  */
 protected function unmarshall(DOMElement $element)
 {
     if (($shape = static::getDOMElementAttributeAs($element, 'shape')) !== null) {
         $shape = Shape::getConstantByName($shape);
         if ($shape !== false) {
             if (($coords = static::getDOMElementAttributeAs($element, 'coords')) !== null) {
                 try {
                     $coords = Utils::stringToCoords($coords, $shape);
                     if (($mappedValue = static::getDOMElementAttributeAs($element, 'mappedValue', 'float')) !== null) {
                         return new AreaMapEntry($shape, $coords, $mappedValue);
                     } else {
                         $msg = "The mandatory attribute 'mappedValue' is missing from element '" . $element->localName . "'.";
                         throw new UnmarshallingException($msg, $element);
                     }
                 } catch (Exception $e) {
                     $msg = "The attribute 'coords' with value '{$coords}' is has an invalid value.";
                     throw new UnmarshallingException($msg, $element, $e);
                 }
             } else {
                 $msg = "The mandatory attribute 'coords' is missing from element '" . $element->localName . "'.";
                 throw new UnmarshallingException($msg, $element);
             }
         } else {
             $msg = "The 'shape' attribute value '{$shape}' is not a valid value to represent QTI shapes.";
             throw new UnmarshallingException($msg, $element);
         }
     } else {
         $msg = "The mandatory attribute 'shape' is missing from element '" . $element->localName . "'.";
         throw new UnmarshallingException($msg, $element);
     }
 }
Esempio n. 2
0
 /**
  * Resolve a relative $url to a canonical path based
  * on the LocalFileResolver's base path.
  *
  * @param string $url A URL to be resolved.
  * @throws \qtism\common\ResolutionException If $url cannot be resolved.
  */
 public function resolve($url)
 {
     $baseUrl = Utils::sanitizeUri($this->getBasePath());
     $baseDir = pathinfo($baseUrl, PATHINFO_DIRNAME);
     if (empty($baseDir)) {
         $msg = "The base directory of the document ('{$baseDir}') could not be resolved.";
         throw new ResolutionException($msg);
     }
     $href = $baseDir . '/' . ltrim($url, '/');
     return $href;
 }
Esempio n. 3
0
 /**
  * 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);
     }
 }
Esempio n. 4
0
 /**
  * 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 QTI inside operator element into an Inside object.
  *
  * @param DOMElement The inside element to unmarshall.
  * @param QtiComponentCollection A collection containing the child Expression objects composing the Operator.
  * @return QtiComponent An Inside object.
  * @throws UnmarshallingException
  */
 protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children)
 {
     if (($shape = static::getDOMElementAttributeAs($element, 'shape')) !== null) {
         if (($coords = static::getDOMElementAttributeAs($element, 'coords')) !== null) {
             $shape = Shape::getConstantByName($shape);
             $coords = Utils::stringToCoords($coords, $shape);
             $object = new Inside($children, $shape, $coords);
             return $object;
         } else {
             $msg = "The mandatory attribute 'coords' is missing from element '" . $element->localName . "'.";
             throw new UnmarshallingException($msg, $element);
         }
     } else {
         $msg = "The mandatory attribute 'shape' is missing from element '" . $element->localName . "'.";
         throw new UnmarshallingException($msg, $element);
     }
 }
 /**
  * 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);
     }
 }
Esempio n. 8
0
 /**
  * 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;
 }
Esempio n. 9
0
 /**
  * @dataProvider invalidUriToSanitizeProvider
  */
 public function testInvalidUriToSanitize($uri)
 {
     $this->setExpectedException('\\InvalidArgumentException');
     $uri = Utils::sanitizeUri($uri);
 }
 /**
  * Unmarshall a DOMElement object corresponding to a hotspotChoice/associableHotspot element.
  * 
  * @param DOMElement $element A DOMElement object.
  * @return QtiComponent A HotspotChoice/AssociableHotspot object.
  * @throws UnmarshallingException
  */
 protected function unmarshall(DOMElement $element)
 {
     if (($identifier = self::getDOMElementAttributeAs($element, 'identifier')) !== null) {
         if (($shape = self::getDOMElementAttributeAs($element, 'shape')) !== null) {
             if (($coords = self::getDOMElementAttributeAs($element, 'coords')) !== null) {
                 $shape = Shape::getConstantByName($shape);
                 if ($shape === false) {
                     $msg = "The value of the mandatory attribute 'shape' is not a value from the 'shape' enumeration.";
                     throw new UnmarshallingException($msg, $element);
                 }
                 try {
                     $coords = Utils::stringToCoords($coords, $shape);
                 } catch (UnexpectedValueException $e) {
                     $msg = "The coordinates 'coords' of element '" . $element->localName . "' are not valid regarding the shape they are bound to.";
                     throw new UnmarshallingException($msg, $element, $e);
                 } catch (InvalidArgumentException $e) {
                     $msg = "The coordinates 'coords' of element '" . $element->localName . "' could not be converted.";
                     throw new UnmarshallingException($msg, $element, $e);
                 }
                 if ($element->localName === 'hotspotChoice') {
                     $component = new HotspotChoice($identifier, $shape, $coords);
                 } else {
                     if (($matchMax = self::getDOMElementAttributeAs($element, 'matchMax', 'integer')) !== null) {
                         $component = new AssociableHotspot($identifier, $matchMax, $shape, $coords);
                         if (($matchMin = self::getDOMElementAttributeAs($element, 'matchMin', 'integer')) !== null) {
                             $component->setMatchMin($matchMin);
                         }
                     } else {
                         $msg = "The mandatory attribute 'matchMax' is missing from element 'associableHotspot'.";
                         throw new UnmarshallingException($msg, $element);
                     }
                 }
                 if (($hotspotLabel = self::getDOMElementAttributeAs($element, 'hotspotLabel')) !== null) {
                     $component->setHotspotLabel($hotspotLabel);
                 }
                 if (($fixed = self::getDOMElementAttributeAs($element, 'fixed', 'boolean')) !== null) {
                     $component->setFixed($fixed);
                 }
                 if (($templateIdentifier = self::getDOMElementAttributeAs($element, 'templateIdentifier')) !== null) {
                     $component->setTemplateIdentifier($templateIdentifier);
                 }
                 if (($showHide = self::getDOMElementAttributeAs($element, 'showHide')) !== null) {
                     if (($showHide = ShowHide::getConstantByName($showHide)) !== false) {
                         $component->setShowHide($showHide);
                     } else {
                         $msg = "The value of the 'showHide' attribute of element '" . $element->localName . "' is not a value from the 'showHide' enumeration.";
                         throw new UnmarshallingException($msg, $element);
                     }
                 }
                 self::fillBodyElement($component, $element);
                 return $component;
             } else {
                 $msg = "The mandatory attribute 'coords' is missing from element '" . $element->localName . "'.";
                 throw new UnmarshallingException($msg, $element);
             }
         } else {
             $msg = "The mandatory attribute 'shape' is missing from element '" . $element->localName . "'.";
             throw new UnmarshallingException($msg, $element);
         }
     } else {
         $msg = "The mandatory attribute 'identifier' is missing from element '" . $element->localName . "'.";
     }
 }
 /**
  * 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);
     }
 }