-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Table.php
124 lines (114 loc) · 2.34 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
<?php
/**
* This file is part of the alphaz Framework.
*
* @author Muhammad Umer Farooq (Malik) <mumerfarooqlablnet01@gmail.com>
*
* @link https://github.com/alphazframework/framework
*
* @author Muhammad Umer Farooq <lablnet01@gmail.com>
*
* @author-profile https://www.facebook.com/Muhammadumerfarooq01/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
*/
namespace alphaz\Console\Input;
class Table
{
/**
* Table header.
*
* @since 1.0.0
*
* @var array
*/
private $header = [];
/**
* Table item.
*
* @since 1.0.0
*
* @var array
*/
private $items = [];
/**
* Create a new Table instance.
*
* @param array $header
* @param array $items
*
* @return void
*/
public function __construct(array $header, array $items)
{
$this->header = $header;
$this->items = $items;
}
/**
* Print table border.
*
* @since 1.0.0
*
* @return array
*/
public function printBorder($row): self
{
foreach ($row as $key => $value) {
$size = mb_strlen($value);
for ($i = 0; $i < $size + 2; $i++) {
echo '-';
}
echo '+';
}
return $this;
}
/**
* Print the row.
*
* @since 1.0.0
*
* @return self
*/
public function printRow($row, $head = false): self
{
if ($head) {
echo ' +';
$this->printBorder($row);
echo "\n";
}
echo ' | ';
foreach ($row as $key => $val) {
echo $val.' | ';
}
if ($head) {
echo "\n";
echo ' +';
$this->printBorder($row);
}
echo "\n";
return $this;
}
/**
* Draw the table.
*
* @since 1.0.0
*
* @return void
*/
public function draw(): void
{
// print the header at top
$this->printRow($this->header, true);
// for other rows
foreach ($this->items as $key => $val) {
$this->printRow($val, false);
}
// for footer
echo ' +';
$this->printBorder($this->header);
echo "\n";
}
}