1 | <?php |
||
7 | class Table extends BasicTerminalObject |
||
8 | { |
||
9 | use StringLength; |
||
10 | |||
11 | /** |
||
12 | * The data for the table, an array of (arrays|objects) |
||
13 | * |
||
14 | * @var array $data |
||
15 | */ |
||
16 | protected $data = []; |
||
17 | |||
18 | /** |
||
19 | * An array of the widths of each column in the table |
||
20 | * |
||
21 | * @var array $column_widths |
||
22 | */ |
||
23 | protected $column_widths = []; |
||
24 | |||
25 | /** |
||
26 | * The width of the table |
||
27 | * |
||
28 | * @var integer $table_width |
||
29 | */ |
||
30 | protected $table_width = 0; |
||
31 | |||
32 | /** |
||
33 | * The divider between table cells |
||
34 | * |
||
35 | * @var string $column_divider |
||
36 | */ |
||
37 | protected $column_divider = ' | '; |
||
38 | |||
39 | /** |
||
40 | * The border to divide each row of the table |
||
41 | * |
||
42 | * @var string $border |
||
43 | */ |
||
44 | protected $border; |
||
45 | |||
46 | /** |
||
47 | * The array of rows that will ultimately be returned |
||
48 | * |
||
49 | * @var array $rows |
||
50 | */ |
||
51 | protected $rows = []; |
||
52 | |||
53 | 28 | /** |
|
54 | * @var string $pregix A string to be output before each row is output. |
||
55 | 28 | */ |
|
56 | 28 | private $prefix = ""; |
|
57 | |||
58 | |||
59 | public function __construct(array $data, $prefix = "") |
||
64 | |||
65 | 28 | /** |
|
66 | 28 | * Return the built rows |
|
67 | 28 | * |
|
68 | * @return array |
||
69 | 28 | */ |
|
70 | public function result() |
||
85 | |||
86 | 28 | /** |
|
87 | 28 | * Append a line to the output. |
|
88 | * |
||
89 | 28 | * @param string $line The line to output |
|
90 | * |
||
91 | * @return void |
||
92 | */ |
||
93 | private function addLine($line) |
||
97 | 28 | ||
98 | |||
99 | /** |
||
100 | * Determine the width of the table |
||
101 | * |
||
102 | * @return integer |
||
103 | */ |
||
104 | 28 | protected function getWidth() |
|
111 | 24 | ||
112 | 24 | /** |
|
113 | 4 | * Get the border for each row based on the table width |
|
114 | */ |
||
115 | 28 | protected function getBorder() |
|
119 | |||
120 | /** |
||
121 | * Check for a header row (if it's an array of associative arrays or objects), |
||
122 | * if there is one, tack it onto the front of the rows array |
||
123 | */ |
||
124 | 28 | protected function buildHeaderRow() |
|
134 | 28 | ||
135 | /** |
||
136 | * Get table row |
||
137 | * |
||
138 | * @param mixed $columns |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | protected function buildRow($columns) |
||
154 | |||
155 | 28 | /** |
|
156 | * Build the string for this particular table cell |
||
157 | 28 | * |
|
158 | * @param mixed $key |
||
159 | 28 | * @param string $column |
|
160 | 4 | * |
|
161 | 4 | * @return string |
|
162 | */ |
||
163 | 28 | protected function buildCell($key, $column) |
|
167 | 28 | ||
168 | 24 | /** |
|
169 | * Get the header row for the table if it's an associative array or object |
||
170 | * |
||
171 | 4 | * @return mixed |
|
172 | */ |
||
173 | protected function getHeaderRow() |
||
191 | 28 | ||
192 | 28 | /** |
|
193 | 28 | * Determine the width of each column |
|
194 | 28 | * |
|
195 | * @return array |
||
196 | 28 | */ |
|
197 | protected function getColumnWidths() |
||
216 | |||
217 | /** |
||
218 | * Set up an array of default column widths |
||
219 | * |
||
220 | * @param array $columns |
||
221 | 28 | * |
|
222 | * @return array |
||
223 | 28 | */ |
|
224 | protected function getDefaultColumnWidths(array $columns) |
||
230 | |||
231 | /** |
||
232 | * Determine the width of the columns without tags |
||
233 | * |
||
234 | * @param array $current_width |
||
235 | * @param string $str |
||
236 | * |
||
237 | * @return integer |
||
238 | */ |
||
239 | protected function getCellWidth($current_width, $str) |
||
243 | } |
||
244 |