-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathtable.php
232 lines (209 loc) · 7.52 KB
/
table.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
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
class table{
public function __construct($table){
$this->headers = $table['header'];
$this->rows = $table['rows'];
$this->crossingChar = '+';
$this->horizontalBorderChar = '-';
$this->verticalBorderChar = '|';
$this->borderFormat = '%s';
$this->cellHeaderFormat = '%s';
$this->cellRowFormat = '%s';
$this->paddingChar = ' ';
$this->padType = STR_PAD_RIGHT;
}
/**
* Renders table to output.
*
* Example:
* +---------------+-----------------------+------------------+
* | ISBN | Title | Author |
* +---------------+-----------------------+------------------+
* | 99921-58-10-7 | Divine Comedy | Dante Alighieri |
* | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
* | 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
* +---------------+-----------------------+------------------+
*
*/
public function render($out = true){
if(!$this->rows)
exit('invalid table content');
//获得表头行首 +---------------+-----------------------+------------------+
$output = $this->renderRowSeparator();
//获取头部输出| ISBN | Title | Author |
$output .= $this->renderRow($this->headers, $this->cellHeaderFormat);
//header存在的话再输出行分割符
if ($this->headers) {
$output .= $this->renderRowSeparator();
}
//渲染每一行
foreach ($this->rows as $row) {
$output .= $this->renderRow($row, $this->cellRowFormat);
}
if ($this->rows) {
$output .= $this->renderRowSeparator();
}
if($out){
exit($output);
}else{
$this->cleanup();
return $output;
}
}
//渲染表格行起始分割行
private function renderRowSeparator(){
if (0 === $count = $this->getNumberOfColumns()) {
return;
}
$markup = $this->crossingChar;
for ($column = 0; $column < $count; $column++) {
$markup .= str_repeat($this->horizontalBorderChar, $this->getColumnWidth($column))
.$this->crossingChar
;
}
return sprintf($this->borderFormat, $markup).PHP_EOL;
}
/**
* 渲染表格行.
*
* Example: | 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
*
* @param array $row
* @param string $cellFormat
*/
private function renderRow(array $row, $cellFormat){
if (empty($row)) {
return;
}
$output = $this->renderColumnSeparator();
for ($column = 0, $count = $this->getNumberOfColumns(); $column < $count; $column++) {
$output .= $this->renderCell($row, $column, $cellFormat);
$output .= $this->renderColumnSeparator();
}
$output .= $this->writeln('');
return $output;
}
/**
* 带边距的渲染单元格.
*
* @param array $row
* @param integer $column
* @param string $cellFormat
*/
private function renderCell(array $row, $column, $cellFormat){
$cell = isset($row[$column]) ? $row[$column] : '';
return sprintf(
$cellFormat,
$this->str_pad(
$this->paddingChar.$cell.$this->paddingChar,
$this->getColumnWidth($column),
$this->paddingChar,
$this->padType
)
);
}
/**
* 渲染水平列分隔符.
*/
private function renderColumnSeparator(){
return(sprintf($this->borderFormat, $this->verticalBorderChar));
}
/**
* 获取表格的列数.
*
* @return int
*/
private function getNumberOfColumns() {
if (null !== $this->numberOfColumns) {
return $this->numberOfColumns;
}
$columns = array(0);
$columns[] = count($this->headers);
foreach ($this->rows as $row) {
$columns[] = count($row);
}
return $this->numberOfColumns = max($columns);
}
/**
* 获取列宽.
*
* @param integer $column
*
* @return int
*/
private function getColumnWidth($column) {
if (isset($this->columnWidths[$column])) {
return $this->columnWidths[$column];
}
$lengths = array(0);
$lengths[] = $this->getCellWidth($this->headers, $column);
foreach ($this->rows as $row) {
$lengths[] = $this->getCellWidth($row, $column);
}
return $this->columnWidths[$column] = max($lengths) + 2;
}
/**
* 获取单元格宽度.
*
* @param array $row
* @param integer $column
*
* @return int
*/
private function getCellWidth(array $row, $column) {
if ($column < 0) {
return 0;
}
if (isset($row[$column])) {
return $this->strlen($row[$column]);
}
return $this->getCellWidth($row, $column - 1);
}
/**
* Returns the length of a string, using mb_strlen if it is available.
*
* @param string $string The string to check its length
*
* @return integer The length of the string
*/
protected function strlen($string) {
// if (!function_exists('mb_strlen')) {
return (strlen($string) + mb_strlen($string,'UTF8')) / 2;
// }
// if (false === $encoding = mb_detect_encoding($string)) {
// return strlen($string);
// }
// return mb_strlen($string, $encoding);
}
/**
* Called after rendering to cleanup cache data.
*/
private function cleanup(){
$this->columnWidths = array();
$this->numberOfColumns = null;
}
public function writeln($line=''){
return $line.PHP_EOL;
}
public function str_pad($input , $pad_length ,$pad_string , $pad_type){
$strlen = $this->strlen($input);
if($strlen < $pad_length){
$difference = $pad_length - $strlen;
switch ($pad_type) {
case STR_PAD_RIGHT:
return $input . str_repeat($pad_string, $difference);
break;
case STR_PAD_LEFT:
return str_repeat($pad_string, $difference) . $input;
break;
default:
$left = $difference / 2;
$right = $difference - $left;
return str_repeat($pad_string, $left) . $input . str_repeat($pad_string, $right);
break;
}
}else{
return $input;
}
}
}