3 * @see https://github.com/zendframework/zend-text for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-text/blob/master/LICENSE.md New BSD License
8 namespace Zend\Text\Table
;
10 use Zend\Text\Table\Decorator\DecoratorInterface
as Decorator
;
13 * Row class for Zend\Text\Table
22 protected $columns = [];
25 * Temporary stored column widths
29 protected $columnWidths = null;
32 * Create a new column and append it to the row
34 * @param string $content
35 * @param array $options
38 public function createColumn($content, array $options = null)
44 if ($options !== null) {
45 extract($options, EXTR_IF_EXISTS
);
48 $column = new Column($content, $align, $colSpan, $encoding);
50 $this->appendColumn($column);
56 * Append a column to the row
58 * @param \Zend\Text\Table\Column $column The column to append to the row
61 public function appendColumn(Column
$column)
63 $this->columns
[] = $column;
69 * Get a column by it's index
71 * Returns null, when the index is out of range
76 public function getColumn($index)
78 if (! isset($this->columns
[$index])) {
82 return $this->columns
[$index];
86 * Get all columns of the row
90 public function getColumns()
92 return $this->columns
;
96 * Get the widths of all columns, which were rendered last
98 * @throws Exception\UnexpectedValueException When no columns were rendered yet
101 public function getColumnWidths()
103 if ($this->columnWidths
=== null) {
104 throw new Exception\
UnexpectedValueException(
105 '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
= [];
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 = [];
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, $colNum, $colSpan)));
148 // Render the column and split it's lines into an array
149 $result = explode("\n", $column->render($columnWidth, $padding));
151 // Store the width of the rendered column
152 $this->columnWidths
[] = $columnWidth;
154 // Store the rendered column and calculate the new max height
155 $renderedColumns[] = $result;
156 $maxHeight = max($maxHeight, count($result));
158 // Set up the internal column number
162 // If the row doesnt contain enough columns to fill the entire row, fill
163 // it with an empty column
164 if ($colNum < count($columnWidths)) {
165 $remainingWidth = (count($columnWidths) - $colNum - 1) +
166 array_sum(array_slice($columnWidths, $colNum));
167 $renderedColumns[] = [str_repeat(' ', $remainingWidth)];
169 $this->columnWidths
[] = $remainingWidth;
172 // Add each single column line to the result
174 for ($line = 0; $line < $maxHeight; $line++
) {
175 $result .= $decorator->getVertical();
177 foreach ($renderedColumns as $index => $renderedColumn) {
178 if (isset($renderedColumn[$line]) === true) {
179 $result .= $renderedColumn[$line];
181 $result .= str_repeat(' ', $this->columnWidths
[$index]);
184 $result .= $decorator->getVertical();