3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Text\Table
;
12 use Zend\Text\Table\Decorator\DecoratorInterface
as Decorator
;
15 * Row class for Zend\Text\Table
24 protected $columns = array();
27 * Temporary stored column widths
31 protected $columnWidths = null;
34 * Create a new column and append it to the row
36 * @param string $content
37 * @param array $options
40 public function createColumn($content, array $options = null)
46 if ($options !== null) {
47 extract($options, EXTR_IF_EXISTS
);
50 $column = new Column($content, $align, $colSpan, $encoding);
52 $this->appendColumn($column);
58 * Append a column to the row
60 * @param \Zend\Text\Table\Column $column The column to append to the row
63 public function appendColumn(Column
$column)
65 $this->columns
[] = $column;
71 * Get a column by it's index
73 * Returns null, when the index is out of range
78 public function getColumn($index)
80 if (!isset($this->columns
[$index])) {
84 return $this->columns
[$index];
88 * Get all columns of the row
92 public function getColumns()
94 return $this->columns
;
98 * Get the widths of all columns, which were rendered last
100 * @throws Exception\UnexpectedValueException When no columns were rendered yet
103 public function getColumnWidths()
105 if ($this->columnWidths
=== null) {
106 throw new Exception\
UnexpectedValueException('render() must be called before columnWidths can be populated');
109 return $this->columnWidths
;
115 * @param array $columnWidths Width of all columns
116 * @param Decorator $decorator Decorator for the row borders
117 * @param int $padding Padding for the columns
118 * @throws Exception\OverflowException When there are too many columns
121 public function render(array $columnWidths, Decorator
$decorator, $padding = 0)
123 // Prepare an array to store all column widths
124 $this->columnWidths
= array();
126 // If there is no single column, create a column which spans over the
128 if (count($this->columns
) === 0) {
129 $this->appendColumn(new Column(null, null, count($columnWidths)));
132 // First we have to render all columns, to get the maximum height
133 $renderedColumns = array();
136 foreach ($this->columns
as $column) {
137 // Get the colspan of the column
138 $colSpan = $column->getColSpan();
140 // Verify if there are enough column widths defined
141 if (($colNum +
$colSpan) > count($columnWidths)) {
142 throw new Exception\
OverflowException('Too many columns');
145 // Calculate the column width
146 $columnWidth = ($colSpan - 1 +
array_sum(array_slice($columnWidths,
150 // Render the column and split it's lines into an array
151 $result = explode("\n", $column->render($columnWidth, $padding));
153 // Store the width of the rendered column
154 $this->columnWidths
[] = $columnWidth;
156 // Store the rendered column and calculate the new max height
157 $renderedColumns[] = $result;
158 $maxHeight = max($maxHeight, count($result));
160 // Set up the internal column number
164 // If the row doesnt contain enough columns to fill the entire row, fill
165 // it with an empty column
166 if ($colNum < count($columnWidths)) {
167 $remainingWidth = (count($columnWidths) - $colNum - 1) +
168 array_sum(array_slice($columnWidths,
170 $renderedColumns[] = array(str_repeat(' ', $remainingWidth));
172 $this->columnWidths
[] = $remainingWidth;
175 // Add each single column line to the result
177 for ($line = 0; $line < $maxHeight; $line++
) {
178 $result .= $decorator->getVertical();
180 foreach ($renderedColumns as $index => $renderedColumn) {
181 if (isset($renderedColumn[$line]) === true) {
182 $result .= $renderedColumn[$line];
184 $result .= str_repeat(' ', $this->columnWidths
[$index]);
187 $result .= $decorator->getVertical();