Grid   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 68
c 1
b 0
f 0
dl 0
loc 217
rs 10
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 3
A getActions() 0 11 2
A getFilters() 0 11 2
A getExtendedNodes() 0 14 4
A getPageSize() 0 7 2
A setTotalCount() 0 9 2
A getSortConditions() 0 3 1
A setEntities() 0 5 1
A __toString() 0 10 1
A getWhereConditions() 0 7 2
A setTemplate() 0 5 1
A getSqlParams() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid;
6
7
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
8
use AbterPhp\Framework\Grid\Component\Actions;
9
use AbterPhp\Framework\Grid\Component\Filters;
10
use AbterPhp\Framework\Grid\Pagination\IPagination;
11
use AbterPhp\Framework\Grid\Table\ITable;
12
use AbterPhp\Framework\Html\Attribute;
13
use AbterPhp\Framework\Html\Helper\Tag as TagHelper;
14
use AbterPhp\Framework\Html\INode;
15
use AbterPhp\Framework\Html\ITemplater;
16
use AbterPhp\Framework\Html\Tag;
17
18
class Grid extends Tag implements IGrid, ITemplater
19
{
20
    /**
21
     * %1$s - filter
22
     * %2$s - actions
23
     * %3$s - table
24
     * %4$s - pagination
25
     */
26
    protected const DEFAULT_TEMPLATE = '%1$s%4$s%2$s%3$s%4$s';
27
28
    protected const TAG_GRID    = 'div';
29
    protected const TAG_FILTER  = 'div';
30
    protected const TAG_ACTIONS = 'div';
31
32
    protected const ATTRIBUTE_GRID_CLASS    = 'grid';
33
    protected const ATTRIBUTE_FILTER_CLASS  = 'grid-filters';
34
    protected const ATTRIBUTE_ACTIONS_CLASS = 'grid-actions';
35
36
    protected string $containerClass = '';
37
38
    protected ITable $table;
39
40
    protected ?IPagination $pagination = null;
41
42
    protected ?Filters $filters = null;
43
44
    protected ?Actions $actions = null;
45
46
    protected string $template = self::DEFAULT_TEMPLATE;
47
48
    /**
49
     * Grid constructor.
50
     *
51
     * @param ITable                       $table
52
     * @param IPagination|null             $pagination
53
     * @param Filters|null                 $filters
54
     * @param Actions|null                 $actions
55
     * @param string[]                     $intents
56
     * @param array<string,Attribute>|null $attributes
57
     */
58
    public function __construct(
59
        ITable $table,
60
        IPagination $pagination = null,
61
        Filters $filters = null,
62
        Actions $actions = null,
63
        array $intents = [],
64
        ?array $attributes = null
65
    ) {
66
        $this->table      = $table;
67
        $this->pagination = $pagination;
68
69
        parent::__construct(null, $intents, $attributes, static::TAG_GRID);
70
71
        $this->appendToClass(static::ATTRIBUTE_GRID_CLASS);
72
73
        if ($actions) {
74
            $this->actions = $actions;
75
            $this->actions->appendToClass(static::ATTRIBUTE_ACTIONS_CLASS);
76
        }
77
78
        if ($filters) {
79
            $this->filters = $filters;
80
            $this->filters->appendToClass(static::ATTRIBUTE_FILTER_CLASS);
81
        }
82
    }
83
84
    /**
85
     * @return Filters
86
     */
87
    public function getFilters(): Filters
88
    {
89
        if ($this->filters instanceof Filters) {
90
            return $this->filters;
91
        }
92
93
        $filters = new Filters();
94
95
        $this->filters = $filters;
96
97
        return $filters;
98
    }
99
100
    /**
101
     * @return Actions
102
     */
103
    public function getActions(): Actions
104
    {
105
        if ($this->actions instanceof Actions) {
106
            return $this->actions;
107
        }
108
109
        $actions = new Actions();
110
111
        $this->actions = $actions;
112
113
        return $actions;
114
    }
115
116
    /**
117
     * @return int
118
     */
119
    public function getPageSize(): int
120
    {
121
        if (!$this->pagination) {
122
            throw new \LogicException();
123
        }
124
125
        return $this->pagination->getPageSize();
126
    }
127
128
    /**
129
     * @return array
130
     */
131
    public function getSortConditions(): array
132
    {
133
        return $this->table->getSortConditions();
134
    }
135
136
    /**
137
     * @return array
138
     */
139
    public function getWhereConditions(): array
140
    {
141
        if (!$this->filters) {
142
            throw new \LogicException();
143
        }
144
145
        return $this->filters->getWhereConditions();
146
    }
147
148
    /**
149
     * @return array<string,string>
150
     */
151
    public function getSqlParams(): array
152
    {
153
        if (!$this->filters) {
154
            throw new \LogicException();
155
        }
156
157
        $tableParams   = $this->table->getSqlParams();
158
        $filtersParams = $this->filters->getSqlParams();
159
160
        return array_merge($tableParams, $filtersParams);
161
    }
162
163
    /**
164
     * @param int $totalCount
165
     *
166
     * @return $this
167
     */
168
    public function setTotalCount(int $totalCount): IGrid
169
    {
170
        if (!$this->pagination) {
171
            throw new \LogicException();
172
        }
173
174
        $this->pagination->setTotalCount($totalCount);
175
176
        return $this;
177
    }
178
179
    /**
180
     * @param IStringerEntity[] $entities
181
     *
182
     * @return IGrid
183
     */
184
    public function setEntities(array $entities): IGrid
185
    {
186
        $this->table->setEntities($entities);
187
188
        return $this;
189
    }
190
191
    /**
192
     * @param string $template
193
     *
194
     * @return $this
195
     */
196
    public function setTemplate(string $template): INode
197
    {
198
        $this->template = $template;
199
200
        return $this;
201
    }
202
203
    /**
204
     * @return INode[]
205
     */
206
    public function getExtendedNodes(): array
207
    {
208
        $extendedNodes = [];
209
        if ($this->filters) {
210
            $extendedNodes[] = $this->filters;
211
        }
212
        if ($this->pagination) {
213
            $extendedNodes[] = $this->pagination;
214
        }
215
        if ($this->table) {
216
            $extendedNodes[] = $this->table;
217
        }
218
219
        return array_merge($extendedNodes, $this->getNodes());
220
    }
221
222
    /**
223
     * @return string
224
     */
225
    public function __toString(): string
226
    {
227
        $filters    = (string)$this->getFilters();
228
        $actions    = (string)$this->getActions();
229
        $table      = (string)$this->table;
230
        $pagination = (string)$this->pagination;
231
232
        $content = sprintf($this->template, $filters, $actions, $table, $pagination);
233
234
        return TagHelper::toString($this->tag, $content, $this->attributes);
235
    }
236
}
237