composer package updates
[openemr.git] / vendor / zendframework / zend-text / src / Table / Row.php
blob0195ffe0ae8b880ce6543e6a283ec2d034220530
1 <?php
2 /**
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
6 */
8 namespace Zend\Text\Table;
10 use Zend\Text\Table\Decorator\DecoratorInterface as Decorator;
12 /**
13 * Row class for Zend\Text\Table
15 class Row
17 /**
18 * List of all columns
20 * @var array
22 protected $columns = [];
24 /**
25 * Temporary stored column widths
27 * @var array
29 protected $columnWidths = null;
31 /**
32 * Create a new column and append it to the row
34 * @param string $content
35 * @param array $options
36 * @return Row
38 public function createColumn($content, array $options = null)
40 $align = null;
41 $colSpan = null;
42 $encoding = null;
44 if ($options !== null) {
45 extract($options, EXTR_IF_EXISTS);
48 $column = new Column($content, $align, $colSpan, $encoding);
50 $this->appendColumn($column);
52 return $this;
55 /**
56 * Append a column to the row
58 * @param \Zend\Text\Table\Column $column The column to append to the row
59 * @return Row
61 public function appendColumn(Column $column)
63 $this->columns[] = $column;
65 return $this;
68 /**
69 * Get a column by it's index
71 * Returns null, when the index is out of range
73 * @param int $index
74 * @return Column|null
76 public function getColumn($index)
78 if (! isset($this->columns[$index])) {
79 return;
82 return $this->columns[$index];
85 /**
86 * Get all columns of the row
88 * @return array
90 public function getColumns()
92 return $this->columns;
95 /**
96 * Get the widths of all columns, which were rendered last
98 * @throws Exception\UnexpectedValueException When no columns were rendered yet
99 * @return int
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;
113 * Render the row
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
119 * @return string
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
127 // entire row
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 = [];
134 $maxHeight = 0;
135 $colNum = 0;
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
159 $colNum += $colSpan;
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
173 $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];
180 } else {
181 $result .= str_repeat(' ', $this->columnWidths[$index]);
184 $result .= $decorator->getVertical();
187 $result .= "\n";
190 return $result;