Пример #1
0
 /**
  * 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|\qtism\data\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 \qtism\data\storage\xml\marshalling\Marshaller The corresponding Marshaller object.
  */
 public function createMarshaller($object, array $args = array())
 {
     if ($object instanceof QtiComponent) {
         $qtiClassName = $object->getQtiClassName();
     } elseif ($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 {
                 // No qtiClassName/mapping entry found.
                 $msg = "No mapping entry found for QTI class name '{$qtiClassName}'.";
                 throw new MarshallerNotFoundException($msg, $qtiClassName);
             }
         } catch (ReflectionException $e) {
             $msg = "No marshaller implementation could be found for component '{$qtiClassName}'.";
             throw new MarshallerNotFoundException($msg, $qtiClassName, $e);
         }
         $marshaller = $this->instantiateMarshaller($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);
     }
 }