-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtexttable_markdown.class.php
103 lines (88 loc) · 3.17 KB
/
texttable_markdown.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
<?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();
}
require_once(__DIR__ . '/texttable.class.php');
/***
* A class to print text in human friendly markdown tables.
*/
/***
* A class to print text in formatted tables.
*/
class texttable_markdown {
/**
* Formats a fixed-width text table, with borders.
*
* @param $rows array of rows. each row contains table cells.
* @param $headertype keys | firstrow
* @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 = null;
if( $headertype == 'keys' ) {
$header = array_keys( static::obj_arr( $rows[0] ) );
}
else if( $headertype == 'firstrow' ) {
$header = static::obj_arr( array_shift( $rows ) );
}
$col_widths = array();
static::calc_row_col_widths_and_align( $col_widths, $align, $header );
foreach( $rows as $row ) {
$row = static::obj_arr( $row );
static::calc_row_col_widths_and_align( $col_widths, $align, $row );
}
$buf = static::print_row( $col_widths, $align, $header );
$buf .= static::print_divider_row( $col_widths, $align );
foreach( $rows as $row ) {
$row = static::obj_arr( $row );
$buf .= static::print_row( $col_widths, $align, $row );
}
return $buf;
}
static protected function print_divider_row( $col_widths, $align) {
$buf = '|';
$idx = 0;
foreach( $col_widths as $width ) {
$rchar = $align[$idx] == 'right' ? ':' : '-';
$buf .= '-' . str_pad( '-', $width, '-' ) . $rchar . "|";
$idx ++;
}
$buf .= "\n";
return $buf;
}
static protected function print_row( $col_widths, $align, $row ) {
$buf = '|';
$idx = 0;
foreach( $row as $val ) {
$pad_type = $align[$idx] == 'right' ? STR_PAD_LEFT : STR_PAD_RIGHT;
$buf .= ' ' . str_pad( $val, $col_widths[$idx], ' ', $pad_type ) . " |";
$idx ++;
}
return $buf . "\n";
}
static protected function calc_row_col_widths_and_align( &$col_widths, &$align, $row ) {
$idx = 0;
foreach( $row as $val ) {
$len = strlen( $val );
if( $len > @$col_widths[$idx] ) {
$col_widths[$idx] = $len;
}
$align[$idx] = @$align[$idx] == 'right' ? 'right' : (is_numeric(trim($val)) ? 'right' : 'left');
$idx ++;
}
}
static protected function obj_arr( $t ) {
return is_object( $t ) ? get_object_vars( $t ) : $t;
}
}