Skip to content

Commit 5bc42f6

Browse files
authored
Created the class with modifications
The class was originally found here: http://tonylandis.com/php/php-text-tables-class/
1 parent 02040e1 commit 5bc42f6

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed

ArrayToTextTable.php

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
/**
3+
* Array to Text Table Generation Class
4+
*
5+
* @author Tony Landis <[email protected]>
6+
* @link http://www.tonylandis.com/
7+
* @copyright Copyright (C) 2006-2009 Tony Landis
8+
* @license http://www.opensource.org/licenses/bsd-license.php
9+
*/
10+
11+
// +----------------------------------------+ //
12+
// Modified for PHP 7 by Imtiaz Mahbub
13+
// +----------------------------------------+ //
14+
15+
class ArrayToTextTable
16+
{
17+
/**
18+
* @var array The array for processing
19+
*/
20+
private $rows;
21+
22+
/**
23+
* @var int The column width settings
24+
*/
25+
private $cs = array();
26+
27+
/**
28+
* @var int The Row lines settings
29+
*/
30+
private $rs = array();
31+
32+
/**
33+
* @var int The Column index of keys
34+
*/
35+
private $keys = array();
36+
37+
/**
38+
* @var int Max Column Height (returns)
39+
*/
40+
private $mH = 2;
41+
42+
/**
43+
* @var int Max Row Width (chars)
44+
*/
45+
private $mW = 30;
46+
47+
private $head = false;
48+
private $pcen = "+";
49+
private $prow = "-";
50+
private $pcol = "|";
51+
52+
53+
/** Prepare array into textual format
54+
*
55+
* @param array $rows The input array
56+
* @param bool $head Show heading
57+
* @param int $maxWidth Max Column Height (returns)
58+
* @param int $maxHeight Max Row Width (chars)
59+
*/
60+
public function __construct($rows)
61+
{
62+
$this->rows =& $rows;
63+
$this->cs=array();
64+
$this->rs=array();
65+
66+
if(!$xc = count($this->rows)) return false;
67+
$this->keys = array_keys($this->rows[0]);
68+
$columns = count($this->keys);
69+
70+
for($x=0; $x<$xc; $x++)
71+
for($y=0; $y<$columns; $y++)
72+
$this->setMax($x, $y, $this->rows[$x][$this->keys[$y]]);
73+
}
74+
75+
/**
76+
* Show the headers using the key values of the array for the titles
77+
*
78+
* @param bool $bool
79+
*/
80+
public function showHeaders($bool)
81+
{
82+
if($bool) $this->setHeading();
83+
}
84+
85+
/**
86+
* Set the maximum width (number of characters) per column before truncating
87+
*
88+
* @param int $maxWidth
89+
*/
90+
public function setMaxWidth($maxWidth)
91+
{
92+
$this->mW = (int) $maxWidth;
93+
}
94+
95+
/**
96+
* Set the maximum height (number of lines) per row before truncating
97+
*
98+
* @param int $maxHeight
99+
*/
100+
public function setMaxHeight($maxHeight)
101+
{
102+
$this->mH = (int) $maxHeight;
103+
}
104+
105+
/**
106+
* Prints the data to a text table
107+
*
108+
* @param bool $return Set to 'true' to return text rather than printing
109+
* @return mixed
110+
*/
111+
public function render($return=false)
112+
{
113+
if($return) ob_start();
114+
115+
$this->printLine();
116+
$this->printHeading();
117+
118+
$rc = count($this->rows);
119+
for($i=0; $i<$rc; $i++) $this->printRow($i);
120+
121+
$this->printLine(false);
122+
123+
if($return) {
124+
$contents = ob_get_clean();
125+
return $contents;
126+
}
127+
}
128+
129+
private function setHeading()
130+
{
131+
$data = array();
132+
foreach($this->keys as $colKey => $value)
133+
{
134+
$this->setMax(false, $colKey, $value);
135+
$data[$colKey] = strtoupper($value);
136+
}
137+
if(!is_array($data)) return false;
138+
$this->head = $data;
139+
}
140+
141+
private function printLine($nl=true)
142+
{
143+
print $this->pcen;
144+
foreach($this->cs as $key => $val)
145+
print $this->prow .
146+
str_pad('', $val, $this->prow, STR_PAD_RIGHT) .
147+
$this->prow .
148+
$this->pcen;
149+
if($nl) print "\n";
150+
}
151+
152+
private function printHeading()
153+
{
154+
if(!is_array($this->head)) return false;
155+
156+
print $this->pcol;
157+
foreach($this->cs as $key => $val)
158+
print ' '.
159+
str_pad($this->head[$key], $val, ' ', STR_PAD_BOTH) .
160+
' ' .
161+
$this->pcol;
162+
163+
print "\n";
164+
$this->printLine();
165+
}
166+
167+
private function printRow($rowKey)
168+
{
169+
// loop through each line
170+
for($line=1; $line <= $this->rs[$rowKey]; $line++)
171+
{
172+
print $this->pcol;
173+
for($colKey=0; $colKey < count($this->keys); $colKey++)
174+
{
175+
print " ";
176+
print str_pad(substr($this->rows[$rowKey][$this->keys[$colKey]], ($this->mW * ($line-1)), $this->mW), $this->cs[$colKey], ' ', STR_PAD_RIGHT);
177+
print " " . $this->pcol;
178+
}
179+
print "\n";
180+
}
181+
}
182+
183+
private function setMax($rowKey, $colKey, &$colVal)
184+
{
185+
$w = mb_strlen($colVal);
186+
$h = 1;
187+
if($w > $this->mW)
188+
{
189+
$h = ceil($w % $this->mW);
190+
if($h > $this->mH) $h=$this->mH;
191+
$w = $this->mW;
192+
}
193+
194+
if(!isset($this->cs[$colKey]) || $this->cs[$colKey] < $w)
195+
$this->cs[$colKey] = $w;
196+
197+
if($rowKey !== false && (!isset($this->rs[$rowKey]) || $this->rs[$rowKey] < $h))
198+
$this->rs[$rowKey] = $h;
199+
}
200+
}
201+
?>

0 commit comments

Comments
 (0)