Total Complexity | 8 |
Total Lines | 82 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
14 | class TableRenderer { |
||
15 | |||
16 | /** |
||
17 | * Starts the table. |
||
18 | * |
||
19 | * @param null|string $classes |
||
20 | * @return string |
||
21 | */ |
||
22 | 46 | public function open(? string $classes = null) : string |
|
23 | { |
||
24 | 46 | return '<table class="' . ($classes ?? 'table') . '">'; |
|
25 | } |
||
26 | |||
27 | /** |
||
28 | * Closes the table. |
||
29 | * |
||
30 | * @return string |
||
31 | */ |
||
32 | 46 | public function close() : string |
|
33 | { |
||
34 | 46 | return '</table>'; |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * Renders the column headers. |
||
39 | * |
||
40 | * @param array $headers |
||
41 | * @param array $formatters |
||
42 | * @return string |
||
43 | */ |
||
44 | 46 | public function renderHeaders(array $headers, array $formatters = []) : string |
|
45 | { |
||
46 | 46 | $html = '<tr>'; |
|
47 | |||
48 | /** @var Header $header */ |
||
49 | 46 | foreach ($headers as $header) |
|
50 | { |
||
51 | 44 | $html .= $header->formatArray($formatters)->print(); |
|
52 | } |
||
53 | 46 | $html .= '</tr>'; |
|
54 | |||
55 | 46 | return $html; |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * Displays the table body. |
||
60 | * |
||
61 | * @param Collection $data |
||
62 | * |
||
63 | * @param array $columns |
||
64 | * @return string |
||
65 | */ |
||
66 | 46 | public function renderBody(Collection $data, array $columns = []) : string |
|
67 | { |
||
68 | 46 | $html = ''; |
|
69 | 46 | foreach ($data as $row) |
|
70 | { |
||
71 | 46 | $html .= $this->_renderRow($row, $columns); |
|
72 | } |
||
73 | |||
74 | 46 | return $html; |
|
75 | } |
||
76 | |||
77 | /** |
||
78 | * Displays a single row. |
||
79 | * |
||
80 | * @param Model $rowModel |
||
81 | * |
||
82 | * @param array $columns |
||
83 | * @return string |
||
84 | */ |
||
85 | 46 | private function _renderRow(Model $rowModel, array $columns) : string |
|
96 | } |
||
97 | } |