Skip to content

Instantly share code, notes, and snippets.

@nhalstead
Forked from tony-landis/array-to-texttable.php
Last active April 16, 2018 02:50
Show Gist options
  • Save nhalstead/d97665911bd737b926cd4d866e1c3262 to your computer and use it in GitHub Desktop.
Save nhalstead/d97665911bd737b926cd4d866e1c3262 to your computer and use it in GitHub Desktop.
PHP: Array to Text Table Generation Class
<?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
*/
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
*/
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;
}
/**
* Returns the Text Table
*
* @return String The Table in the Text form.
*/
public function render() {
$out = "";
$out .= $this->printLine();
$out .= $this->printHeading();
$rc = count($this->rows);
for($i=0; $i<$rc; $i++) {
$out .= $this->printRow($i);
}
$out .= $this->printLine(false);
return $out;
}
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) {
$o = "";
$o .= $this->pcen;
foreach($this->cs as $key => $val){
$o .= $this->prow;
$o .= str_pad('', $val, $this->prow, STR_PAD_RIGHT);
$o .= $this->prow;
$o .= $this->pcen;
}
if($nl) $o .= "\n";
return $o;
}
private function printHeading() {
$o = "";
if(!is_array($this->head)) return false;
$o .= $this->pcol;
foreach($this->cs as $key => $val){
$o .= ' ';
$o .= str_pad($this->head[$key], $val, ' ', STR_PAD_BOTH);
$o .= ' ' ;
$o .= $this->pcol;
}
$o .= "\n";
$o .= $this->printLine();
return $o;
}
private function printRow($rowKey){
$o = "";
// loop through each line
for($line=1; $line <= $this->rs[$rowKey]; $line++) {
$o .= $this->pcol;
for($colKey=0; $colKey < count($this->keys); $colKey++){
$o .= " ";
$o .= str_pad(substr($this->rows[$rowKey][$this->keys[$colKey]], ($this->mW * ($line-1)), $this->mW), $this->cs[$colKey], ' ', STR_PAD_RIGHT);
$o .= " " . $this->pcol;
}
$o .= "\n";
}
return $o;
}
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;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment