JsonRenderer::getSorters()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.9
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4
1
<?php
2
3
namespace kalanis\kw_table\output_json;
4
5
6
use kalanis\kw_connect\core\ConnectException;
7
use kalanis\kw_paging\Positions;
8
use kalanis\kw_table\core\Interfaces\Table\IFilterMulti;
9
use kalanis\kw_table\core\Table;
10
use kalanis\kw_table\core\TableException;
11
12
13
/**
14
 * Class JsonRenderer
15
 * @package kalanis\kw_table\output_json
16
 * Render output in json format
17
 */
18
class JsonRenderer extends Table\AOutput
19
{
20
    protected ?Positions $positions = null;
21
22 7
    public function __construct(Table $table)
23
    {
24 7
        parent::__construct($table);
25
        try {
26 7
            $this->positions = new Positions($table->getPager()->getPager());
27 5
        } catch (TableException $ex) {
28
            // nothing to do
29
        }
30 7
    }
31
32
    /**
33
     * @throws ConnectException
34
     * @return string
35
     */
36 7
    public function render(): string
37
    {
38 7
        return strval(json_encode($this->renderData()));
39
    }
40
41
    /**
42
     * @throws ConnectException
43
     * @return array<string, array<string|int, string|int|float|bool|array<string|int, string|int|float|bool|null>|null>>
44
     */
45 7
    public function renderData(): array
46
    {
47
        return [
48 7
            'header' => $this->getHeaders(),
49 7
            'sorted' => $this->getSorters(),
50 7
            'filtered' => $this->getHeaderFilters(),
51 7
            'body' => $this->getCells(),
52 7
            'pager' => $this->getPager(),
53
        ];
54
    }
55
56
    /**
57
     * @return array<string|int, string>
58
     */
59 7
    protected function getHeaders(): array
60
    {
61 7
        $line = [];
62 7
        foreach ($this->table->getColumns() as $column) {
63 7
            $line[$column->getSourceName()] = $column->getHeaderText();
64
        }
65 7
        return $line;
66
    }
67
68
    /**
69
     * @return array<string|int, array<string, string|int>>
70
     */
71 7
    protected function getSorters(): array
72
    {
73
        try {
74 7
            $order = $this->table->getOrder();
75 2
        } catch (TableException $ex) {
76 2
            return [];
77
        }
78
79 5
        $line = [];
80 5
        foreach ($this->table->getColumns() as $column) {
81 5
            if ($order->isInOrder($column)) {
82 4
                $line[$column->getSourceName()] = [
83 4
                    'is_active' => intval($order->isActive($column)),
84 4
                    'direction' => $order->getActiveDirection($column),
85
                ];
86
            }
87
        }
88 5
        return $line;
89
    }
90
91
    /**
92
     * @return array<string|int, string|int|float|bool|null>
93
     */
94 7
    protected function getHeaderFilters(): array
95
    {
96 7
        $headerFilter = $this->table->getHeaderFilter();
97 7
        if (!$headerFilter) {
98 5
            return [];
99
        }
100
101 2
        $form = $this->table->getHeaderFilter()->getConnector();
102 2
        $line = [];
103 2
        foreach ($this->table->getColumns() as $column) {
104 2
            if ($column->hasHeaderFilterField()) {
105 2
                if ($column->getHeaderFilterField() instanceof IFilterMulti) {
106
                    // skip for now, there is no form with that name
107
                } else {
108 2
                    $line[$column->getSourceName()] = $form->getValue($column->getFilterName());
109
                }
110
            }
111
        }
112 2
        return $line;
113
    }
114
115
    /**
116
     * @throws ConnectException
117
     * @return array<int, array<string|int, string|int|float|bool|null>>
118
     */
119 7
    protected function getCells(): array
120
    {
121 7
        $cell = [];
122 7
        foreach ($this->table->getTableData() as $row) {
123
            /** @var Table\Internal\Row $row */
124 7
            $line = [];
125 7
            foreach ($row as $column) {
126
                /** @var Table\Columns\AColumn $column */
127 7
                $line[$column->getSourceName()] = $column->getValue($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...mns\AColumn::getValue() 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

127
                $line[$column->getSourceName()] = $column->getValue(/** @scrutinizer ignore-type */ $row->getSource());
Loading history...
128
            }
129 7
            $cell[] = $line;
130
        }
131 7
        return $cell;
132
    }
133
134
    /**
135
     * @return array<string, array<string, int>>
136
     */
137 7
    protected function getPager(): array
138
    {
139 7
        if (empty($this->positions)) {
140 5
            return [];
141
        }
142 2
        $pager = $this->positions->getPager();
143
144 2
        $pages = [];
145 2
        $pages['first'] = $this->positions->getFirstPage();
146 2
        $pages['prev'] = $this->positions->prevPageExists() ? $this->positions->getPrevPage() : $this->positions->getFirstPage() ;
147 2
        $pages['actual'] = $pager->getActualPage();
148 2
        $pages['next'] = $this->positions->nextPageExists() ? $this->positions->getNextPage() : $this->positions->getLastPage() ;
149 2
        $pages['last'] = $this->positions->getLastPage();
150
151 2
        $results = [];
152 2
        $results['from'] = $pager->getOffset() + 1;
153 2
        $results['to'] = min($pager->getOffset() + $pager->getLimit(), $pager->getMaxResults());
154 2
        $results['total'] = $pager->getMaxResults();
155
156
        return [
157 2
            'positions' => $pages,
158 2
            'results' => $results,
159
        ];
160
    }
161
}
162