public function formatTable(TableDom $document) { $document->formatOutput = true; $cellEls = $document->xpath()->query('//cell[@class]'); foreach ($cellEls as $cellEl) { $this->formatCell($cellEl); } }
public static function sortTable(TableDom $tableDom, array $sortDefinition) { $groups = array(); foreach (array_reverse($sortDefinition) as $columnSpecifier => $direction) { if (is_numeric($columnSpecifier)) { $columnSpecifier = $direction; $direction = 'asc'; } $parts = explode('#', $columnSpecifier); if (count($parts) === 1) { $group = TableBuilder::DEFAULT_GROUP; $columnName = $parts[0]; } else { $group = $parts[0]; $columnName = $parts[1]; } if (!isset($groups[$group])) { $rowEls = $tableDom->xpath()->query('//group[@name="' . $group . '"]/row'); $rowEls = iterator_to_array($rowEls); $groups[$group] = $rowEls; } else { $rowEls = $groups[$group]; } self::mergesort($rowEls, function (Element $rowEl1, Element $rowEl2) use($columnName, $direction) { $cellEl1 = $rowEl1->query('.//cell[@name="' . $columnName . '"]')->item(0); $cellEl2 = $rowEl2->query('.//cell[@name="' . $columnName . '"]')->item(0); if (null === $cellEl1 || null === $cellEl2) { throw new \InvalidArgumentException(sprintf('Unknown column "%s"', $columnName)); } $row1Value = $cellEl1->nodeValue; $row2Value = $cellEl2->nodeValue; if ($row1Value == $row2Value) { return 0; } $greaterThan = $row1Value > $row2Value; if (strtolower($direction) === 'asc') { return $greaterThan ? 1 : -1; } return $greaterThan ? -1 : 1; }); $groups[$group] = $rowEls; } foreach ($groups as $group => $rowEls) { $groupEl = $tableDom->xpath()->query('//group[@name="' . $group . '"]')->item(0); if (!$groupEl) { continue; } foreach ($groupEl->childNodes as $childNode) { $groupEl->removeChild($childNode); } foreach ($rowEls as $rowEl) { $groupEl->appendChild($rowEl); } } }
/** * Transform the source DOM into a series of row elements according * to the row definitions. * * @param \DOMDocument $sourceDom * @param array $rowDefinitions * @param array $parameters * * @return Document */ public function buildTable(\DOMDocument $sourceDom, Definition $definition, array $parameters = array()) { $tableDom = new TableDom(); $sourceXpath = new XPath($sourceDom); $this->xpathResolver->registerXPathFunctions($tableDom->xpath()); $this->xpathResolver->registerXPathFunctions($sourceXpath); $tableEl = $tableDom->createRoot('table'); $this->iterateRowDefinitions($tableEl, $sourceXpath, $definition, $parameters); $this->executePasses($definition, $tableEl); return $tableDom; }
/** * Render the table. * * @param mixed $tableDom * @param mixed $config */ private function render(TableDom $tableDom, $config) { $rows = array(); $row = null; foreach ($tableDom->xpath()->query('//row') as $rowEl) { $row = array(); foreach ($tableDom->xpath()->query('.//cell', $rowEl) as $cellEl) { $colName = $cellEl->getAttribute('name'); // exclude cells if (isset($config['exclude']) && in_array($colName, $config['exclude'])) { continue; } $row[$colName] = $cellEl->nodeValue; } $rows[] = $row; } $table = $this->createTable(); $table->setHeaders(array_keys($row ?: array())); $table->setRows($rows); $this->renderTable($table); $this->output->writeln(''); }
private function assertTable($expected, TableDom $result) { $results = array(); foreach ($result->xpath()->query('//row') as $rowEl) { $row = array(); foreach ($result->xpath()->query('./cell', $rowEl) as $cellEl) { $row[$cellEl->getAttribute('name')] = $cellEl->nodeValue; } $results[] = $row; } $this->assertEquals($expected, $results); }
private function createTable($xml) { $table = new TableDom(); $table->formatOutput = true; $table->loadXml($xml); return $table; }