-
Notifications
You must be signed in to change notification settings - Fork 2
/
texttable.class.php
135 lines (117 loc) · 4.41 KB
/
texttable.class.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
<?php
// be safe and sane. use strictmode if available via composer.
$autoload_file = __DIR__ . '/vendor/autoload.php';
if( file_exists( $autoload_file )) {
require_once($autoload_file);
\strictmode\initializer::init();
}
/***
* A class to print text in formatted tables.
*/
class texttable {
/**
* Formats a fixed-width text table, with borders.
*
* @param $rows array of rows. each row contains table cells.
* @param $headertype keys | firstrow | none/null
* @param $footertype keys | lastrow | none/null
* @param $empty_row_string String to use when there is no data, or null.
*/
static public function table( $rows, $headertype = 'keys', $footertype = 'none', $empty_row_string = 'No Data' ) {
if( !@count( $rows ) ) {
if( $empty_row_string !== null ) {
$rows = [ [ $empty_row_string ] ];
}
else {
return '';
}
}
$header = $footer = null;
if( $headertype == 'keys' ) {
$header = array_keys( static::obj_arr( $rows[0] ) );
}
else if( $headertype == 'firstrow' ) {
$header = static::obj_arr( array_shift( $rows ) );
}
if( $footertype == 'keys' && count( $rows ) ) {
$footer = array_keys( static::obj_arr( $rows[count($rows) - 1] ) );
}
else if( $footertype == 'lastrow' && count( $rows ) ) {
$footer = static::obj_arr( array_pop( $rows ) );
}
$col_widths = array();
if( $header ) {
static::calc_row_col_widths( $col_widths, $header );
}
if( $footer ) {
static::calc_row_col_widths( $col_widths, $footer );
}
foreach( $rows as $row ) {
$row = static::obj_arr( $row );
static::calc_row_col_widths( $col_widths, $row );
}
$buf = '';
$buf .= static::print_divider_row( $col_widths, 'top' );
if( $header ) {
$buf .= static::print_header($col_widths, $header );
}
foreach( $rows as $row ) {
$row = static::obj_arr( $row );
$buf .= static::print_row( $col_widths, $row );
}
$buf .= static::print_divider_row( $col_widths, 'bottom' );
if( $footer ) {
$buf .= static::print_footer($col_widths, $footer );
}
return $buf;
}
static protected function print_footer($col_widths, $footer) {
$buf = static::print_row( $col_widths, $footer );
$buf .= static::print_divider_row( $col_widths, 'footer' );
return $buf;
}
static protected function print_header($col_widths, $header) {
$buf = static::print_row( $col_widths, $header );
$buf .= static::print_divider_row( $col_widths, 'header' );
return $buf;
}
static protected function print_divider_row( $col_widths, $position ) {
$buf = '+';
foreach( $col_widths as $width ) {
$buf .= '-' . str_pad( '-', $width, '-' ) . "-+";
}
$buf .= "\n";
return $buf;
}
static protected function print_row( $col_widths, $row ) {
$buf = '|';
$idx = 0;
foreach( $row as $val ) {
$pad_type = is_numeric( $val ) ? STR_PAD_LEFT : STR_PAD_RIGHT;
$buf .= ' ' . self::mb_str_pad_to_width( $val, $col_widths[$idx], ' ', $pad_type ) . " |";
$idx ++;
}
return $buf . "\n";
}
static protected function calc_row_col_widths( &$col_widths, $row ) {
$idx = 0;
foreach( $row as $val ) {
$len = mb_strwidth( $val );
if( $len > @$col_widths[$idx] ) {
$col_widths[$idx] = $len;
}
$idx ++;
}
}
static protected function obj_arr( $t ) {
return is_object( $t ) ? get_object_vars( $t ) : $t;
}
/* Pads a string to a given width, where width is measured as per
* mb_strwidth, ie regular chars count as 1, and double-wide chars count as 2.
*/
static protected function mb_str_pad_to_width($input, $pad_length, $pad_string=' ', $pad_type=STR_PAD_RIGHT,$encoding='UTF-8'){
$mb_diff=mb_strlen($input, $encoding)-strlen($input);
$padded = str_pad($input,$pad_length-$mb_diff,$pad_string,$pad_type);
return mb_strimwidth($padded, 0, $pad_length);
}
}