|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Text_Table |
|
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
18 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
19 * @version $Id: Row.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * Row class for Zend_Text_Table |
|
24 * |
|
25 * @category Zend |
|
26 * @package Zend_Text_Table |
|
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
28 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
29 */ |
|
30 class Zend_Text_Table_Row |
|
31 { |
|
32 /** |
|
33 * List of all columns |
|
34 * |
|
35 * @var array |
|
36 */ |
|
37 protected $_columns = array(); |
|
38 |
|
39 /** |
|
40 * Temporary stored column widths |
|
41 * |
|
42 * @var array |
|
43 */ |
|
44 protected $_columnWidths = null; |
|
45 |
|
46 /** |
|
47 * Create a new column and append it to the row |
|
48 * |
|
49 * @param string $content |
|
50 * @param array $options |
|
51 * @return Zend_Text_Table_Row |
|
52 */ |
|
53 public function createColumn($content, array $options = null) |
|
54 { |
|
55 $align = null; |
|
56 $colSpan = null; |
|
57 $encoding = null; |
|
58 |
|
59 if ($options !== null) { |
|
60 extract($options, EXTR_IF_EXISTS); |
|
61 } |
|
62 |
|
63 require_once 'Zend/Text/Table/Column.php'; |
|
64 |
|
65 $column = new Zend_Text_Table_Column($content, $align, $colSpan, $encoding); |
|
66 |
|
67 $this->appendColumn($column); |
|
68 |
|
69 return $this; |
|
70 } |
|
71 |
|
72 /** |
|
73 * Append a column to the row |
|
74 * |
|
75 * @param Zend_Text_Table_Column $column The column to append to the row |
|
76 * @return Zend_Text_Table_Row |
|
77 */ |
|
78 public function appendColumn(Zend_Text_Table_Column $column) |
|
79 { |
|
80 $this->_columns[] = $column; |
|
81 |
|
82 return $this; |
|
83 } |
|
84 |
|
85 /** |
|
86 * Get a column by it's index |
|
87 * |
|
88 * Returns null, when the index is out of range |
|
89 * |
|
90 * @param integer $index |
|
91 * @return Zend_Text_Table_Column|null |
|
92 */ |
|
93 public function getColumn($index) |
|
94 { |
|
95 if (!isset($this->_columns[$index])) { |
|
96 return null; |
|
97 } |
|
98 |
|
99 return $this->_columns[$index]; |
|
100 } |
|
101 |
|
102 /** |
|
103 * Get all columns of the row |
|
104 * |
|
105 * @return array |
|
106 */ |
|
107 public function getColumns() |
|
108 { |
|
109 return $this->_columns; |
|
110 } |
|
111 |
|
112 /** |
|
113 * Get the widths of all columns, which were rendered last |
|
114 * |
|
115 * @throws Zend_Text_Table_Exception When no columns were rendered yet |
|
116 * @return integer |
|
117 */ |
|
118 public function getColumnWidths() |
|
119 { |
|
120 if ($this->_columnWidths === null) { |
|
121 require_once 'Zend/Text/Table/Exception.php'; |
|
122 throw new Zend_Text_Table_Exception('No columns were rendered yet'); |
|
123 } |
|
124 |
|
125 return $this->_columnWidths; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Render the row |
|
130 * |
|
131 * @param array $columnWidths Width of all columns |
|
132 * @param Zend_Text_Table_Decorator_Interface $decorator Decorator for the row borders |
|
133 * @param integer $padding Padding for the columns |
|
134 * @throws Zend_Text_Table_Exception When there are too many columns |
|
135 * @return string |
|
136 */ |
|
137 public function render(array $columnWidths, |
|
138 Zend_Text_Table_Decorator_Interface $decorator, |
|
139 $padding = 0) |
|
140 { |
|
141 // Prepare an array to store all column widths |
|
142 $this->_columnWidths = array(); |
|
143 |
|
144 // If there is no single column, create a column which spans over the |
|
145 // entire row |
|
146 if (count($this->_columns) === 0) { |
|
147 require_once 'Zend/Text/Table/Column.php'; |
|
148 $this->appendColumn(new Zend_Text_Table_Column(null, null, count($columnWidths))); |
|
149 } |
|
150 |
|
151 // First we have to render all columns, to get the maximum height |
|
152 $renderedColumns = array(); |
|
153 $maxHeight = 0; |
|
154 $colNum = 0; |
|
155 foreach ($this->_columns as $column) { |
|
156 // Get the colspan of the column |
|
157 $colSpan = $column->getColSpan(); |
|
158 |
|
159 // Verify if there are enough column widths defined |
|
160 if (($colNum + $colSpan) > count($columnWidths)) { |
|
161 require_once 'Zend/Text/Table/Exception.php'; |
|
162 throw new Zend_Text_Table_Exception('Too many columns'); |
|
163 } |
|
164 |
|
165 // Calculate the column width |
|
166 $columnWidth = ($colSpan - 1 + array_sum(array_slice($columnWidths, |
|
167 $colNum, |
|
168 $colSpan))); |
|
169 |
|
170 // Render the column and split it's lines into an array |
|
171 $result = explode("\n", $column->render($columnWidth, $padding)); |
|
172 |
|
173 // Store the width of the rendered column |
|
174 $this->_columnWidths[] = $columnWidth; |
|
175 |
|
176 // Store the rendered column and calculate the new max height |
|
177 $renderedColumns[] = $result; |
|
178 $maxHeight = max($maxHeight, count($result)); |
|
179 |
|
180 // Set up the internal column number |
|
181 $colNum += $colSpan; |
|
182 } |
|
183 |
|
184 // If the row doesnt contain enough columns to fill the entire row, fill |
|
185 // it with an empty column |
|
186 if ($colNum < count($columnWidths)) { |
|
187 $remainingWidth = (count($columnWidths) - $colNum - 1) + |
|
188 array_sum(array_slice($columnWidths, |
|
189 $colNum)); |
|
190 $renderedColumns[] = array(str_repeat(' ', $remainingWidth)); |
|
191 |
|
192 $this->_columnWidths[] = $remainingWidth; |
|
193 } |
|
194 |
|
195 // Add each single column line to the result |
|
196 $result = ''; |
|
197 for ($line = 0; $line < $maxHeight; $line++) { |
|
198 $result .= $decorator->getVertical(); |
|
199 |
|
200 foreach ($renderedColumns as $renderedColumn) { |
|
201 if (isset($renderedColumn[$line]) === true) { |
|
202 $result .= $renderedColumn[$line]; |
|
203 } else { |
|
204 $result .= str_repeat(' ', strlen($renderedColumn[0])); |
|
205 } |
|
206 |
|
207 $result .= $decorator->getVertical(); |
|
208 } |
|
209 |
|
210 $result .= "\n"; |
|
211 } |
|
212 |
|
213 return $result; |
|
214 } |
|
215 } |