-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextTable.php
128 lines (115 loc) · 3.21 KB
/
TextTable.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
<?php
/**
* Creates a markdown table based on the parsed documentation
*
* @author Peter-Christoph Haider <[email protected]>
* @author PICCORO Lenz MCKAY <[email protected]>
* @package Apidoc
* @version 1.00 (2014-04-04)
* @license GNU Lesser Public License
*/
class TextTable {
/** @var int max leng of descriptions */
public $maxlen = 50;
/** @var array data to be procesed */
private $data = array();
/** @var array header of the table output text */
private $header = array();
/** @var array The source path */
private $len = array();
/** @var array alingment of the headers columns */
private $align = array(
'name' => 'L',
'type' => 'C'
);
/**
* can init the default values to markdown text table
*
* @param array $header The header array [key => label, ...]
* @param array $content Content table as matrix of tags funtions
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function __construct($header=null, $content=array(), $align=false) {
if ($header) {
$this->header = $header;
} elseif ($content) {
foreach ($content[0] as $key => $value)
$this->header[$key] = $key;
}
foreach ($this->header as $key => $label) {
$this->len[$key] = strlen($label);
}
if (is_array($align))
$this->setAlgin($align);
$this->addData($content);
}
/**
* Overwrite the alignment array
*
* @param array $align Alignment optios [key => L|R|C, ...]
*/
public function setAlgin($align) {
$this->align = $align;
}
/**
* Add data to the table from matrix array tags class
*
* @param array $content Content matrix of tags class
* @return class self object class with data content populated
*/
public function addData($content) {
foreach ($content as &$row) {
foreach ($this->header as $key => $value) {
if (!isset($row[$key])) {
$row[$key] = '-';
} elseif (strlen($row[$key]) > $this->maxlen) {
$this->len[$key] = $this->maxlen;
} elseif (strlen($row[$key]) > $this->len[$key]) {
$this->len[$key] = strlen($row[$key]);
}
}
}
$this->data = $this->data + $content;
return $this;
}
/**
* Add a delimiter based on aling of the class setted
*
* @return string
*/
private function renderDelimiter() {
$res = '|';
foreach ($this->len as $key => $l)
$res .= (isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'L') ? ':' : ' ')
.str_repeat('-', $l)
.(isset($this->align[$key]) && ($this->align[$key] == 'C' || $this->align[$key] == 'R') ? ':' : ' ')
.'|';
return $res."\n";
}
/**
* Render a single row for markdown table
*
* @param array $row
* @return string
*/
private function renderRow($row) {
$res = '|';
foreach ($this->len as $key => $l) {
$res .= ' '.$row[$key].($l > strlen($row[$key]) ? str_repeat(' ', $l - strlen($row[$key])) : '').' |';
}
return $res."\n";
}
/**
* Render the table text as markdown
*
* @param array $content Additional table content
* @return string
*/
public function render($content=array()) {
$this->addData($content);
$res = $this->renderRow($this->header).$this->renderDelimiter();
foreach ($this->data as $row)
$res .= $this->renderRow($row);
return $res;
}
}