KwRenderer   A
last analyzed

Complexity

Total Complexity 30

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 30
eloc 85
c 0
b 0
f 0
dl 0
loc 164
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A renderFilter() 0 7 5
A getFootFilter() 0 14 4
A getHeadFilter() 0 14 4
A renderPagers() 0 13 4
A renderScript() 0 8 4
A getHeader() 0 16 4
A render() 0 12 1
A getCells() 0 16 3
A __construct() 0 10 1
1
<?php
2
3
namespace kalanis\kw_table\output_kw;
4
5
6
use kalanis\kw_connect\core\ConnectException;
7
use kalanis\kw_forms\Exceptions\RenderException;
8
use kalanis\kw_table\core\Table;
9
use kalanis\kw_table\core\TableException;
10
11
12
/**
13
 * Class KwRenderer
14
 * @package kalanis\kw_table\output_kw
15
 * Render output in html templates from kw_template
16
 */
17
class KwRenderer extends Table\AOutput
18
{
19
    protected Html\TableBase $templateBase;
20
    protected Html\TableCell $templateCell;
21
    protected Html\TableFoot $templateFoot;
22
    protected Html\TableHead $templateHead;
23
    protected Html\TableHeadSorted $templateHeadSorted;
24
    protected Html\TableRow $templateRow;
25
    protected Html\TableScript $templateScript;
26
27
    public function __construct(Table $table)
28
    {
29
        parent::__construct($table);
30
        $this->templateBase = new Html\TableBase();
31
        $this->templateCell = new Html\TableCell();
32
        $this->templateFoot = new Html\TableFoot();
33
        $this->templateHead = new Html\TableHead();
34
        $this->templateHeadSorted = new Html\TableHeadSorted();
35
        $this->templateRow = new Html\TableRow();
36
        $this->templateScript = new Html\TableScript();
37
    }
38
39
    /**
40
     * @throws ConnectException
41
     * @throws RenderException
42
     * @throws TableException
43
     * @return string
44
     */
45
    public function render(): string
46
    {
47
        $this->renderPagers();
48
        $this->renderFilter();
49
        $this->renderScript();
50
        return $this->templateBase->setData(
51
            $this->getCells(),
52
            $this->getHeader(),
53
            $this->getHeadFilter(),
54
            $this->getFootFilter(),
55
            $this->table->getClassesInString()
56
        )->render();
57
    }
58
59
    protected function renderPagers(): void
60
    {
61
        try {
62
            $paging = $this->table->getPager();
63
        } catch (TableException $ex) {
64
            return;
65
        }
66
67
        if ($this->table->showPagerOnHead()) {
68
            $this->templateBase->addPagerHead($paging->render());
69
        }
70
        if ($this->table->showPagerOnFoot()) {
71
            $this->templateBase->addPagerFoot($paging->render());
72
        }
73
    }
74
75
    /**
76
     * @throws RenderException
77
     */
78
    protected function renderFilter(): void
79
    {
80
        $headerFilter = $this->table->getHeaderFilter();
81
        $footerFilter = $this->table->getFooterFilter();
82
        $this->templateBase->addFilter(
83
            $headerFilter ? $headerFilter->renderStart() : ($footerFilter ? $footerFilter->renderStart() : ''),
84
            $headerFilter ? $headerFilter->renderEnd() : ($footerFilter ? $footerFilter->renderEnd() : '')
85
        );
86
    }
87
88
    protected function renderScript(): void
89
    {
90
        $headerFilter = $this->table->getHeaderFilter();
91
        $footerFilter = $this->table->getFooterFilter();
92
        $formName = $this->table->getFormName();
93
        if ($formName && ($headerFilter || $footerFilter)) {
94
            $this->templateBase->addScript(
95
                $this->templateScript->reset()->setData($formName)->render()
96
            );
97
        }
98
    }
99
100
    /**
101
     * @throws ConnectException
102
     * @throws TableException
103
     * @return string
104
     */
105
    protected function getCells(): string
106
    {
107
        $cell = [];
108
        foreach ($this->table->getTableData() as $row) {
109
            /** @var Table\Internal\Row $row */
110
            $this->templateRow->reset()->setData($row->getCellStyle($row->getSource()));
0 ignored issues
show
Bug introduced by
It seems like $row->getSource() can also be of type null; however, parameter $source of kalanis\kw_table\core\Table\AStyle::getCellStyle() does only seem to accept kalanis\kw_connect\core\Interfaces\IRow, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
            $this->templateRow->reset()->setData($row->getCellStyle(/** @scrutinizer ignore-type */ $row->getSource()));
Loading history...
111
            foreach ($row as $column) {
112
                /** @var Table\Columns\AColumn $column */
113
                $this->templateRow->addCell($this->templateCell->reset()->setData(
114
                    $column->translate($row->getSource()),
0 ignored issues
show
Bug introduced by
It seems like $row->getSource() can also be of type null; however, parameter $source of kalanis\kw_table\core\Ta...ns\AColumn::translate() does only seem to accept kalanis\kw_connect\core\Interfaces\IRow, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
                    $column->translate(/** @scrutinizer ignore-type */ $row->getSource()),
Loading history...
115
                    $column->getCellStyle($row->getSource())
116
                )->render());
117
            }
118
            $cell[] = $this->templateRow->render();
119
        }
120
        return implode('', $cell);
121
    }
122
123
    protected function getHeader(): string
124
    {
125
        $order = $this->table->getOrderOrNull();
126
        $this->templateRow->reset()->setData();
127
        foreach ($this->table->getColumns() as $column) {
128
            if ($order && $order->isInOrder($column)) {
129
                $this->templateRow->addCell($this->templateHeadSorted->reset()->setData(
130
                    $order->getHeaderText($column), $order->getHref($column)
131
                )->render());
132
            } else {
133
                $this->templateRow->addCell($this->templateHead->reset()->setData(
134
                    $column->getHeaderText()
135
                )->render());
136
            }
137
        }
138
        return $this->templateRow->render();
139
    }
140
141
    /**
142
     * @throws RenderException
143
     * @throws TableException
144
     * @return string
145
     */
146
    protected function getHeadFilter(): string
147
    {
148
        $headerFilter = $this->table->getHeaderFilter();
149
        if (!$headerFilter) {
150
            return '';
151
        }
152
153
        $this->templateRow->reset()->setData();
154
        foreach ($this->table->getColumns() as $column) {
155
            $this->templateRow->addCell($this->templateHead->reset()->setData(
156
                $column->hasHeaderFilterField() ? $headerFilter->renderHeaderInput($column) : ''
157
            )->render());
158
        }
159
        return $this->templateRow->render();
160
    }
161
162
    /**
163
     * @throws RenderException
164
     * @throws TableException
165
     * @return string
166
     */
167
    protected function getFootFilter(): string
168
    {
169
        $footerFilter = $this->table->getFooterFilter();
170
        if (!$footerFilter) {
171
            return '';
172
        }
173
174
        $this->templateRow->reset()->setData();
175
        foreach ($this->table->getColumns() as $column) {
176
            $this->templateRow->addCell($this->templateCell->reset()->setData(
177
                $column->hasFooterFilterField() ? $footerFilter->renderFooterInput($column) : ''
178
            )->render());
179
        }
180
        return $this->templateRow->render();
181
    }
182
}
183