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(); 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']); } }
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(''); }