TableRenderer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 17
c 3
b 0
f 0
dl 0
loc 82
ccs 21
cts 21
cp 1
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _renderRow() 0 11 2
A renderBody() 0 9 2
A renderHeaders() 0 12 2
A open() 0 3 1
A close() 0 3 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
use hamburgscleanest\DataTables\Models\Column\Column;
6
use hamburgscleanest\DataTables\Models\Header;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Support\Collection;
9
10
/**
11
 * Class TableRenderer
12
 * @package hamburgscleanest\DataTables\Helpers
13
 */
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
86
    {
87 46
        $html = '<tr>';
88
        /** @var Column $column */
89 46
        foreach ($columns as $column)
90
        {
91 44
            $html .= '<td>' . $column->getFormattedValue($rowModel) . '</td>';
92
        }
93 46
        $html .= '</tr>';
94
95 46
        return $html;
96
    }
97
}