/** * Unmarshall a DOMElement object corresponding to a printedVariable element. * * @param DOMElement $element A DOMElement object. * @return QtiComponent A PrintedVariable object. * @throws UnmarshallingException */ protected function unmarshall(DOMElement $element) { if (($identifier = self::getDOMElementAttributeAs($element, 'identifier')) !== null) { $component = new PrintedVariable($identifier); if (($format = self::getDOMElementAttributeAs($element, 'format')) !== null) { $component->setFormat($format); } if (($powerForm = self::getDOMElementAttributeAs($element, 'powerForm', 'boolean')) !== null) { $component->setPowerForm($powerForm); } if (($base = self::getDOMElementAttributeAs($element, 'base')) !== null) { $component->setBase(Format::isInteger($base) === true ? intval($base) : $base); } if (($index = self::getDOMElementAttributeAs($element, 'index')) !== null) { $component->setIndex(Format::isInteger($index) === true ? intval($index) : $base); } if (($delimiter = self::getDOMElementAttributeAs($element, 'delimiter')) !== null) { $component->setDelimiter($delimiter); } if (($field = self::getDOMElementAttributeAs($element, 'field')) !== null) { $component->setField($field); } if ($mappingIndicator = self::getDOMElementAttributeAs($element, 'mappingIndicator')) { $component->setMappingIndicator($mappingIndicator); } if (($xmlBase = self::getXmlBase($element)) !== false) { $component->setXmlBase($xmlBase); } self::fillBodyElement($component, $element); return $component; } else { $msg = "The mandatory 'identifier' attribute is missing from the 'printedVariable' element."; throw new UnmarshallingException($msg, $element); } }
/** * @param mixed $value * @param string $expected * @param string $format * @param boolean $powerForm * @param integer|string $base * @param $integer|string $index * @param string $delimiter * @param string $field * @param stribng $mappingIndicator * @dataProvider printedVariableProvider */ public function testPrintedVariable($expected, $identifier, State $state, $format = '', $powerForm = false, $base = 10, $index = -1, $delimiter = ';', $field = '', $mappingIndicator = '=') { $printedVariable = new PrintedVariable($identifier); $printedVariable->setFormat($format); $printedVariable->setPowerForm($powerForm); $printedVariable->setBase($base); $printedVariable->setIndex($index); $printedVariable->setDelimiter($delimiter); $printedVariable->setField($field); $printedVariable->setMappingIndicator($mappingIndicator); $engine = new PrintedVariableEngine($printedVariable); $engine->setContext($state); $this->assertEquals($expected, $engine->process()); }
/** * Helper method to be used in QTI template oriented rendering to produce the final * value of a qti:printedVariable component. * * If an error occurs while printing the variable, the return value will contain the error message. * * @param State $context A State object from where values will be retrieved prior to formatting, depending on the $identifier argument. * @param string $identifier The identifier of the variable to be printed. * @param string $format The ISO 9899 format string (see printf). * @param boolean $powerForm Render float values in 'e' or 'E' format. * @param integer|string $base The number base to use when formatting integer variables (can be a variable reference). * @param integer|string $index The index to use when displaying ordered cardinality variables (can be a variable reference). * @param integer $delimiter The delimiter to use between values when displaying ordered, multiple, or record cardinality variables. * @param string $field The field specifier to use when displaying variables of record cardinality. * @param string $mappingIndicator The mapping indicator to use between field name and field value when displaying record cardinality variables. * @return string The formatted variable or an error message. */ public static function printVariable(State $context, $identifier, $format = '', $powerForm = false, $base = 10, $index = -1, $delimiter = ';', $field = '', $mappingIndicator = '=') { try { $printedVariable = new PrintedVariable($identifier); $printedVariable->setFormat($format); $printedVariable->setPowerForm($powerForm); $printedVariable->setBase($base); $printedVariable->setIndex($index); $printedVariable->setDelimiter($delimiter); $printedVariable->setField($field); $printedVariable->setMappingIndicator($mappingIndicator); $engine = new PrintedVariableEngine($printedVariable); $engine->setContext($context); return $engine->process(); } catch (Exception $e) { $previous = $e->getMessage(); return "qtism\\runtime\\rendering\\markup\\Utils::printVariable() method threw an unexpected exception:\n{$previous}."; } }