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 | * Where each row is an array of arrays, with one value per line in the column. |
||
18 | * |
||
19 | * @var array $data |
||
20 | */ |
||
21 | protected $data = []; |
||
22 | |||
23 | /** |
||
24 | * An array of the widths of each column in the table |
||
25 | * |
||
26 | * @var array $column_widths |
||
27 | */ |
||
28 | protected $column_widths = []; |
||
29 | |||
30 | /** |
||
31 | * The width of the table |
||
32 | * |
||
33 | * @var integer $table_width |
||
34 | */ |
||
35 | protected $table_width = 0; |
||
36 | |||
37 | /** |
||
38 | * The divider between table cells |
||
39 | * |
||
40 | * @var string $column_divider |
||
41 | */ |
||
42 | protected $column_divider = ' | '; |
||
43 | |||
44 | /** |
||
45 | * The border to divide each row of the table |
||
46 | * |
||
47 | * @var string $border |
||
48 | */ |
||
49 | protected $border; |
||
50 | |||
51 | /** |
||
52 | * The array of rows that will ultimately be returned |
||
53 | 28 | * |
|
54 | * @var array $rows |
||
55 | 28 | */ |
|
56 | 28 | protected $rows = []; |
|
57 | |||
58 | /** |
||
59 | * @var string $pregix A string to be output before each row is output. |
||
60 | */ |
||
61 | private $prefix = ""; |
||
62 | |||
63 | 28 | ||
64 | public function __construct(array $data, $prefix = "") |
||
69 | 28 | ||
70 | |||
71 | 28 | /** |
|
72 | 28 | * @param array $input |
|
73 | 28 | * |
|
74 | 28 | * @return array |
|
75 | */ |
||
76 | 28 | private function getData(array $input) |
|
94 | |||
95 | 28 | /** |
|
96 | * @param string $string |
||
97 | 28 | * |
|
98 | * @return array |
||
99 | */ |
||
100 | private function stringToLines(string $string) |
||
104 | 28 | ||
105 | /** |
||
106 | 28 | * Split each row in $data into an array of arrays |
|
107 | * Where each value represents a line in the column |
||
108 | 28 | * |
|
109 | 24 | * @param array $data |
|
110 | 24 | * |
|
111 | 24 | * @return array |
|
112 | 24 | */ |
|
113 | 4 | private function splitRows($data) |
|
135 | |||
136 | |||
137 | /** |
||
138 | * Return the built rows |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | public function result() |
||
159 | 28 | ||
160 | 4 | /** |
|
161 | 4 | * Append a line to the output. |
|
162 | * |
||
163 | 28 | * @param string $line The line to output |
|
164 | 28 | * |
|
165 | * @return void |
||
166 | */ |
||
167 | 28 | private function addLine($line) |
|
171 | 4 | ||
172 | |||
173 | /** |
||
174 | * Determine the width of the table |
||
175 | * |
||
176 | * @return integer |
||
177 | */ |
||
178 | protected function getWidth() |
||
186 | |||
187 | /** |
||
188 | 28 | * Get the border for each row based on the table width |
|
189 | */ |
||
190 | 28 | protected function getBorder() |
|
194 | 28 | ||
195 | /** |
||
196 | 28 | * Check for a header row (if it's an array of associative arrays or objects), |
|
197 | * if there is one, tack it onto the front of the rows array |
||
198 | */ |
||
199 | protected function buildHeaderRow() |
||
209 | |||
210 | 28 | /** |
|
211 | * Get table row |
||
212 | * |
||
213 | * @param mixed $columns |
||
214 | * |
||
215 | * @return string |
||
216 | */ |
||
217 | protected function buildRow($columns) |
||
229 | |||
230 | /** |
||
231 | * Build the string for this particular table cell |
||
232 | * |
||
233 | * @param mixed $key |
||
234 | * @param string $column |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | protected function buildCell($key, $column) |
||
242 | |||
243 | /** |
||
244 | * Get the header row for the table if it's an associative array or object |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | protected function getHeaderRow() |
||
263 | |||
264 | /** |
||
265 | * Determine the width of each column |
||
266 | * |
||
267 | * @return array |
||
268 | */ |
||
269 | protected function getColumnWidths() |
||
287 | |||
288 | /** |
||
289 | * Set up an array of default column widths |
||
290 | * |
||
291 | * @param array $columns |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | protected function getDefaultColumnWidths(array $columns) |
||
301 | |||
302 | /** |
||
303 | * Determine the width of the columns without tags |
||
304 | * |
||
305 | * @param array $current_width |
||
306 | * @param string $str |
||
307 | * |
||
308 | * @return integer |
||
309 | */ |
||
310 | protected function getCellWidth($current_width, $str) |
||
314 | } |
||
315 |