1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Kernel\Cli\Writers; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | /** |
||
19 | * Class Table |
||
20 | * |
||
21 | * Table generator for PHP command line interface (cli). |
||
22 | * Based on LucidFrame\Console\ConsoleTable class. |
||
23 | * |
||
24 | * @author Sithu K. <[email protected]> |
||
25 | * Steeve Andrian Salim |
||
26 | * |
||
27 | * @package O2System\Kernel\Cli\Writers |
||
28 | */ |
||
29 | class Table |
||
30 | { |
||
31 | /** |
||
32 | * Table::HEADER_INDEX |
||
33 | * |
||
34 | * @var int |
||
35 | */ |
||
36 | const HEADER_INDEX = -1; |
||
37 | /** |
||
38 | * Table::$isShowBorder |
||
39 | * |
||
40 | * Table show border flag. |
||
41 | * |
||
42 | * @var bool |
||
43 | */ |
||
44 | public $isShowBorder = true; |
||
45 | /** |
||
46 | * Table::$rows |
||
47 | * |
||
48 | * Array of table rows. |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $rows = []; |
||
53 | /** |
||
54 | * Table::$padding |
||
55 | * |
||
56 | * Numbers of table padding. |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | protected $padding = 1; |
||
61 | |||
62 | /** |
||
63 | * Table::$leftMargin |
||
64 | * |
||
65 | * Numbers of table left margin. |
||
66 | * |
||
67 | * @var int |
||
68 | */ |
||
69 | protected $leftMargin = 0; |
||
70 | |||
71 | /** |
||
72 | * Table::$rowIndex |
||
73 | * |
||
74 | * Table row indexing numbers. |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | private $rowIndex = -1; |
||
79 | |||
80 | /** |
||
81 | * Table::$columnsWidths |
||
82 | * |
||
83 | * Cache list of table column widths. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | private $columnWidths = []; |
||
88 | |||
89 | // ------------------------------------------------------------------------ |
||
90 | |||
91 | /** |
||
92 | * Table::setHeaders |
||
93 | * |
||
94 | * Adds the headers for the columns |
||
95 | * |
||
96 | * @param array $headers of header cell content |
||
97 | * |
||
98 | * @return static |
||
99 | */ |
||
100 | public function setHeaders(array $headers) |
||
101 | { |
||
102 | $this->rows[ self::HEADER_INDEX ] = $headers; |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | // ------------------------------------------------------------------------ |
||
108 | |||
109 | /** |
||
110 | * Table::addHeader |
||
111 | * |
||
112 | * Adds a column to the table header |
||
113 | * |
||
114 | * @param mixed $content Table column cell content. |
||
115 | * |
||
116 | * @return static |
||
117 | */ |
||
118 | public function addHeader($content = '') |
||
119 | { |
||
120 | $this->rows[ self::HEADER_INDEX ][] = $content; |
||
121 | |||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | // ------------------------------------------------------------------------ |
||
126 | |||
127 | /** |
||
128 | * Table::getHeaders |
||
129 | * |
||
130 | * Get the row of header. |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function getHeaders() |
||
135 | { |
||
136 | return isset($this->rows[ self::HEADER_INDEX ]) |
||
137 | ? $this->rows[ self::HEADER_INDEX ] |
||
138 | : null; |
||
139 | } |
||
140 | |||
141 | // ------------------------------------------------------------------------ |
||
142 | |||
143 | /** |
||
144 | * Table::addRow |
||
145 | * |
||
146 | * Adds a row to the table. |
||
147 | * |
||
148 | * @param array $data The row data to add |
||
149 | * |
||
150 | * @return static |
||
151 | */ |
||
152 | public function addRow($data = null) |
||
153 | { |
||
154 | $this->rowIndex++; |
||
155 | if (is_array($data)) { |
||
156 | foreach ($data as $column => $content) { |
||
157 | $this->rows[ $this->rowIndex ][ $column ] = $content; |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | // ------------------------------------------------------------------------ |
||
165 | |||
166 | /** |
||
167 | * Adds a column to the table |
||
168 | * |
||
169 | * @param mixed $content The data of the column |
||
170 | * @param int $columnIndex The column index to populate |
||
171 | * @param int $rowIndex The row index, if the row isn't starting from zero, specify it here. |
||
172 | * |
||
173 | * @return static |
||
174 | */ |
||
175 | public function addColumn($content, $columnIndex = null, $rowIndex = null) |
||
176 | { |
||
177 | $rowIndex = $rowIndex === null |
||
178 | ? $this->rowIndex |
||
179 | : $rowIndex; |
||
180 | if ($columnIndex === null) { |
||
181 | $columnIndex = isset($this->rows[ $rowIndex ]) |
||
182 | ? count($this->rows[ $rowIndex ]) |
||
183 | : 0; |
||
184 | } |
||
185 | $this->rows[ $rowIndex ][ $columnIndex ] = $content; |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | // ------------------------------------------------------------------------ |
||
191 | |||
192 | /** |
||
193 | * Table::setPadding |
||
194 | * |
||
195 | * Set padding for each cell. |
||
196 | * |
||
197 | * @param int $numbers The numbers of padding, the default numbers is 1. |
||
198 | * |
||
199 | * @return static |
||
200 | */ |
||
201 | public function setPadding($numbers = 1) |
||
202 | { |
||
203 | $this->padding = $numbers; |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | // ------------------------------------------------------------------------ |
||
209 | |||
210 | /** |
||
211 | * Table::setLeftMargin |
||
212 | * |
||
213 | * Set left indentation for the table. |
||
214 | * |
||
215 | * @param int $numbers The numbers of left margin, the default numbers is 0. |
||
216 | * |
||
217 | * @return static |
||
218 | */ |
||
219 | public function setLeftMargin($numbers = 0) |
||
220 | { |
||
221 | $this->leftMargin = $numbers; |
||
222 | |||
223 | return $this; |
||
224 | } |
||
225 | |||
226 | // ------------------------------------------------------------------------ |
||
227 | |||
228 | /** |
||
229 | * Table::__toString |
||
230 | * |
||
231 | * Implementation __toString magic method so that when the class is converted to a string |
||
232 | * automatically performs the rendering process. |
||
233 | * |
||
234 | * @return string |
||
235 | */ |
||
236 | public function __toString() |
||
237 | { |
||
238 | return $this->render(); |
||
239 | } |
||
240 | |||
241 | // ------------------------------------------------------------------------ |
||
242 | |||
243 | /** |
||
244 | * Table::render |
||
245 | * |
||
246 | * Render the table. |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | public function render() |
||
251 | { |
||
252 | $this->calculateColumnWidth(); |
||
253 | $output = $this->isShowBorder |
||
254 | ? $this->renderBorder() |
||
255 | : ''; |
||
256 | foreach ($this->rows as $rowIndex => $row) { |
||
257 | foreach ($row as $cellIndex => $cell) { |
||
258 | $output .= $this->renderCell($cellIndex, $row); |
||
259 | } |
||
260 | $output .= "\n"; |
||
261 | if ($rowIndex === self::HEADER_INDEX) { |
||
262 | $output .= $this->renderBorder(); |
||
263 | } |
||
264 | } |
||
265 | $output .= $this->isShowBorder |
||
266 | ? $this->renderBorder() |
||
267 | : ''; |
||
268 | |||
269 | return $output; |
||
270 | } |
||
271 | |||
272 | // ------------------------------------------------------------------------ |
||
273 | |||
274 | /** |
||
275 | * Table::calculateColumnWidth |
||
276 | * |
||
277 | * Calculate maximum width of each column |
||
278 | * |
||
279 | * @return array |
||
280 | */ |
||
281 | private function calculateColumnWidth() |
||
282 | { |
||
283 | foreach ($this->rows as $rowIndex => $row) { |
||
284 | foreach ($row as $columnIndex => $column) { |
||
285 | if ( ! isset($this->columnWidths[ $columnIndex ])) { |
||
286 | $this->columnWidths[ $columnIndex ] = strlen($column); |
||
287 | } else { |
||
288 | if (strlen($column) > $this->columnWidths[ $columnIndex ]) { |
||
289 | $this->columnWidths[ $columnIndex ] = strlen($column); |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | return $this->columnWidths; |
||
296 | } |
||
297 | |||
298 | // ------------------------------------------------------------------------ |
||
299 | |||
300 | /** |
||
301 | * Table::renderBorder |
||
302 | * |
||
303 | * Render the table border. |
||
304 | * |
||
305 | * @return string |
||
306 | */ |
||
307 | private function renderBorder() |
||
308 | { |
||
309 | $output = ''; |
||
310 | $columnCount = count($this->rows[ 0 ]); |
||
311 | for ($col = 0; $col < $columnCount; $col++) { |
||
312 | $output .= $this->renderCell($col); |
||
313 | } |
||
314 | if ($this->isShowBorder) { |
||
315 | $output .= '+'; |
||
316 | } |
||
317 | $output .= "\n"; |
||
318 | |||
319 | return $output; |
||
320 | } |
||
321 | |||
322 | // ------------------------------------------------------------------------ |
||
323 | |||
324 | /** |
||
325 | * Table::renderCell |
||
326 | * |
||
327 | * Render the table cell content. |
||
328 | * |
||
329 | * @param int $index The column index. |
||
330 | * @param array $row The table row. |
||
331 | * |
||
332 | * @return string |
||
333 | */ |
||
334 | private function renderCell($index, $row = null) |
||
335 | { |
||
336 | $cell = $row |
||
337 | ? $row[ $index ] |
||
338 | : '-'; |
||
339 | $width = $this->columnWidths[ $index ]; |
||
340 | $pad = $row |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
341 | ? $width - strlen($cell) |
||
342 | : $width; |
||
343 | $padding = str_repeat( |
||
344 | $row |
||
345 | ? ' ' |
||
346 | : '-', |
||
347 | $this->padding |
||
348 | ); |
||
349 | $output = ''; |
||
350 | if ($index === 0) { |
||
351 | $output .= str_repeat(' ', $this->leftMargin); |
||
352 | } |
||
353 | if ($this->isShowBorder) { |
||
354 | $output .= $row |
||
355 | ? '|' |
||
356 | : '+'; |
||
357 | } |
||
358 | $output .= $padding; # left padding |
||
359 | $output .= str_pad( |
||
360 | $cell, |
||
361 | $width, |
||
362 | $row |
||
363 | ? ' ' |
||
364 | : '-' |
||
365 | ); # cell content |
||
366 | $output .= $padding; # right padding |
||
367 | |||
368 | $row = is_array($row) ? $row : []; |
||
369 | if ($index == count($row) - 1 && $this->isShowBorder) { |
||
370 | $output .= $row |
||
371 | ? '|' |
||
372 | : '+'; |
||
373 | } |
||
374 | |||
375 | return $output; |
||
376 | } |
||
377 | } |