public function testCreateFromDataModel()
 {
     $valueCollection = new ValueCollection();
     $value = new Value(15, BaseType::INTEGER);
     $value->setPartOfRecord(true);
     $value->setFieldIdentifier("val1");
     $valueCollection[] = $value;
     $value = new Value('string', BaseType::STRING);
     $value->setPartOfRecord(true);
     $value->setFieldIdentifier("val2");
     $valueCollection[] = $value;
     $record = RecordContainer::createFromDataModel($valueCollection);
     $this->assertInstanceOf('qtism\\runtime\\common\\RecordContainer', $record);
     $this->assertEquals(2, count($record));
     $this->assertEquals(15, $record['val1']->getValue());
     $this->assertEquals('string', $record['val2']->getValue());
 }
Beispiel #2
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;
 }