/** * Append a row to the table * * @param array|Row $row The row to append to the table * @throws Exception\InvalidArgumentException When $row is neither an array nor Zend_Zext_Table_Row * @throws Exception\OverflowException When a row contains too many columns * @return Table */ public function appendRow($row) { if (!is_array($row) && !$row instanceof Row) { throw new Exception\InvalidArgumentException('$row must be an array or instance of Zend\\Text\\Table\\Row'); } if (is_array($row)) { if (count($row) > count($this->_columnWidths)) { throw new Exception\OverflowException('Row contains too many columns'); } $data = $row; $row = new Row(); $colNum = 0; foreach ($data as $columnData) { if (isset($this->_defaultColumnAligns[$colNum])) { $align = $this->_defaultColumnAligns[$colNum]; } else { $align = null; } $row->appendColumn(new Column($columnData, $align)); $colNum++; } } $this->_rows[] = $row; return $this; }
/** * list available view scripts */ public function listviewscriptsAction() { $config = $this->getServiceLocator()->get('Config'); $table = new Table(array('columnWidths' => array(40, 40, 40), 'decorator' => 'ascii')); $table->appendRow(array('Module', 'Name', 'Description')); $offset = strlen(getcwd()) + 1; $links = ""; $github = 'https://github.com/cross-solution/YAWIK/blob/master/'; foreach ($config['view_manager']['template_map'] as $key => $absolute_filename) { // strip the application_root plus an additional slash $filename = substr(realpath($absolute_filename), $offset); if (preg_match('~module/([^/]+)~', $filename, $match)) { $module = $match[1]; } else { $module = "not found ({$key})"; } $viewModel = new ViewModel(); $viewModel->setTemplate($key); $row = new Row(); $row->appendColumn(new Column($module)); if ($filename) { $row->appendColumn(new Column('`' . $key . '`_')); $links .= '.. _' . $key . ': ' . $github . $filename . PHP_EOL; } else { $row->appendColumn(new Column("WRONG CONFIGURATION")); } $comment = ""; if (file_exists($absolute_filename)) { $src = file_get_contents($absolute_filename); $comment = "file exists"; if (preg_match("/{{rtd:\\s*(.*)}}/", $src, $match)) { $comment = $match['1']; } } $row->appendColumn(new Column($comment)); $table->appendRow($row); } echo $table . PHP_EOL; echo $links; return PHP_EOL; }
public function testTableMagicToString() { $table = new Table\Table(array('columnWidths' => array(10))); $row = new Table\Row(); $row->appendColumn(new Table\Column('foobar')); $table->appendRow($row); $this->assertEquals((string) $table, "┌──────────┐\n│foobar │\n└──────────┘\n"); }
/** * * @return TextTable */ private function getTable() { $paginator = $this->getPaginator(); $translator = $this->getTranslator(); $options = ['columnWidths' => $this->getColumnWidths()]; $table = new TextTable($options); $table->setDecorator('ascii'); /* * Title */ $tableRow = new Table\Row(); $tableColumn = new Table\Column($this->getTitle()); $tableColumn->setColSpan(count($options['columnWidths'])); $tableColumn->setAlign(Table\Column::ALIGN_CENTER); $tableRow->appendColumn($tableColumn); $table->appendRow($tableRow); /** * Header */ $tableRow = new Table\Row(); foreach ($this->getColumnsToDisplay() as $column) { $label = $column->getLabel(); if ($this->getTranslator() !== null) { $label = $this->getTranslator()->translate($label); } if (function_exists('mb_strtoupper')) { $label = mb_strtoupper($label); } else { $label = strtoupper($label); } $tableColumn = new Table\Column($label); if ($column->getType() instanceof Type\Number) { $tableColumn->setAlign(Table\Column::ALIGN_RIGHT); } else { $tableColumn->setAlign(Table\Column::ALIGN_LEFT); } $tableRow->appendColumn($tableColumn); } $table->appendRow($tableRow); /* * Data */ foreach ($this->getData() as $row) { $tableRow = new Table\Row(); foreach ($this->getColumnsToDisplay() as $column) { $value = ''; if (isset($row[$column->getUniqueId()])) { $value = $row[$column->getUniqueId()]; } if (is_array($value)) { $value = implode(', ', $value); } $tableColumn = new Table\Column($value); if ($column->getType() instanceof Type\Number) { $tableColumn->setAlign(Table\Column::ALIGN_RIGHT); } else { $tableColumn->setAlign(Table\Column::ALIGN_LEFT); } $tableRow->appendColumn($tableColumn); } $table->appendRow($tableRow); } /* * Pagination */ $tableRow = new Table\Row(); $footer = $translator->translate('Page') . ' '; $footer .= $paginator->getCurrentPageNumber() . ' ' . $translator->translate('of') . ' ' . $paginator->count(); $footer .= ' / '; $footer .= $translator->translate('Showing') . ' ' . $paginator->getCurrentItemCount() . ' ' . $translator->translate('of') . ' ' . $paginator->getTotalItemCount() . ' ' . $translator->translate('items'); $tableColumn = new Table\Column($footer); $tableColumn->setColSpan(count($options['columnWidths'])); $tableColumn->setAlign(Table\Column::ALIGN_CENTER); $tableRow->appendColumn($tableColumn); $table->appendRow($tableRow); return $table; }
private function parseTable(\DOMElement $node, $context) { /*{{{*/ $trs = $this->xpath->query('txt:tr', $node); $num_cols = 0; $rows = array(); foreach ($trs as $tr) { $row = new Zend_Row(); $tds = $this->xpath->query('txt:td', $tr); $len = $tds->length; if ($len > $num_cols) { $num_cols = $len; } foreach ($tds as $td) { $td_text = ""; foreach ($td->childNodes as $child) { $td_text .= $this->parseNode($child, $context); } $column = new Zend_Column($td_text); if ($td->hasAttribute('colspan')) { $column->setColSpan((int) $td->getAttribute('colspan')); } $row->appendColumn($column); } $rows[] = $row; } if ($num_cols) { $col_width = (int) floor($this->wordwrap / $num_cols); $table = new Zend_Table(array('columnWidths' => array_fill(0, $num_cols, $col_width), 'decorator' => 'ascii', 'padding' => 1)); foreach ($rows as $row) { $table->appendRow($row); } return $table->render(); } }
/** * Convert the table tag * * @param DOMElement $node * @return string */ public static function table($node) { // check if thead exists if (0 !== $node[0]->getElementsByTagName('thead')->length) { $head = true; } else { $head = false; } $rows = $node[0]->getElementsByTagName('row'); $table = array(); $totRow = $rows->length; $j = 0; foreach ($rows as $row) { $cols = $row->getElementsByTagName('entry'); $totCol = $cols->length; if (!isset($widthCol)) { $widthCol = array_fill(0, $totCol, 0); } $i = 0; foreach ($cols as $col) { $table[$j][$i] = self::formatText($col->nodeValue); $length = strlen($table[$j][$i]); if ($length > $widthCol[$i]) { $widthCol[$i] = $length; } $i++; } $j++; } $tableText = new Table\Table(array('columnWidths' => $widthCol, 'decorator' => 'ascii')); for ($j = 0; $j < $totRow; $j++) { $row = new Table\Row(); for ($i = 0; $i < $totCol; $i++) { $row->appendColumn(new Table\Column($table[$j][$i])); } $tableText->appendRow($row); } $output = $tableText->render(); // if thead exists change the table style with head (= instead of -) if ($head) { $table = explode("\n", $output); $newOutput = ''; $i = 0; foreach ($table as $row) { if ('+-' === substr($row, 0, 2)) { $i++; } if (2 === $i) { $row = str_replace('-', '=', $row); } $newOutput .= "{$row}\n"; } return $newOutput; } return $output; }