/**
  * Write a scalar value into the current stream. 
  * 
  * If the given value is a string, it will be output surrounded by double quotes (") characters.
  * 
  * @param mixed $scalar A PHP scalar value or null.
  * @throws InvalidArgumentException If $scalar is not a PHP scalar value nor null.
  * @throws StreamAccessException If an error occurs while writing the scalar value.
  */
 public function writeScalar($scalar)
 {
     if (Utils::isScalar($scalar) === false) {
         $msg = "A '" . gettype($scalar) . "' value is not a PHP scalar value nor null.";
         throw new InvalidArgumentException($msg);
     }
     try {
         if (is_int($scalar) === true) {
             $this->getStream()->write($scalar);
         } else {
             if (is_double($scalar) === true) {
                 if (strpos('' . $scalar, '.') === false) {
                     $scalar = $scalar . '.0';
                 }
                 $this->getStream()->write($scalar);
             } else {
                 if (is_string($scalar) === true) {
                     $this->getStream()->write(PhpUtils::doubleQuotedPhpString($scalar));
                 } else {
                     if (is_bool($scalar) === true) {
                         $this->getStream()->write($scalar === true ? 'true' : 'false');
                     } else {
                         if (is_null($scalar) === true) {
                             $this->getStream()->write('null');
                         }
                     }
                 }
             }
         }
     } catch (StreamException $e) {
         $msg = "An error occured while writing the scalar value '{$scalar}'.";
         throw new StreamAccessException($msg, $this, 0, $e);
     }
 }
 protected function includeChoiceComponent(QtiComponent $component, DOMDocumentFragment $rendering)
 {
     $choiceIndex = $this->choiceCounter;
     $choiceIdentifier = PhpUtils::doubleQuotedPhpString($component->getIdentifier());
     $interactionIndex = $this->interactionCounter;
     $stateName = $this->getStateName();
     $includeStmtCmt = $rendering->ownerDocument->createComment(' qtism-include($' . "{$stateName}, {$interactionIndex}, {$choiceIdentifier}, {$choiceIndex}): ");
     $endIncludeStmtCmt = $rendering->ownerDocument->createComment(' qtism-endinclude ');
     $rendering->insertBefore($includeStmtCmt, $rendering->firstChild);
     $rendering->appendChild($endIncludeStmtCmt);
 }
 /**
  * @see \qtism\runtime\rendering\markup\xhtml\AbstractXhtmlRenderer::appendChildren()
  */
 protected function appendChildren(DOMDocumentFragment $fragment, QtiComponent $component, $base = '')
 {
     $renderingEngine = $this->getRenderingEngine();
     if ($renderingEngine !== null) {
         switch ($renderingEngine->getPrintedVariablePolicy()) {
             case AbstractMarkupRenderingEngine::CONTEXT_AWARE:
                 $value = Utils::printVariable($this->getRenderingEngine()->getState(), $component->getIdentifier(), $component->getFormat(), $component->mustPowerForm(), $component->getBase(), $component->getIndex(), $component->getDelimiter(), $component->getField(), $component->getMappingIndicator());
                 $fragment->firstChild->appendChild($fragment->ownerDocument->createTextNode($value));
                 break;
             case AbstractMarkupRenderingEngine::TEMPLATE_ORIENTED:
                 $base = $component->getBase();
                 $index = $component->getIndex();
                 $params = array('$' . $renderingEngine->getStateName(), PhpUtils::doubleQuotedPhpString($component->getIdentifier()), PhpUtils::doubleQuotedPhpString($component->getFormat()), $component->mustPowerForm() === true ? 'true' : 'false', is_int($base) === true ? $base : PhpUtils::doubleQuotedPhpString($base), is_int($index) === true ? $index : PhpUtils::doubleQuotedPhpString($index), PhpUtils::doubleQuotedPhpString($component->getDelimiter()), PhpUtils::doubleQuotedPhpString($component->getField()), PhpUtils::doubleQuotedPhpString($component->getMappingIndicator()));
                 $value = " qtism-printVariable(" . implode(', ', $params) . ") ";
                 $fragment->firstChild->appendChild($fragment->ownerDocument->createComment($value));
                 break;
         }
     }
 }
示例#4
0
 /**
  * @dataProvider doubleQuotedPhpStringDataProvider
  */
 public function testDoubleQuotedPhpString($input, $expected)
 {
     $this->assertEquals($expected, PhpUtils::doubleQuotedPhpString($input));
 }