/** * Render constraints * * @param \Zend\Db\Metadata\ConstraintCollection $constraints * @return string */ public function renderConstraints(\Zend\Db\Metadata\ConstraintCollection $constraints) { $rows = array(); foreach ($constraints as $constraint) { $row = array(); $row[] = $constraint->getName(); $row[] = $constraint->getType(); $rows[] = $row; } $table = new \Zend\Text\Table\Table(array('columnWidths' => array(25, 25), 'decorator' => 'ascii')); foreach ($rows as $row) { $table->appendRow($row); } return 'Constraints: ' . PHP_EOL . $table->render(); }
/** * Render a text table containing the data provided, that will fit inside console window's width. * * @param $data * @param $cols * @param $consoleWidth * @return string */ protected function renderTable($data, $cols, $consoleWidth) { $result = ''; $padding = 2; // If there is only 1 column, just concatenate it if ($cols == 1) { foreach ($data as $row) { $result .= $row[0] . "\n"; } return $result; } // Determine max width for each column $maxW = array(); for ($x = 1; $x <= $cols; $x++) { $maxW[$x] = 0; foreach ($data as $row) { $maxW[$x] = max($maxW[$x], mb_strlen($row[$x - 1], 'utf-8') + $padding * 2); } } /** * Check if the sum of x-1 columns fit inside console window width - 10 chars. If columns do not fit inside * console window, then we'll just concatenate them and output as is. */ $width = 0; for ($x = 1; $x < $cols; $x++) { $width += $maxW[$x]; } if ($width >= $consoleWidth - 10) { foreach ($data as $row) { $result .= join(" ", $row) . "\n"; } return $result; } /** * Use Zend\Text\Table to render the table. * The last column will use the remaining space in console window (minus 1 character to prevent double * wrapping at the edge of the screen). */ $maxW[$cols] = $consoleWidth - $width - 1; $table = new \Zend\Text\Table\Table(); $table->setColumnWidths($maxW); $table->setDecorator(new \Zend\Text\Table\Decorator\Blank()); $table->setPadding(2); foreach ($data as $row) { $table->appendRow($row); } return $table->render(); }