Header   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
dl 0
loc 113
rs 10
c 1
b 0
f 0
wmc 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseUrl() 0 13 4
A setParams() 0 13 4
A getQueryParams() 0 3 1
A getSortedUrl() 0 19 5
A __construct() 0 3 1
A getSortConditions() 0 14 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Grid\Component;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Grid\Cell\Sortable;
9
use AbterPhp\Framework\Grid\Row\IRow;
10
use AbterPhp\Framework\Html\Attribute;
11
use AbterPhp\Framework\Html\Tag;
12
13
class Header extends Tag
14
{
15
    protected const DEFAULT_TAG  = Html5::TAG_THEAD;
16
    protected const CONTENT_TYPE = IRow::class;
17
18
    /** @var IRow[] */
19
    protected array $content = [];
20
21
    protected string $nodeClass = IRow::class;
22
23
    /**
24
     * Header constructor.
25
     *
26
     * @param IRow[]|null                  $content
27
     * @param string[]                     $intents
28
     * @param array<string,Attribute>|null $attributes
29
     */
30
    public function __construct(?array $content = null, array $intents = [], ?array $attributes = null)
31
    {
32
        parent::__construct($content, $intents, $attributes);
33
    }
34
35
    /**
36
     * @param string $baseUrl
37
     *
38
     * @return $this
39
     */
40
    public function setBaseUrl(string $baseUrl): Header
41
    {
42
        foreach ($this->content as $row) {
43
            foreach ($row->getCells() as $cell) {
44
                if (!($cell instanceof Sortable)) {
45
                    continue;
46
                }
47
48
                $cell->setBaseUrl($baseUrl);
49
            }
50
        }
51
52
        return $this;
53
    }
54
55
    /**
56
     * @param array $params
57
     *
58
     * @return $this
59
     */
60
    public function setParams(array $params): Header
61
    {
62
        foreach ($this->content as $row) {
63
            foreach ($row->getCells() as $cell) {
64
                if (!($cell instanceof Sortable)) {
65
                    continue;
66
                }
67
68
                $cell->setParams($params);
69
            }
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $baseUrl
77
     *
78
     * @return string
79
     */
80
    public function getSortedUrl(string $baseUrl): string
81
    {
82
        $params = [];
83
        foreach ($this->content as $row) {
84
            foreach ($row->getCells() as $cell) {
85
                if (!($cell instanceof Sortable)) {
86
                    continue;
87
                }
88
89
                $queryParam = $cell->getQueryParam();
90
                if (!$queryParam) {
91
                    continue;
92
                }
93
94
                $params[] = $queryParam;
95
            }
96
        }
97
98
        return $baseUrl . implode('', $params);
99
    }
100
101
    /**
102
     * @return array<string,string>
103
     */
104
    public function getSortConditions(): array
105
    {
106
        $conditions = [];
107
        foreach ($this->content as $row) {
108
            foreach ($row->getCells() as $cell) {
109
                if (!($cell instanceof Sortable)) {
110
                    continue;
111
                }
112
113
                $conditions = array_merge($conditions, $cell->getSortConditions());
114
            }
115
        }
116
117
        return $conditions;
118
    }
119
120
    /**
121
     * @return array<string,string>
122
     */
123
    public function getQueryParams(): array
124
    {
125
        return [];
126
    }
127
}
128