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