Пример #1
0
 /**
  * 
  * @dataProvider marshallDataProvider
  * @param string $expectedInStream
  * @param mixed $scalar
  */
 public function testMarshall($expectedInStream, $scalar)
 {
     $ctx = $this->createMarshallingContext();
     $marshaller = new PhpScalarMarshaller($ctx, $scalar);
     $marshaller->marshall();
     $this->assertEquals($expectedInStream, $this->getStream()->getBinary());
 }
 public function testIntegerCollection()
 {
     $collection = new IntegerCollection(array(10, 11, 12));
     $ctx = $this->createMarshallingContext();
     $scalarMarshaller = new PhpScalarMarshaller($ctx, $collection[0]);
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($collection[1]);
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($collection[2]);
     $scalarMarshaller->marshall();
     $collectionMarshaller = new PhpCollectionMarshaller($ctx, $collection);
     $collectionMarshaller->marshall();
     $expected = "\$integer_0 = 10;\n";
     $expected .= "\$integer_1 = 11;\n";
     $expected .= "\$integer_2 = 12;\n";
     $expected .= "\$array_0 = array(\$integer_0, \$integer_1, \$integer_2);\n";
     $expected .= "\$integercollection_0 = new qtism\\common\\collections\\IntegerCollection(\$array_0);\n";
     $this->assertEquals($expected, $this->getStream()->getBinary());
 }
 /**
  * Save the PhpDocument to a specific location.
  * 
  * @param string $url A URL (Uniform Resource Locator) describing where to save the document.
  * @throws PhpStorageException If an error occurs while saving.
  */
 public function save($url)
 {
     $stack = new SplStack();
     $stack->push($this->getDocumentComponent());
     // 1st/2nd pass marker.
     $marker = array();
     try {
         // marshalling context.
         $stream = new MemoryStream();
         $stream->open();
         $streamAccess = new PhpStreamAccess($stream);
         $streamAccess->writeOpeningTag();
         $ctx = new PhpMarshallingContext($streamAccess);
         $ctx->setFormatOutput(true);
         while (count($stack) > 0) {
             $component = $stack->pop();
             $isMarked = in_array($component, $marker, true);
             if ($isMarked === false && $component instanceof QtiComponent) {
                 // -- QtiComponent node, 1st pass.
                 // Mark as explored.
                 array_push($marker, $component);
                 // Ask for a 2nd pass.
                 $stack->push($component);
                 // Let's look at the Bean properties and ask for a future exploration.
                 $bean = new Bean($component, false, self::getBaseImplementation($component));
                 $ctorGetters = $bean->getConstructorGetters();
                 $bodyGetters = $bean->getGetters(true);
                 $getters = array_reverse(array_merge($bodyGetters->getArrayCopy(), $ctorGetters->getArrayCopy()));
                 foreach ($getters as $getter) {
                     $stack->push(call_user_func(array($component, $getter->getName())));
                 }
             } else {
                 if ($isMarked === false && ($component instanceof AbstractCollection && !$component instanceof Coords)) {
                     // AbstractCollection node, 1st pass.
                     // Mark as explored.
                     array_push($marker, $component);
                     // Ask for a 2nd pass.
                     $stack->push($component);
                     // Explore all values of the collection please!
                     $values = array_reverse($component->getArrayCopy());
                     foreach ($values as $val) {
                         $stack->push($val);
                     }
                 } else {
                     if ($isMarked === true && $component instanceof QtiComponent) {
                         // QtiComponent, 2nd pass.
                         $marshaller = new PhpQtiComponentMarshaller($ctx, $component);
                         $marshaller->setAsInstanceOf(self::getBaseImplementation($component));
                         if ($component === $this->getDocumentComponent()) {
                             $marshaller->setVariableName('rootcomponent');
                         }
                         $marshaller->marshall();
                     } else {
                         if ($component instanceof QtiDatatype) {
                             // Leaf node QtiDataType.
                             $marshaller = new PhpQtiDatatypeMarshaller($ctx, $component);
                             $marshaller->marshall();
                         } else {
                             if ($isMarked === true && $component instanceof AbstractCollection) {
                                 // AbstractCollection, 2nd pass.
                                 $marshaller = new PhpCollectionMarshaller($ctx, $component);
                                 $marshaller->marshall();
                             } else {
                                 if (PhpUtils::isScalar($component) === true) {
                                     // Leaf node (QtiDatatype or PHP scalar (including the null value)).
                                     $marshaller = new PhpScalarMarshaller($ctx, $component);
                                     $marshaller->marshall();
                                 } else {
                                     if (is_array($component) === true) {
                                         // Leaf node array.
                                         $marshaller = new PhpArrayMarshaller($ctx, $component);
                                         $marshaller->marshall();
                                     } else {
                                         $msg = "Datatype '" . gettype($component) . "' cannot be handled by the PhpDocument::save() method.";
                                         throw new PhpStorageException($msg);
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
         file_put_contents($url, $stream->getBinary());
     } catch (StreamAccessException $e) {
         $msg = "An error occured while writing the PHP source code stream.";
         throw new PhpStorageException($msg, 0, $e);
     }
 }
 public function testOnlyScalarPropertiesConstructorAndProperties()
 {
     $component = new ItemSessionControl();
     $ctx = $this->createMarshallingContext();
     $scalarMarshaller = new PhpScalarMarshaller($ctx, $component->getMaxAttempts());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->mustShowFeedback());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->doesAllowReview());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->mustShowSolution());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->doesAllowComment());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->mustValidateResponses());
     $scalarMarshaller->marshall();
     $scalarMarshaller->setToMarshall($component->doesAllowSkipping());
     $scalarMarshaller->marshall();
     $componentMarshaller = new PhpQtiComponentMarshaller($ctx, $component);
     $componentMarshaller->marshall();
     $expected = "\$integer_0 = 1;\n";
     $expected .= "\$boolean_0 = false;\n";
     $expected .= "\$boolean_1 = true;\n";
     $expected .= "\$boolean_2 = false;\n";
     $expected .= "\$boolean_3 = false;\n";
     $expected .= "\$boolean_4 = false;\n";
     $expected .= "\$boolean_5 = true;\n";
     $expected .= "\$itemsessioncontrol_0 = new qtism\\data\\ItemSessionControl();\n";
     $expected .= "\$itemsessioncontrol_0->setMaxAttempts(\$integer_0);\n";
     $expected .= "\$itemsessioncontrol_0->setShowFeedback(\$boolean_0);\n";
     $expected .= "\$itemsessioncontrol_0->setAllowReview(\$boolean_1);\n";
     $expected .= "\$itemsessioncontrol_0->setShowSolution(\$boolean_2);\n";
     $expected .= "\$itemsessioncontrol_0->setAllowComment(\$boolean_3);\n";
     $expected .= "\$itemsessioncontrol_0->setValidateResponses(\$boolean_4);\n";
     $expected .= "\$itemsessioncontrol_0->setAllowSkipping(\$boolean_5);\n";
     $this->assertEquals($expected, $this->getStream()->getBinary());
 }