/**
  * 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);
 }
 public function __construct($identifier, $baseType = -1, $cardinality = Cardinality::SINGLE, DefaultValue $defaultValue = null)
 {
     parent::__construct($identifier, $baseType, $cardinality, $defaultValue);
 }
Пример #4
0
 /**
  * @see \qtism\data\state\VariableDeclaration::getComponents()
  */
 public function getComponents()
 {
     $comp = parent::getComponents()->getArrayCopy();
     if ($this->getAreaMapping() !== null) {
         $comp[] = $this->getAreaMapping();
     }
     if ($this->getMapping() !== null) {
         $comp[] = $this->getMapping();
     }
     if ($this->getCorrectResponse() !== null) {
         $comp[] = $this->getCorrectResponse();
     }
     return new QtiComponentCollection($comp);
 }
Пример #5
0
 /**
  * @see \qtism\data\state\VariableDeclaration::getComponents()
  */
 public function getComponents()
 {
     $comp = parent::getComponents()->getArrayCopy();
     if ($this->getLookupTable() !== null) {
         $comp[] = $this->getLookupTable();
     }
     return new QtiComponentCollection($comp);
 }
Пример #6
0
 /**
  * Create a runtime Variable object from its Data Model representation.
  *
  * @param \qtism\data\state\VariableDeclaration $variableDeclaration A VariableDeclaration object from the QTI Data Model.
  * @return \qtism\runtime\common\Variable A Variable object.
  * @throws \UnexpectedValueException If $variableDeclaration is not consistent.
  */
 public static function createFromDataModel(VariableDeclaration $variableDeclaration)
 {
     $identifier = $variableDeclaration->getIdentifier();
     $baseType = $variableDeclaration->getBaseType();
     $cardinality = $variableDeclaration->getCardinality();
     $variable = new static($identifier, $cardinality, $baseType);
     // Default value?
     $dataModelDefaultValue = $variableDeclaration->getDefaultValue();
     if (!empty($dataModelDefaultValue)) {
         $dataModelValues = $dataModelDefaultValue->getValues();
         $defaultValue = static::dataModelValuesToRuntime($dataModelValues, $baseType, $cardinality);
         $variable->setDefaultValue($defaultValue);
     }
     return $variable;
 }