Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Text / Table / Row.php
blobee3fa5f7c5f56a4ff643b95ab125b4e2cf380baf
1 <?php
2 /**
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
8 */
10 namespace Zend\Text\Table;
12 use Zend\Text\Table\Decorator\DecoratorInterface as Decorator;
14 /**
15 * Row class for Zend\Text\Table
17 class Row
19 /**
20 * List of all columns
22 * @var array
24 protected $columns = array();
26 /**
27 * Temporary stored column widths
29 * @var array
31 protected $columnWidths = null;
33 /**
34 * Create a new column and append it to the row
36 * @param string $content
37 * @param array $options
38 * @return Row
40 public function createColumn($content, array $options = null)
42 $align = null;
43 $colSpan = null;
44 $encoding = null;
46 if ($options !== null) {
47 extract($options, EXTR_IF_EXISTS);
50 $column = new Column($content, $align, $colSpan, $encoding);
52 $this->appendColumn($column);
54 return $this;
57 /**
58 * Append a column to the row
60 * @param \Zend\Text\Table\Column $column The column to append to the row
61 * @return Row
63 public function appendColumn(Column $column)
65 $this->columns[] = $column;
67 return $this;
70 /**
71 * Get a column by it's index
73 * Returns null, when the index is out of range
75 * @param int $index
76 * @return Column|null
78 public function getColumn($index)
80 if (!isset($this->columns[$index])) {
81 return null;
84 return $this->columns[$index];
87 /**
88 * Get all columns of the row
90 * @return array
92 public function getColumns()
94 return $this->columns;
97 /**
98 * Get the widths of all columns, which were rendered last
100 * @throws Exception\UnexpectedValueException When no columns were rendered yet
101 * @return int
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;
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 = array();
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 = array();
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,
147 $colNum,
148 $colSpan)));
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
161 $colNum += $colSpan;
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,
169 $colNum));
170 $renderedColumns[] = array(str_repeat(' ', $remainingWidth));
172 $this->columnWidths[] = $remainingWidth;
175 // Add each single column line to the result
176 $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];
183 } else {
184 $result .= str_repeat(' ', $this->columnWidths[$index]);
187 $result .= $decorator->getVertical();
190 $result .= "\n";
193 return $result;