1 | <?php |
||
16 | class Table extends BasicTerminalObject |
||
17 | { |
||
18 | use StringLength; |
||
19 | |||
20 | /** |
||
21 | * The data for the table, an array of (arrays|objects) |
||
22 | * |
||
23 | * @var array $data |
||
24 | */ |
||
25 | protected $data = []; |
||
26 | |||
27 | /** |
||
28 | * An array of the widths of each column in the table |
||
29 | * |
||
30 | * @var array $column_widths |
||
31 | */ |
||
32 | protected $column_widths = []; |
||
33 | |||
34 | /** |
||
35 | * The width of the table |
||
36 | * |
||
37 | * @var integer $table_width |
||
38 | */ |
||
39 | protected $table_width = 0; |
||
40 | |||
41 | /** |
||
42 | * The divider between table cells |
||
43 | * |
||
44 | * @var string $column_divider |
||
45 | */ |
||
46 | protected $column_divider = ' | '; |
||
47 | |||
48 | /** |
||
49 | * The border to divide each row of the table |
||
50 | * |
||
51 | * @var string $border |
||
52 | */ |
||
53 | 28 | protected $border; |
|
54 | |||
55 | 28 | /** |
|
56 | 28 | * The array of rows that will ultimately be returned |
|
57 | * |
||
58 | * @var array $rows |
||
59 | */ |
||
60 | protected $rows = []; |
||
61 | |||
62 | /** |
||
63 | 28 | * @var string $pregix A string to be output before each row is output. |
|
64 | */ |
||
65 | 28 | private $prefix = ""; |
|
66 | 28 | ||
67 | 28 | ||
68 | public function __construct(array $data, $prefix = "") |
||
73 | 28 | ||
74 | 28 | ||
75 | /** |
||
76 | 28 | * @param array $input |
|
77 | * |
||
78 | * @return array |
||
79 | */ |
||
80 | private function getData(array $input) |
||
98 | |||
99 | /** |
||
100 | * Split each row in $data into an array of arrays |
||
101 | * Where each value represents a line in a cell |
||
102 | * |
||
103 | * @param array $data |
||
104 | 28 | * |
|
105 | * @return array |
||
106 | 28 | */ |
|
107 | private function splitRows($data) |
||
129 | 28 | ||
130 | 28 | ||
131 | /** |
||
132 | 28 | * Return the built rows |
|
133 | * |
||
134 | 28 | * @return array |
|
135 | */ |
||
136 | public function result() |
||
153 | |||
154 | /** |
||
155 | 28 | * Append a line to the output. |
|
156 | * |
||
157 | 28 | * @param string $line The line to output |
|
158 | * |
||
159 | 28 | * @return void |
|
160 | 4 | */ |
|
161 | 4 | private function addLine($line) |
|
165 | |||
166 | |||
167 | 28 | /** |
|
168 | 24 | * Determine the width of the table |
|
169 | * |
||
170 | * @return integer |
||
171 | 4 | */ |
|
172 | protected function getWidth() |
||
180 | |||
181 | 28 | /** |
|
182 | * Get the border for each row based on the table width |
||
183 | 28 | */ |
|
184 | 4 | protected function getBorder() |
|
188 | 28 | ||
189 | /** |
||
190 | 28 | * Check for a header row (if it's an array of associative arrays or objects), |
|
191 | 28 | * if there is one, tack it onto the front of the rows array |
|
192 | 28 | */ |
|
193 | 28 | protected function buildHeaderRow() |
|
203 | |||
204 | /** |
||
205 | * Get table row |
||
206 | 28 | * |
|
207 | * @param mixed $columns |
||
208 | 28 | * |
|
209 | * @return string |
||
210 | 28 | */ |
|
211 | protected function buildRow($columns) |
||
223 | 28 | ||
224 | /** |
||
225 | * Build the string for this particular table cell |
||
226 | * |
||
227 | * @param mixed $key |
||
228 | * @param string $column |
||
229 | * |
||
230 | * @return string |
||
231 | */ |
||
232 | protected function buildCell($key, $column) |
||
236 | |||
237 | /** |
||
238 | * Get the header row for the table if it's an associative array or object |
||
239 | * |
||
240 | * @return mixed |
||
241 | */ |
||
242 | protected function getHeaderRow() |
||
257 | |||
258 | /** |
||
259 | * Determine the width of each column |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | protected function getColumnWidths() |
||
281 | |||
282 | /** |
||
283 | * Set up an array of default column widths |
||
284 | * |
||
285 | * @param array $columns |
||
286 | * |
||
287 | * @return array |
||
288 | */ |
||
289 | protected function getDefaultColumnWidths(array $columns) |
||
295 | |||
296 | /** |
||
297 | * Determine the width of the columns without tags |
||
298 | * |
||
299 | * @param array $current_width |
||
300 | * @param string $str |
||
301 | * |
||
302 | * @return integer |
||
303 | */ |
||
304 | protected function getCellWidth($current_width, $str) |
||
308 | } |
||
309 |