|
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: Column.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
20 */ |
|
21 |
|
22 /** |
|
23 * @see Zend_Text_Table |
|
24 */ |
|
25 require_once 'Zend/Text/Table.php'; |
|
26 |
|
27 /** |
|
28 * @see Zend_Text_MultiByte |
|
29 */ |
|
30 require_once 'Zend/Text/MultiByte.php'; |
|
31 |
|
32 /** |
|
33 * Column class for Zend_Text_Table_Row |
|
34 * |
|
35 * @category Zend |
|
36 * @package Zend_Text_Table |
|
37 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
38 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
39 */ |
|
40 class Zend_Text_Table_Column |
|
41 { |
|
42 /** |
|
43 * Aligns for columns |
|
44 */ |
|
45 const ALIGN_LEFT = 'left'; |
|
46 const ALIGN_CENTER = 'center'; |
|
47 const ALIGN_RIGHT = 'right'; |
|
48 |
|
49 /** |
|
50 * Content of the column |
|
51 * |
|
52 * @var string |
|
53 */ |
|
54 protected $_content = ''; |
|
55 |
|
56 /** |
|
57 * Align of the column |
|
58 * |
|
59 * @var string |
|
60 */ |
|
61 protected $_align = self::ALIGN_LEFT; |
|
62 |
|
63 /** |
|
64 * Colspan of the column |
|
65 * |
|
66 * @var integer |
|
67 */ |
|
68 protected $_colSpan = 1; |
|
69 |
|
70 /** |
|
71 * Allowed align parameters |
|
72 * |
|
73 * @var array |
|
74 */ |
|
75 protected $_allowedAligns = array(self::ALIGN_LEFT, self::ALIGN_CENTER, self::ALIGN_RIGHT); |
|
76 |
|
77 /** |
|
78 * Create a column for a Zend_Text_Table_Row object. |
|
79 * |
|
80 * @param string $content The content of the column |
|
81 * @param string $align The align of the content |
|
82 * @param integer $colSpan The colspan of the column |
|
83 * @param string $charset The encoding of the content |
|
84 */ |
|
85 public function __construct($content = null, $align = null, $colSpan = null, $charset = null) |
|
86 { |
|
87 if ($content !== null) { |
|
88 $this->setContent($content, $charset); |
|
89 } |
|
90 |
|
91 if ($align !== null) { |
|
92 $this->setAlign($align); |
|
93 } |
|
94 |
|
95 if ($colSpan !== null) { |
|
96 $this->setColSpan($colSpan); |
|
97 } |
|
98 } |
|
99 |
|
100 /** |
|
101 * Set the content. |
|
102 * |
|
103 * If $charset is not defined, it is assumed that $content is encoded in |
|
104 * the charset defined via Zend_Text_Table::setInputCharset() (defaults |
|
105 * to utf-8). |
|
106 * |
|
107 * @param string $content Content of the column |
|
108 * @param string $charset The charset of the content |
|
109 * @throws Zend_Text_Table_Exception When $content is not a string |
|
110 * @return Zend_Text_Table_Column |
|
111 */ |
|
112 public function setContent($content, $charset = null) |
|
113 { |
|
114 if (is_string($content) === false) { |
|
115 require_once 'Zend/Text/Table/Exception.php'; |
|
116 throw new Zend_Text_Table_Exception('$content must be a string'); |
|
117 } |
|
118 |
|
119 if ($charset === null) { |
|
120 $inputCharset = Zend_Text_Table::getInputCharset(); |
|
121 } else { |
|
122 $inputCharset = strtolower($charset); |
|
123 } |
|
124 |
|
125 $outputCharset = Zend_Text_Table::getOutputCharset(); |
|
126 |
|
127 if ($inputCharset !== $outputCharset) { |
|
128 if (PHP_OS !== 'AIX') { |
|
129 // AIX does not understand these character sets |
|
130 $content = iconv($inputCharset, $outputCharset, $content); |
|
131 } |
|
132 |
|
133 } |
|
134 |
|
135 $this->_content = $content; |
|
136 |
|
137 return $this; |
|
138 } |
|
139 |
|
140 /** |
|
141 * Set the align |
|
142 * |
|
143 * @param string $align Align of the column |
|
144 * @throws Zend_Text_Table_Exception When supplied align is invalid |
|
145 * @return Zend_Text_Table_Column |
|
146 */ |
|
147 public function setAlign($align) |
|
148 { |
|
149 if (in_array($align, $this->_allowedAligns) === false) { |
|
150 require_once 'Zend/Text/Table/Exception.php'; |
|
151 throw new Zend_Text_Table_Exception('Invalid align supplied'); |
|
152 } |
|
153 |
|
154 $this->_align = $align; |
|
155 |
|
156 return $this; |
|
157 } |
|
158 |
|
159 /** |
|
160 * Set the colspan |
|
161 * |
|
162 * @param int $colSpan |
|
163 * @throws Zend_Text_Table_Exception When $colSpan is smaller than 1 |
|
164 * @return Zend_Text_Table_Column |
|
165 */ |
|
166 public function setColSpan($colSpan) |
|
167 { |
|
168 if (is_int($colSpan) === false or $colSpan < 1) { |
|
169 require_once 'Zend/Text/Table/Exception.php'; |
|
170 throw new Zend_Text_Table_Exception('$colSpan must be an integer and greater than 0'); |
|
171 } |
|
172 |
|
173 $this->_colSpan = $colSpan; |
|
174 |
|
175 return $this; |
|
176 } |
|
177 |
|
178 /** |
|
179 * Get the colspan |
|
180 * |
|
181 * @return integer |
|
182 */ |
|
183 public function getColSpan() |
|
184 { |
|
185 return $this->_colSpan; |
|
186 } |
|
187 |
|
188 /** |
|
189 * Render the column width the given column width |
|
190 * |
|
191 * @param integer $columnWidth The width of the column |
|
192 * @param integer $padding The padding for the column |
|
193 * @throws Zend_Text_Table_Exception When $columnWidth is lower than 1 |
|
194 * @throws Zend_Text_Table_Exception When padding is greater than columnWidth |
|
195 * @return string |
|
196 */ |
|
197 public function render($columnWidth, $padding = 0) |
|
198 { |
|
199 if (is_int($columnWidth) === false or $columnWidth < 1) { |
|
200 require_once 'Zend/Text/Table/Exception.php'; |
|
201 throw new Zend_Text_Table_Exception('$columnWidth must be an integer and greater than 0'); |
|
202 } |
|
203 |
|
204 $columnWidth -= ($padding * 2); |
|
205 |
|
206 if ($columnWidth < 1) { |
|
207 require_once 'Zend/Text/Table/Exception.php'; |
|
208 throw new Zend_Text_Table_Exception('Padding (' . $padding . ') is greater than column width'); |
|
209 } |
|
210 |
|
211 switch ($this->_align) { |
|
212 case self::ALIGN_LEFT: |
|
213 $padMode = STR_PAD_RIGHT; |
|
214 break; |
|
215 |
|
216 case self::ALIGN_CENTER: |
|
217 $padMode = STR_PAD_BOTH; |
|
218 break; |
|
219 |
|
220 case self::ALIGN_RIGHT: |
|
221 $padMode = STR_PAD_LEFT; |
|
222 break; |
|
223 |
|
224 default: |
|
225 // This can never happen, but the CS tells I have to have it ... |
|
226 break; |
|
227 } |
|
228 |
|
229 $outputCharset = Zend_Text_Table::getOutputCharset(); |
|
230 $lines = explode("\n", Zend_Text_MultiByte::wordWrap($this->_content, $columnWidth, "\n", true, $outputCharset)); |
|
231 $paddedLines = array(); |
|
232 |
|
233 foreach ($lines AS $line) { |
|
234 $paddedLines[] = str_repeat(' ', $padding) |
|
235 . Zend_Text_MultiByte::strPad($line, $columnWidth, ' ', $padMode, $outputCharset) |
|
236 . str_repeat(' ', $padding); |
|
237 } |
|
238 |
|
239 $result = implode("\n", $paddedLines); |
|
240 |
|
241 return $result; |
|
242 } |
|
243 } |