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 | * @var array $rows |
||
54 | */ |
||
55 | protected $rows = []; |
||
56 | |||
57 | /** |
||
58 | * @var string $pregix A string to be output before each row is output. |
||
59 | */ |
||
60 | private $prefix = ""; |
||
61 | |||
62 | |||
63 | 36 | public function __construct(array $data, $prefix = "") |
|
68 | |||
69 | |||
70 | /** |
||
71 | * @param array $input |
||
72 | * |
||
73 | * @return array |
||
74 | */ |
||
75 | 36 | private function getData(array $input) |
|
76 | { |
||
77 | 36 | $output = []; |
|
78 | |||
79 | 36 | foreach ($input as $item) { |
|
80 | 36 | if (is_object($item)) { |
|
81 | 4 | $item = get_object_vars($item); |
|
82 | } |
||
83 | |||
84 | 36 | if (!is_array($item)) { |
|
85 | 4 | throw new InvalidArgumentException("Invalid table data, you must pass an array of arrays or objects"); |
|
86 | } |
||
87 | |||
88 | 32 | $output[] = $item; |
|
89 | } |
||
90 | |||
91 | 32 | return $output; |
|
92 | } |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Return the built rows |
||
97 | * |
||
98 | * @return array |
||
99 | */ |
||
100 | 32 | public function result() |
|
101 | { |
||
102 | 32 | $this->column_widths = $this->getColumnWidths(); |
|
103 | 32 | $this->table_width = $this->getWidth(); |
|
104 | 32 | $this->border = $this->getBorder(); |
|
105 | |||
106 | 32 | $this->buildHeaderRow(); |
|
107 | |||
108 | 32 | foreach ($this->data as $columns) { |
|
109 | 32 | $this->addLine($this->buildRow($columns)); |
|
110 | 32 | $this->addLine($this->border); |
|
111 | } |
||
112 | |||
113 | 32 | return $this->rows; |
|
114 | } |
||
115 | |||
116 | /** |
||
117 | * Append a line to the output. |
||
118 | * |
||
119 | * @param string $line The line to output |
||
120 | * |
||
121 | * @return void |
||
122 | */ |
||
123 | 32 | private function addLine($line) |
|
124 | { |
||
125 | 32 | $this->rows[] = $this->prefix . $line; |
|
126 | 32 | } |
|
127 | |||
128 | |||
129 | /** |
||
130 | * Determine the width of the table |
||
131 | * |
||
132 | * @return integer |
||
133 | */ |
||
134 | 32 | protected function getWidth() |
|
135 | { |
||
136 | 32 | $first_row = reset($this->data); |
|
137 | 32 | $first_row = $this->buildRow($first_row); |
|
138 | |||
139 | 32 | return $this->lengthWithoutTags($first_row); |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Get the border for each row based on the table width |
||
144 | */ |
||
145 | 32 | 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 | 32 | protected function buildHeaderRow() |
|
164 | |||
165 | /** |
||
166 | * Get table row |
||
167 | * |
||
168 | * @param mixed $columns |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | 32 | protected function buildRow($columns) |
|
184 | |||
185 | /** |
||
186 | * Build the string for this particular table cell |
||
187 | * |
||
188 | * @param mixed $key |
||
189 | * @param string $column |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | 32 | 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 | 32 | protected function getHeaderRow() |
|
217 | |||
218 | /** |
||
219 | * Determine the width of each column |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | 32 | 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 | 32 | 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 | 32 | protected function getCellWidth($current_width, $str) |
|
265 | } |
||
266 |