-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArrayToTextTable.php
201 lines (172 loc) · 4.89 KB
/
ArrayToTextTable.php
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
<?php
/**
* Array to Text Table Generation Class
*
* @author Tony Landis <[email protected]>
* @link http://www.tonylandis.com/
* @copyright Copyright (C) 2006-2009 Tony Landis
* @license http://www.opensource.org/licenses/bsd-license.php
*/
// +----------------------------------------+ //
// Modified for PHP 7 by Imtiaz Mahbub
// +----------------------------------------+ //
class ArrayToTextTable
{
/**
* @var array The array for processing
*/
private $rows;
/**
* @var int The column width settings
*/
private $cs = array();
/**
* @var int The Row lines settings
*/
private $rs = array();
/**
* @var int The Column index of keys
*/
private $keys = array();
/**
* @var int Max Column Height (returns)
*/
private $mH = 2;
/**
* @var int Max Row Width (chars)
*/
private $mW = 30;
private $head = false;
private $pcen = "+";
private $prow = "-";
private $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]]);
}
/**
* 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();
$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_clean();
return $contents;
}
}
private 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;
}
private 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";
}
private 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();
}
private 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";
}
}
private 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;
}
}
?>