1 | <?php |
||
11 | class Table extends BasicTerminalObject |
||
12 | { |
||
13 | use StringLength; |
||
14 | |||
15 | /** |
||
16 | * The data for the table, an array of (arrays|objects) |
||
17 | * |
||
18 | * @var array $data |
||
19 | */ |
||
20 | protected $data = []; |
||
21 | |||
22 | /** |
||
23 | * An array of the widths of each column in the table |
||
24 | * |
||
25 | * @var array $column_widths |
||
26 | */ |
||
27 | protected $column_widths = []; |
||
28 | |||
29 | /** |
||
30 | * The width of the table |
||
31 | * |
||
32 | * @var integer $table_width |
||
33 | */ |
||
34 | protected $table_width = 0; |
||
35 | |||
36 | /** |
||
37 | * The divider between table cells |
||
38 | * |
||
39 | * @var string $column_divider |
||
40 | */ |
||
41 | protected $column_divider = ' | '; |
||
42 | |||
43 | /** |
||
44 | * The border to divide each row of the table |
||
45 | * |
||
46 | * @var string $border |
||
47 | */ |
||
48 | protected $border; |
||
49 | |||
50 | /** |
||
51 | * The array of rows that will ultimately be returned |
||
52 | * |
||
53 | 28 | * @var array $rows |
|
54 | */ |
||
55 | 28 | protected $rows = []; |
|
56 | 28 | ||
57 | /** |
||
58 | * @var string $pregix A string to be output before each row is output. |
||
59 | */ |
||
60 | private $prefix = ""; |
||
61 | |||
62 | |||
63 | 28 | public function __construct(array $data, $prefix = "") |
|
68 | |||
69 | 28 | ||
70 | /** |
||
71 | 28 | * @param array $input |
|
72 | 28 | * |
|
73 | 28 | * @return array |
|
74 | 28 | */ |
|
75 | private function getData(array $input) |
||
93 | |||
94 | |||
95 | 28 | /** |
|
96 | * Return the built rows |
||
97 | 28 | * |
|
98 | * @return array |
||
99 | */ |
||
100 | public function result() |
||
115 | 28 | ||
116 | /** |
||
117 | * Append a line to the output. |
||
118 | * |
||
119 | * @param string $line The line to output |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | private function addLine($line) |
||
127 | |||
128 | 28 | ||
129 | 28 | /** |
|
130 | 28 | * Determine the width of the table |
|
131 | * |
||
132 | 28 | * @return integer |
|
133 | */ |
||
134 | 28 | protected function getWidth() |
|
141 | |||
142 | /** |
||
143 | * Get the border for each row based on the table width |
||
144 | */ |
||
145 | 28 | protected function getBorder() |
|
149 | |||
150 | /** |
||
151 | * Check for a header row (if it's an array of associative arrays or objects), |
||
152 | * if there is one, tack it onto the front of the rows array |
||
153 | */ |
||
154 | protected function buildHeaderRow() |
||
164 | 28 | ||
165 | /** |
||
166 | * Get table row |
||
167 | 28 | * |
|
168 | 24 | * @param mixed $columns |
|
169 | * |
||
170 | * @return string |
||
171 | 4 | */ |
|
172 | protected function buildRow($columns) |
||
184 | 4 | ||
185 | 4 | /** |
|
186 | * Build the string for this particular table cell |
||
187 | * |
||
188 | 28 | * @param mixed $key |
|
189 | * @param string $column |
||
190 | 28 | * |
|
191 | 28 | * @return string |
|
192 | 28 | */ |
|
193 | 28 | protected function buildCell($key, $column) |
|
197 | |||
198 | /** |
||
199 | * Get the header row for the table if it's an associative array or object |
||
200 | * |
||
201 | * @return mixed |
||
202 | */ |
||
203 | protected function getHeaderRow() |
||
217 | |||
218 | /** |
||
219 | * Determine the width of each column |
||
220 | * |
||
221 | 28 | * @return array |
|
222 | */ |
||
223 | 28 | protected function getColumnWidths() |
|
238 | |||
239 | /** |
||
240 | * Set up an array of default column widths |
||
241 | * |
||
242 | * @param array $columns |
||
243 | * |
||
244 | * @return array |
||
245 | */ |
||
246 | protected function getDefaultColumnWidths(array $columns) |
||
252 | |||
253 | /** |
||
254 | * Determine the width of the columns without tags |
||
255 | * |
||
256 | * @param array $current_width |
||
257 | * @param string $str |
||
258 | * |
||
259 | * @return integer |
||
260 | */ |
||
261 | protected function getCellWidth($current_width, $str) |
||
265 | } |
||
266 |