/**
  * @see \qtism\data\storage\xml\marshalling\RecursiveMarshaller::unmarshallChildrenKnown()
  */
 protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children)
 {
     $expressionElts = self::getChildElementsByTagName($element, Expression::getExpressionClassNames());
     if (count($expressionElts) > 0) {
         $marshaller = $this->getMarshallerFactory()->createMarshaller($expressionElts[0]);
         $expression = $marshaller->unmarshall($expressionElts[0]);
     } elseif (($element->localName == 'responseIf' || $element->localName == 'responseElseIf') && count($expressionElts) == 0) {
         $msg = "A '" . $element->localName . "' must contain an 'expression' element. None found at line " . $element->getLineNo() . "'.";
         throw new UnmarshallingException($msg, $element);
     }
     if ($element->localName == 'responseIf' || $element->localName == 'responseElseIf') {
         $className = 'qtism\\data\\rules\\' . ucfirst($element->localName);
         $class = new ReflectionClass($className);
         $object = Reflection::newInstance($class, array($expression, $children));
     } else {
         $className = 'qtism\\data\\rules\\' . ucfirst($element->localName);
         $class = new ReflectionClass($className);
         $object = Reflection::newInstance($class, array($children));
     }
     return $object;
 }
 protected function instantiateMarshaller(ReflectionClass $class, array $args)
 {
     array_unshift($args, '2.0.0');
     return Reflection::newInstance($class, $args);
 }
Beispiel #3
0
 public function testNewInstanceWithoutArguments()
 {
     $clazz = new ReflectionClass('\\stdClass');
     $instance = Reflection::newInstance($clazz);
     $this->assertInstanceOf('\\stdClass', $instance);
 }
Beispiel #4
0
 /**
  * @see \qtism\data\storage\xml\marshalling\RecursiveMarshaller::unmarshallChildrenKnown()
  */
 protected function unmarshallChildrenKnown(DOMElement $element, QtiComponentCollection $children)
 {
     // Some exceptions applies on instanciation e.g. the And operator is named
     // AndOperator because of PHP reserved words restriction.
     if ($element->localName === 'and') {
         $className = 'qtism\\data\\expressions\\operators\\AndOperator';
     } elseif ($element->localName === 'or') {
         $className = 'qtism\\data\\expressions\\operators\\OrOperator';
     } else {
         $className = 'qtism\\data\\expressions\\operators\\' . ucfirst($element->localName);
     }
     $class = new ReflectionClass($className);
     $params = array($children);
     if ($element->localName === 'customOperator') {
         // Retrieve XML content as a string.
         $frag = $element->ownerDocument->createDocumentFragment();
         $element = $element->cloneNode(true);
         $frag->appendChild($element);
         $params[] = $frag->ownerDocument->saveXML($frag);
         $component = Reflection::newInstance($class, $params);
         if (($class = self::getDOMElementAttributeAs($element, 'class')) !== null) {
             $component->setClass($class);
         }
         if (($definition = self::getDOMElementAttributeAs($element, 'definition')) !== null) {
             $component->setDefinition($definition);
         }
         return $component;
     } else {
         return Reflection::newInstance($class, $params);
     }
 }
 /**
  * Create a marshaller for a given QtiComponent or DOMElement object, depending on the current mapping
  * of the MarshallerFactory. If no mapping entry can be found, the factory will perform a ultimate
  * trial in the qtism\\data\\storage\\xml\\marshalling namespace to find the relevant Marshaller object.
  * 
  * The newly created marshaller will be set up with the MarshallerFactory itself as its MarshallerFactory
  * object (yes, we know, this is highly recursive but necessary x)).
  * 
  * @param DOMElement|QtiComponent $object A QtiComponent or DOMElement object you want to get the corresponding Marshaller object.
  * @param array $args An optional array of arguments to be passed to the Marshaller constructor.
  * @throws InvalidArgumentException If $object is not a QtiComponent nor a DOMElement object.
  * @throws RuntimeException If no Marshaller object can be created for the given $object.
  * @return Marshaller The corresponding Marshaller object.
  */
 public function createMarshaller($object, array $args = array())
 {
     if ($object instanceof QtiComponent) {
         $qtiClassName = $object->getQtiClassName();
     } else {
         if ($object instanceof DOMElement) {
             $qtiClassName = $object->localName;
         }
     }
     if (isset($qtiClassName)) {
         try {
             // Look for a mapping entry.
             if ($this->hasMappingEntry($qtiClassName)) {
                 $class = new ReflectionClass($this->getMappingEntry($qtiClassName));
             } else {
                 // Look for default.
                 $className = 'qtism\\data\\storage\\xml\\marshalling\\' . ucfirst($qtiClassName) . 'Marshaller';
                 $class = new ReflectionClass($className);
             }
         } catch (ReflectionException $e) {
             $msg = "No marshaller implementation could be found for component '{$qtiClassName}'.";
             throw new RuntimeException($msg, 0, $e);
         }
         $marshaller = Reflection::newInstance($class, $args);
         $marshaller->setMarshallerFactory($this);
         return $marshaller;
     } else {
         $msg = "The object argument must be a QtiComponent or a DOMElementObject, '" . gettype($object) . "' given.";
         throw new InvalidArgumentException($msg);
     }
 }