Passed
Push — main ( 3c0f49...2c259b )
by Peter
04:10
created

Grid::getActions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Domain\Entities\IStringerEntity;
9
use AbterPhp\Framework\Grid\Component\Actions;
10
use AbterPhp\Framework\Grid\Component\Filters;
11
use AbterPhp\Framework\Grid\Pagination\IPagination;
12
use AbterPhp\Framework\Grid\Table\ITable;
13
use AbterPhp\Framework\Html\Component;
14
use AbterPhp\Framework\Html\Helper\StringHelper;
15
use AbterPhp\Framework\Html\INode;
16
use AbterPhp\Framework\Html\ITemplater;
17
18
class Grid extends Component 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            $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 = []
65
    ) {
66
        $this->table      = $table;
67
        $this->pagination = $pagination;
68
69
        parent::__construct(null, $intents, $attributes, static::TAG_GRID);
70
71
        $this->appendToAttribute(Html5::ATTR_CLASS, static::ATTRIBUTE_GRID_CLASS);
72
73
        if ($actions) {
74
            $this->actions = $actions;
75
            $this->actions->appendToAttribute(Html5::ATTR_CLASS, static::ATTRIBUTE_ACTIONS_CLASS);
76
        }
77
78
        if ($filters) {
79
            $this->filters = $filters;
80
            $this->filters->appendToAttribute(Html5::ATTR_CLASS, static::ATTRIBUTE_FILTER_CLASS);
81
        }
82
    }
83
84
    /**
85
     * @return Filters
86
     */
87
    public function getFilters(): Filters
88
    {
89
        if ($this->filters === null) {
90
            $this->filters = new Filters();
91
        }
92
93
        return $this->filters;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->filters could return the type null which is incompatible with the type-hinted return AbterPhp\Framework\Grid\Component\Filters. Consider adding an additional type-check to rule them out.
Loading history...
94
    }
95
96
    /**
97
     * @return Actions
98
     */
99
    public function getActions(): Actions
100
    {
101
        if ($this->actions === null) {
102
            $this->actions = new Actions();
103
        }
104
105
        return $this->actions;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->actions could return the type null which is incompatible with the type-hinted return AbterPhp\Framework\Grid\Component\Actions. Consider adding an additional type-check to rule them out.
Loading history...
106
    }
107
108
    /**
109
     * @return int
110
     */
111
    public function getPageSize(): int
112
    {
113
        if (!$this->pagination) {
114
            throw new \LogicException();
115
        }
116
117
        $pageSize = $this->pagination->getPageSize();
118
119
        return $pageSize;
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function getSortConditions(): array
126
    {
127
        return $this->table->getSortConditions();
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function getWhereConditions(): array
134
    {
135
        if (!$this->filters) {
136
            throw new \LogicException();
137
        }
138
139
        return $this->filters->getWhereConditions();
140
    }
141
142
    /**
143
     * @return array
144
     */
145
    public function getSqlParams(): array
146
    {
147
        if (!$this->filters) {
148
            throw new \LogicException();
149
        }
150
151
        $tableParams   = $this->table->getSqlParams();
152
        $filtersParams = $this->filters->getSqlParams();
153
154
        return array_merge($tableParams, $filtersParams);
155
    }
156
157
    /**
158
     * @param int $totalCount
159
     *
160
     * @return $this
161
     */
162
    public function setTotalCount(int $totalCount): IGrid
163
    {
164
        if (!$this->pagination) {
165
            throw new \LogicException();
166
        }
167
168
        $this->pagination->setTotalCount($totalCount);
169
170
        return $this;
171
    }
172
173
    /**
174
     * @param IStringerEntity[] $entities
175
     *
176
     * @return IGrid
177
     */
178
    public function setEntities(array $entities): IGrid
179
    {
180
        $this->table->setEntities($entities);
181
182
        return $this;
183
    }
184
185
    /**
186
     * @param string $template
187
     *
188
     * @return $this
189
     */
190
    public function setTemplate(string $template): INode
191
    {
192
        $this->template = $template;
193
194
        return $this;
195
    }
196
197
    /**
198
     * @return INode[]
199
     */
200
    public function getExtendedNodes(): array
201
    {
202
        $extendedNodes = [];
203
        if ($this->filters) {
204
            $extendedNodes[] = $this->filters;
205
        }
206
        if ($this->pagination) {
207
            $extendedNodes[] = $this->pagination;
208
        }
209
        if ($this->table) {
210
            $extendedNodes[] = $this->table;
211
        }
212
        $extendedNodes = array_merge($extendedNodes, $this->getNodes());
213
214
        return $extendedNodes;
215
    }
216
217
    /**
218
     * @return string
219
     */
220
    public function __toString(): string
221
    {
222
        $filters    = (string)$this->getFilters();
223
        $actions    = (string)$this->getActions();
224
        $table      = (string)$this->table;
225
        $pagination = (string)$this->pagination;
226
227
        $content = sprintf($this->template, $filters, $actions, $table, $pagination);
228
229
        $content = StringHelper::wrapInTag($content, $this->tag, $this->attributes);
230
231
        return $content;
232
    }
233
}
234