/** * Unmarshall a DOMElement object corresponding to an XHTML table element. * * @param \DOMElement $element A DOMElement object. * @return \qtism\data\QtiComponent A Table object. * @throws \qtism\data\storage\xml\marshalling\UnmarshallingException */ protected function unmarshall(DOMElement $element) { $tbodyElts = self::getChildElementsByTagName($element, 'tbody'); if (count($tbodyElts) > 0) { $tbodies = new TbodyCollection(); foreach ($tbodyElts as $tbodyElt) { $marshaller = $this->getMarshallerFactory()->createMarshaller($tbodyElt); $tbodies[] = $marshaller->unmarshall($tbodyElt); } $component = new Table($tbodies); if (($summary = self::getDOMElementAttributeAs($element, 'summary')) !== null) { $component->setSummary($summary); } if (($xmlBase = self::getXmlBase($element)) !== false) { $component->setXmlBase($xmlBase); } $captionElts = self::getChildElementsByTagName($element, 'caption'); if (count($captionElts) > 0) { $marshaller = $this->getMarshallerFactory()->createMarshaller($captionElts[0]); $component->setCaption($marshaller->unmarshall($captionElts[0])); } $colElts = self::getChildElementsByTagName($element, 'col'); if (count($colElts) > 0) { $cols = new ColCollection(); foreach ($colElts as $c) { $marshaller = $this->getMarshallerFactory()->createMarshaller($c); $cols[] = $marshaller->unmarshall($c); } $component->setCols($cols); } $colgroupElts = self::getChildElementsByTagName($element, 'colgroup'); if (count($colgroupElts) > 0) { $colgroups = new ColgroupCollection(); foreach ($colgroupElts as $c) { $marshaller = $this->getMarshallerFactory()->createMarshaller($c); $colgroups[] = $marshaller->unmarshall($c); } $component->setColgroups($colgroups); } $theadElts = self::getChildElementsByTagName($element, 'thead'); if (count($theadElts) > 0) { $marshaller = $this->getMarshallerFactory()->createMarshaller($theadElts[0]); $component->setThead($marshaller->unmarshall($theadElts[0])); } $tfootElts = self::getChildElementsByTagName($element, 'tfoot'); if (count($tfootElts) > 0) { $marshaller = $this->getMarshallerFactory()->createMarshaller($tfootElts[0]); $component->setTfoot($marshaller->unmarshall($tfootElts[0])); } $this->fillBodyElement($component, $element); return $component; } else { $msg = "A 'table' element must contain at lease one 'tbody' element."; throw new UnmarshallingException($msg, $element); } }
public function testMarshall() { $th1 = new Th('firstname'); $th1->setContent(new FlowCollection(array(new TextRun('First Name')))); $th1->setAxis('identity'); $th1->setScope(TableCellScope::COL); $th2 = new Th('lastname'); $th2->setContent(new FlowCollection(array(new TextRun('Last Name')))); $th2->setAxis('identity'); $th2->setScope(TableCellScope::COL); $tr = new Tr(new TableCellCollection(array($th1, $th2))); $thead = new Thead(new TrCollection(array($tr))); $caption = new Caption(); $strong = new Strong(); $strong->setContent(new InlineCollection(array(new TextRun('people')))); $caption->setContent(new InlineCollection(array(new TextRun('Some '), $strong, new TextRun(' ...')))); $col1 = new Col(); $col1->setSpan(1); $col2 = new Col(); $col2->setSpan(1); $cols = new ColCollection(array($col1, $col2)); $td1 = new Td(); $td1->setContent(new FlowCollection(array(new TextRun('John')))); $td1->setRowspan(1); $td1->setColspan(1); $td1->setHeaders(new IdentifierCollection(array('firstname'))); $td2 = new Td(); $td2->setContent(new FlowCollection(array(new TextRun('Dunbar Smith Wayson')))); $td2->setHeaders(new IdentifierCollection(array('lastname'))); $td2->setAbbr('Dunbar S.W.'); $tr1 = new Tr(new TableCellCollection(array($td1, $td2))); $td1 = new Td(); $td1->setContent(new FlowCollection(array(new TextRun('Flash')))); $td2 = new Td(); $td2->setContent(new FlowCollection(array(new TextRun('Gordon')))); $tr2 = new Tr(new TableCellCollection(array($td1, $td2))); $tbody = new Tbody(new TrCollection(array($tr1, $tr2))); $tbodies = new TbodyCollection(array($tbody)); $table = new Table($tbodies, 'my-table', 'qti table'); $table->setSummary('Some people...'); $table->setThead($thead); $table->setCaption($caption); $table->setCols($cols); $marshaller = $this->getMarshallerFactory('2.1.0')->createMarshaller($table); $element = $marshaller->marshall($table); $dom = new DOMDocument('1.0', 'UTF-8'); $element = $dom->importNode($element, true); $expected = '<table summary="Some people..." id="my-table" class="qti table">'; $expected .= '<caption>Some <strong>people</strong> ...</caption>'; $expected .= '<col/>'; $expected .= '<col/>'; $expected .= '<thead>'; $expected .= '<tr>'; $expected .= '<th scope="col" axis="identity" id="firstname">First Name</th>'; $expected .= '<th scope="col" axis="identity" id="lastname">Last Name</th>'; $expected .= '</tr>'; $expected .= '</thead>'; $expected .= '<tbody>'; $expected .= '<tr>'; $expected .= '<td headers="firstname" rowspan="1" colspan="1">John</td>'; $expected .= '<td headers="lastname" abbr="Dunbar S.W.">Dunbar Smith Wayson</td>'; $expected .= '</tr>'; $expected .= '<tr>'; $expected .= '<td>Flash</td>'; $expected .= '<td>Gordon</td>'; $expected .= '</tr>'; $expected .= '</tbody>'; $expected .= '</table>'; $this->assertEquals($expected, $dom->saveXML($element)); }