AbstractHtmlElement::getId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace WebTheory\Html;
4
5
use WebTheory\Html\Attributes\Classlist;
6
use WebTheory\Html\Attributes\Style;
7
use WebTheory\Html\Contracts\HtmlInterface;
8
use WebTheory\Html\Traits\ElementConstructorTrait;
9
10
abstract class AbstractHtmlElement implements HtmlInterface
11
{
12
    use ElementConstructorTrait;
13
14
    public string $id = '';
15
16
    protected Classlist $classlist;
17
18
    protected Style $styles;
19
20
    protected array $attributes = [];
21
22
    protected string $charset = 'UTF-8';
23
24
    public function __construct()
25
    {
26
        $this->classlist = new Classlist();
27
        $this->styles = new Style();
28
    }
29
30
    public function getCharset(): string
31
    {
32
        return $this->charset;
33
    }
34
35
    /**
36
     * @return $this
37
     */
38
    public function setCharset(string $charset): AbstractHtmlElement
39
    {
40
        $this->charset = $charset;
41
42
        return $this;
43
    }
44
45
    public function getId(): string
46
    {
47
        return $this->id;
48
    }
49
50
    /**
51
     * @return $this
52
     */
53
    public function setId(string $id): AbstractHtmlElement
54
    {
55
        $this->id = $id;
56
57
        return $this;
58
    }
59
60
    public function getClasslist(): Classlist
61
    {
62
        return $this->classlist;
63
    }
64
65
    /**
66
     * @return $this
67
     */
68
    public function setClasslist(array $classlist): AbstractHtmlElement
69
    {
70
        $this->classlist->set($classlist);
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return $this
77
     */
78
    public function addClass(string $class): AbstractHtmlElement
79
    {
80
        $this->classlist->add($class);
81
82
        return $this;
83
    }
84
85
    /**
86
     * @return $this
87
     */
88
    public function removeClass(string $class): AbstractHtmlElement
89
    {
90
        $this->classlist->remove($class);
91
92
        return $this;
93
    }
94
95
    public function getStyles(): Style
96
    {
97
        return $this->styles;
98
    }
99
100
    /**
101
     * @return $this
102
     */
103
    public function setStyles(array $styles): AbstractHtmlElement
104
    {
105
        foreach ($styles as $property => $value) {
106
            $this->addStyle($property, $value);
107
        }
108
109
        return $this;
110
    }
111
112
    /**
113
     * @return $this
114
     */
115
    public function addStyle(string $property, string $value): AbstractHtmlElement
116
    {
117
        $this->styles->set($property, $value);
118
119
        return $this;
120
    }
121
122
    /**
123
     * @return $this
124
     */
125
    public function removeStyle(string $style): AbstractHtmlElement
126
    {
127
        $this->styles->remove($style);
128
129
        return $this;
130
    }
131
132
    public function getAttributes(): array
133
    {
134
        return $this->attributes;
135
    }
136
137
    /**
138
     * @return $this
139
     */
140
    public function setAttributes(array $attributes): AbstractHtmlElement
141
    {
142
        foreach ($attributes as $key => $value) {
143
            $this->addAttribute($key, $value);
144
        }
145
146
        return $this;
147
    }
148
149
    /**
150
     * @return $this
151
     */
152
    public function addAttribute($attribute, $value): AbstractHtmlElement
153
    {
154
        $this->attributes[$attribute] = $value;
155
156
        return $this;
157
    }
158
159
    /**
160
     * @return $this
161
     */
162
    protected function resolveAttributes(): AbstractHtmlElement
163
    {
164
        return $this
165
            ->addAttribute('id', $this->id)
166
            ->addAttribute('class', $this->classlist->parse())
167
            ->addAttribute('style', $this->styles->parse());
168
    }
169
170
    public function toHtml(): string
171
    {
172
        return $this->resolveAttributes()->renderHtmlMarkup();
173
    }
174
175
    final public function __toString(): string
176
    {
177
        return $this->toHtml();
178
    }
179
180
    abstract protected function renderHtmlMarkup(): string;
181
}
182