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
|
<?php
namespace Icinga\Module\Translation\Cli;
/**
* Array to Text Table Generation Class
*
* @author Tony Landis <tony@tonylandis.com>
* @link http://www.tonylandis.com/
* @copyright Copyright (C) 2006-2009 Tony Landis
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class ArrayToTextTableHelper
{
/**
* @var array The array for processing
*/
protected $rows;
/**
* @var int The column width settings
*/
protected $cs = array();
/**
* @var int The Row lines settings
*/
protected $rs = array();
/**
* @var int The Column index of keys
*/
protected $keys = array();
/**
* @var int Max Column Height (returns)
*/
protected $mH = 2;
/**
* @var int Max Row Width (chars)
*/
protected $mW = 30;
protected $head = false;
protected $pcen = "+";
protected $prow = "-";
protected $pcol = "|";
/**
* Prepare array into textual format
*
* @param array $rows The input array
* @param bool $head Show heading
* @param int $maxWidth Max Column Height (returns)
* @param int $maxHeight Max Row Width (chars)
*/
public function __construct($rows)
{
$this->rows =& $rows;
$this->cs = array();
$this->rs = array();
if (! $xc = count($this->rows)) {
return false;
}
$this->keys = array_keys($this->rows[0]);
$columns = count($this->keys);
for ($x = 0; $x < $xc; $x++) {
for ($y = 0; $y < $columns; $y++) {
$this->setMax($x, $y, $this->rows[$x][$this->keys[$y]]);
}
}
return $this;
}
/**
* Show the headers using the key values of the array for the titles
*
* @param bool $bool
*/
public function showHeaders($bool)
{
if ($bool) {
$this->setHeading();
}
}
/**
* Set the maximum width (number of characters) per column before truncating
*
* @param int $maxWidth
*/
public function setMaxWidth($maxWidth)
{
$this->mW = (int) $maxWidth;
}
/**
* Set the maximum height (number of lines) per row before truncating
*
* @param int $maxHeight
*/
public function setMaxHeight($maxHeight)
{
$this->mH = (int) $maxHeight;
}
/**
* Prints the data to a text table
*
* @param bool $return Set to 'true' to return text rather than printing
*
* @return mixed
*/
public function render($return = false)
{
if ($return) {
ob_start(null, 0, true);
}
$this->printLine();
$this->printHeading();
$rc = count($this->rows);
for ($i = 0; $i < $rc; $i++) {
$this->printRow($i);
}
$this->printLine(false);
if ($return) {
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return null;
}
protected function setHeading()
{
$data = array();
foreach ($this->keys as $colKey => $value) {
$this->setMax(false, $colKey, $value);
$data[$colKey] = strtoupper($value);
}
if (! is_array($data)) {
return false;
}
$this->head = $data;
return $this;
}
protected function printLine($nl = true)
{
print $this->pcen;
foreach ($this->cs as $key => $val) {
print $this->prow .
str_pad('', $val, $this->prow, STR_PAD_RIGHT) .
$this->prow .
$this->pcen;
}
if ($nl) {
print "\n";
}
}
protected function printHeading()
{
if (! is_array($this->head)) {
return false;
}
print $this->pcol;
foreach ($this->cs as $key => $val) {
print ' ' .
str_pad($this->head[$key], $val, ' ', STR_PAD_BOTH) .
' ' .
$this->pcol;
}
print "\n";
$this->printLine();
return $this;
}
protected function printRow($rowKey)
{
// loop through each line
for ($line = 1; $line <= $this->rs[$rowKey]; $line++) {
print $this->pcol;
for ($colKey = 0; $colKey < count($this->keys); $colKey++) {
print " ";
print str_pad(
substr($this->rows[$rowKey][$this->keys[$colKey]], ($this->mW * ($line - 1)), $this->mW),
$this->cs[$colKey],
' ',
STR_PAD_RIGHT
);
print " " . $this->pcol;
}
print "\n";
}
}
protected function setMax($rowKey, $colKey, &$colVal)
{
$w = mb_strlen($colVal);
$h = 1;
if ($w > $this->mW) {
$h = ceil($w % $this->mW);
if ($h > $this->mH) {
$h = $this->mH;
}
$w = $this->mW;
}
if (! isset($this->cs[$colKey]) || $this->cs[$colKey] < $w) {
$this->cs[$colKey] = $w;
}
if ($rowKey !== false && (! isset($this->rs[$rowKey]) || $this->rs[$rowKey] < $h)) {
$this->rs[$rowKey] = $h;
}
}
}
|