Completed
Push — master ( 895c5c...49cc4e )
by Timo
11:39
created

TableRenderer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Helpers;
4
5
use hamburgscleanest\DataTables\Models\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 33
    public function open(? string $classes = null) : string
23
    {
24 33
        return '<table class="' . ($classes ?? 'table') . '">';
25
    }
26
27
    /**
28
     * Closes the table.
29
     *
30
     * @return string
31
     */
32 33
    public function close() : string
33
    {
34 33
        return '</table>';
35
    }
36
37
    /**
38
     * Renders the column headers.
39
     *
40
     * @param array $headers
41
     * @param array $formatters
42
     * @return string
43
     */
44 33
    public function renderHeaders(array $headers, array $formatters = []) : string
45
    {
46 33
        $html = '<tr>';
47
48
        /** @var Header $header */
49 33
        foreach ($headers as $header)
50
        {
51 33
            $html .= $header->formatArray($formatters)->print();
52
        }
53 33
        $html .= '</tr>';
54
55 33
        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 33
    public function renderBody(Collection $data, array $columns = []) : string
67
    {
68 33
        $html = '';
69 33
        foreach ($data as $row)
70
        {
71 33
            $html .= $this->_renderRow($row, $columns);
72
        }
73
74 33
        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 33
    private function _renderRow(Model $rowModel, array $columns) : string
86
    {
87 33
        $html = '<tr>';
88
        /** @var Column $column */
89 33
        foreach ($columns as $column)
90
        {
91 33
            $html .= '<td>' . $column->getFormattedValue($rowModel) . '</td>';
92
        }
93 33
        $html .= '</tr>';
94
95 33
        return $html;
96
    }
97
}
98