コード例 #1
0
 /**
  * @see \qtism\data\storage\php\marshalling\PhpMarshaller::marshall()
  */
 public function marshall()
 {
     $ctx = $this->getContext();
     $access = $ctx->getStreamAccess();
     $component = $this->getToMarshall();
     try {
         $asInstanceOf = $this->getAsInstanceOf();
         $bean = new Bean($component, false, $asInstanceOf);
         // -- Component Instantiation.
         $ctorArgs = $bean->getConstructorParameters();
         $ctorArgsCount = count($ctorArgs);
         $phpArgs = new PhpArgumentCollection();
         if ($ctorArgsCount > 0) {
             $poppedVarNames = $ctx->popFromVariableStack($ctorArgsCount);
             for ($i = 0; $i < $ctorArgsCount; $i++) {
                 $phpArgs[] = new PhpArgument(new PhpVariable($poppedVarNames[$i]));
             }
         }
         $componentVarName = $this->getVariableName();
         $componentVarName = empty($componentVarName) === true ? $ctx->generateVariableName($component) : $componentVarName;
         $access->writeVariable($componentVarName);
         $access->writeEquals($ctx->mustFormatOutput());
         $access->writeInstantiation(empty($asInstanceOf) === true ? get_class($component) : $asInstanceOf, $phpArgs);
         $access->writeSemicolon($ctx->mustFormatOutput());
         // -- Call to setters (that are not involved in the component construction).
         $setters = $bean->getSetters(true);
         $settersCount = count($setters);
         if ($settersCount > 0) {
             $poppedVarNames = $ctx->popFromVariableStack($settersCount);
             for ($i = 0; $i < $settersCount; $i++) {
                 $phpArgs = new PhpArgumentCollection();
                 $phpArgs[] = new PhpArgument(new PhpVariable($poppedVarNames[$i]));
                 $access->writeMethodCall($componentVarName, $setters[$i]->getName(), $phpArgs);
                 $access->writeSemicolon($ctx->mustFormatOutput());
             }
         }
         $ctx->pushOnVariableStack($componentVarName);
     } catch (BeanException $e) {
         $msg = "The given QtiComponent to be marshalled into PHP source code is not a strict bean.";
         throw new PhpMarshallingException($msg, PhpMarshallingException::RUNTIME, $e);
     }
 }