protected function renderTableElement(Element $tableEl, $config) { $rows = []; $colNames = []; foreach ($tableEl->query('.//col') as $colEl) { $colNames[] = $colEl->getAttribute('label'); } foreach ($tableEl->query('.//row') as $rowEl) { $row = []; $formatterParams = []; foreach ($rowEl->query('./formatter-param') as $paramEl) { $formatterParams[$paramEl->getAttribute('name')] = $paramEl->nodeValue; } foreach ($rowEl->query('.//cell') as $cellEl) { $colName = $cellEl->getAttribute('name'); $value = $cellEl->nodeValue; if ('' !== $value && $cellEl->hasAttribute('class')) { $classes = explode(' ', $cellEl->getAttribute('class')); $value = $this->formatter->applyClasses($classes, $value, $formatterParams); } $row[$colName] = $value; } $rows[] = $row; } $table = $this->createTable(); // style only supported in Symfony > 2.4 if (method_exists($table, 'setStyle')) { $table->setStyle($config['table_style']); } $table->setHeaders($colNames); $table->setRows($rows); $this->renderTable($table); $this->output->writeln(''); }
private function formatCell(Element $cellEl) { $class = $cellEl->getAttribute('class'); if (!isset($this->classDefinitions[$class])) { throw new \InvalidArgumentException(sprintf('No class defined with name "%s", known classes: "%s"', $class, implode('", "', array_keys($this->classDefinitions)))); } $formatterDefinitions = $this->classDefinitions[$class]; $value = $cellEl->nodeValue; foreach ($formatterDefinitions as $formatterDefinition) { if (count($formatterDefinition) == 2) { list($formatterName, $options) = $formatterDefinition; } else { list($formatterName) = $formatterDefinition; } $formatter = $this->registry->get($formatterName); $defaultOptions = $formatter->getDefaultOptions(); $diff = array_diff_key($options, $defaultOptions); if (count($diff)) { throw new \InvalidArgumentException(sprintf('Unknown options ["%s"] for formatter "%s" (class "%s"). Known options "%s"', implode('", "', array_keys($diff)), $formatterName, $class, implode('", "', array_keys($defaultOptions)))); } $options = array_merge($defaultOptions, $options); $params = array(); foreach ($cellEl->query('ancestor::row/param') as $paramEl) { $params[$paramEl->getAttribute('name')] = $paramEl->nodeValue; } $this->replaceOptionValues($options, $params); try { $value = $formatter->format($value, $options); } catch (\Exception $e) { throw new \RuntimeException(sprintf('Error encountered formatting cell "%s" with value "%s"', $cellEl->getAttribute('name'), print_r($cellEl->nodeValue, true)), null, $e); } } $cellEl->nodeValue = $value; }
protected function renderTableElement(Element $tableEl, $config) { $rows = array(); $row = null; foreach ($tableEl->query('.//row') as $rowEl) { $row = array(); foreach ($rowEl->query('.//cell') as $cellEl) { $colName = $cellEl->getAttribute('name'); $row[$colName] = $cellEl->nodeValue; } $rows[] = $row; } $table = $this->createTable(); // style only supported in Symfony > 2.4 if (method_exists($table, 'setStyle')) { $table->setStyle($config['table_style']); } $table->setHeaders(array_keys($row ?: array())); $table->setRows($rows); $this->renderTable($table); $this->output->writeln(''); }
protected function renderTableElement(Element $tableEl, $config) { $rows = array(); if (true === $config['header']) { $header = array(); foreach ($tableEl->query('.//row') as $rowEl) { foreach ($rowEl->query('.//cell') as $cellEl) { $colName = $cellEl->getAttribute('name'); $header[$colName] = $colName; } } $rows[] = $header; } foreach ($tableEl->query('.//row') as $rowEl) { $row = array(); foreach ($rowEl->query('.//cell') as $cellEl) { $colName = $cellEl->getAttribute('name'); $row[$colName] = $cellEl->nodeValue; } $rows[] = $row; } if ($config['file']) { $pointer = fopen($config['file'], 'w'); } else { $pointer = fopen('php://temp', 'w+'); } foreach ($rows as $row) { // use fputcsv to handle escaping fputcsv($pointer, $row, $config['delimiter']); } rewind($pointer); $this->output->write(stream_get_contents($pointer)); fclose($pointer); if ($config['file']) { $this->output->writeln('Dumped delimited file:'); $this->output->writeln($config['file']); } }
private function iterateRowDefinitions(Element $tableEl, XPath $sourceXpath, Definition $definition, array $parameters) { foreach ($definition['rows'] as $rowDefinition) { $selector = '/'; if (isset($rowDefinition['with_query'])) { $selector = $rowDefinition['with_query']; $selector = $this->xpathResolver->replaceFunctions($selector); } foreach ($sourceXpath->query($selector) as $sourceEl) { if (isset($rowDefinition['group'])) { $group = $this->tokenReplacer->replaceTokens($rowDefinition['group'], null, null, $parameters); } else { $group = self::DEFAULT_GROUP; } $groupEls = $tableEl->ownerDocument->xpath()->query('//group[@name="' . $group . '"]'); if ($groupEls->length > 0) { $groupEl = $groupEls->item(0); } else { $groupEl = $tableEl->appendElement('group'); $groupEl->setAttribute('name', $group); } $rowEl = $groupEl->appendElement('row'); $rowParameters = $parameters; if (isset($rowDefinition['param_exprs'])) { foreach ($rowDefinition['param_exprs'] as $paramName => $paramExpr) { // TODO: We should prevent DOMNodeList instances being returned from evaluate.. $paramValue = $sourceXpath->evaluate($paramExpr, $sourceEl); $rowParam = $rowEl->appendElement('param'); $rowParam->setAttribute('name', $paramName); $rowParam->nodeValue = $paramValue; $rowParameters[$paramName] = $paramValue; } } foreach ($rowDefinition['cells'] as $cellDefinition) { $columnName = $cellDefinition['name']; $cellEl = $rowEl->appendElement('cell'); $cellEl->setAttribute('name', $columnName); $pass = null; if (isset($cellDefinition['pass'])) { $pass = $cellDefinition['pass']; $cellEl->setAttribute('pass', $pass); } $value = null; if (isset($cellDefinition['class'])) { $class = $this->tokenReplacer->replaceTokens($cellDefinition['class'], null, null, $rowParameters); if ($class) { $cellEl->setAttribute('class', $class); } } if (isset($cellDefinition['expr'])) { $expr = $cellDefinition['expr']; $expr = $this->xpathResolver->replaceFunctions($expr); if (null === $pass) { $value = $sourceXpath->evaluate($expr, $sourceEl); } else { $value = $expr; } } if (array_key_exists('literal', $cellDefinition)) { $value = $cellDefinition['literal']; } $cellEl->nodeValue = $value; } } } }