Completed
Pull Request — master (#8)
by Timo
03:50 queued 54s
created

TableRenderer::_getMutatedAttributes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 2
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\Http\Request;
9
use Illuminate\Support\Collection;
10
11
/**
12
 * Class TableRenderer
13
 * @package hamburgscleanest\DataTables\Helpers
14
 */
15
class TableRenderer {
16
17
    /** @var Request */
18
    private $_request;
19
20
    /**
21
     * TableRenderer constructor.
22
     * @param Request $request
23
     */
24 31
    public function __construct(Request $request)
25
    {
26 31
        $this->_request = $request;
27 31
    }
28
29
    /**
30
     * Starts the table.
31
     *
32
     * @param null|string $classes
33
     * @return string
34
     */
35 31
    public function open(? string $classes = null) : string
36
    {
37 31
        return '<table class="' . ($classes ?? 'table') . '">';
38
    }
39
40
    /**
41
     * Closes the table.
42
     *
43
     * @return string
44
     */
45 31
    public function close() : string
46
    {
47 31
        return '</table>';
48
    }
49
50
    /**
51
     * Renders the column headers.
52
     *
53
     * @param array $headers
54
     * @param array $formatters
55
     * @return string
56
     */
57 31
    public function renderHeaders(array $headers, array $formatters = []) : string
58
    {
59 31
        $html = '<tr>';
60
61
        /** @var Header $header */
62 31
        foreach ($headers as $header)
63
        {
64 31
            $html .= $header->formatArray($formatters, $this->_request)->print();
65
        }
66 31
        $html .= '</tr>';
67
68 31
        return $html;
69
    }
70
71
    /**
72
     * Displays the table body.
73
     *
74
     * @param Collection $data
75
     *
76
     * @param array $columns
77
     * @return string
78
     */
79 31
    public function renderBody(Collection $data, array $columns = []) : string
80
    {
81 31
        $html = '';
82 31
        foreach ($data as $row)
83
        {
84 31
            $html .= $this->_renderRow($row, $columns);
85
        }
86
87 31
        return $html;
88
    }
89
90
    /**
91
     * Displays a single row.
92
     *
93
     * @param Model $rowModel
94
     *
95
     * @param array $columns
96
     * @return string
97
     */
98 31
    private function _renderRow(Model $rowModel, array $columns) : string
99
    {
100 31
        $html = '<tr>';
101
        /** @var Column $column */
102 31
        foreach ($columns as $column)
103
        {
104 31
            $html .= '<td>' . $column->getFormattedValue($rowModel) . '</td>';
105
        }
106 31
        $html .= '</tr>';
107
108 31
        return $html;
109
    }
110
}
111