Passed
Push — main ( 7f60ea...60225c )
by Thierry
08:29
created

Element::addComment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Lagdo\UiBuilder\Builder\Html;
4
5
use AvpLab\Element\Comment;
6
use AvpLab\Element\Element as Block;
7
use AvpLab\Element\Text;
8
use Lagdo\UiBuilder\Element\ElementInterface;
9
use Closure;
10
11
use function is_array;
12
use function is_string;
13
use function trim;
14
15
class Element extends AbstractElement implements ElementInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    public $attributes = [];
21
22
    /**
23
     * @var array
24
     */
25
    public $classes = [[]]; // In the first entry are the element base classes.
26
27
    /**
28
     * @var array
29
     */
30
    public $escapes = [];
31
32
    /**
33
     * @var array<Block>
34
     */
35
    public $blocks = [];
36
37
    /**
38
     * @var array<AbstractElement>
39
     */
40
    public $children = [];
41
42
    /**
43
     * @var array
44
     */
45
    public $wrappers = [];
46
47
    /**
48
     * The constructor
49
     *
50
     * @param HtmlBuilder $builder
51
     * @param string $name
52
     * @param array $arguments
53
     */
54
    public function __construct(public HtmlBuilder $builder,
55
        public string $name, array $arguments = [])
56
    {
57
        // Resolve arguments
58
        foreach ($arguments as $argument) {
59
            if (is_string($argument)) {
60
                $this->blocks[] = new Text($argument, false);
61
            } elseif (is_array($argument)) {
62
                $this->setAttributes($argument);
63
            }
64
        }
65
66
        $this->onCreate();
67
68
        // The arguments can also contain the list of child elements.
69
        $this->children(...$arguments);
70
    }
71
72
    /**
73
     * @return static
74
     */
75
    public function children(...$arguments): static
76
    {
77
        $this->children = $arguments;
78
        return $this;
79
    }
80
81
    /**
82
     * @param string $method
83
     * @param array $arguments
84
     *
85
     * @return static
86
     */
87
    public function __call(string $method, array $arguments): static
88
    {
89
        $this->builder->make($method, $arguments, $this);
90
        return $this;
91
    }
92
93
    /**
94
     * @param string $name
95
     * @param string $value
96
     * @param bool $escape
97
     *
98
     * @return static
99
     */
100
    public function setAttribute(string $name, string $value, bool $escape = true): static
101
    {
102
        $this->attributes[$name] = $value;
103
        $this->escapes[$name] = $escape;
104
        return $this;
105
    }
106
107
    /**
108
     * @param array $attributes
109
     * @param bool $escape
110
     *
111
     * @return static
112
     */
113
    public function setAttributes(array $attributes, bool $escape = true): static
114
    {
115
        foreach ($attributes as $name => $value) {
116
            if (is_string($value)) {
117
                $this->setAttribute($name, $value, $escape);
118
            }
119
        }
120
        return $this;
121
    }
122
123
    /**
124
     * Append a class to the existing one.
125
     *
126
     * @param string $class
127
     *
128
     * @return static
129
     */
130
    public function addClass(string $class): static
131
    {
132
        $this->classes[] = trim($class);
133
        return $this;
134
    }
135
136
    /**
137
     * Prepend a class to the existing one.
138
     *
139
     * @param string $class
140
     *
141
     * @return static
142
     */
143
    public function addBaseClass(string $class): static
144
    {
145
        $this->classes[0][] = trim($class);
146
        return $this;
147
    }
148
149
    /**
150
     * @param string $class
151
     *
152
     * @return static
153
     */
154
    public function setClass(string $class): static
155
    {
156
        // Actually appends the class.
157
        $this->addClass($class);
158
        return $this;
159
    }
160
161
    /**
162
     * @param Block $block
163
     *
164
     * @return static
165
     */
166
    protected function addBlock(Block $block): static
167
    {
168
        $this->blocks[] = $block;
169
        return $this;
170
    }
171
172
    /**
173
     * @inheritDoc
174
     */
175
    public function addText(string $text): static
176
    {
177
        $this->addBlock(new Text($text));
178
        return $this;
179
    }
180
181
    /**
182
     * @inheritDoc
183
     */
184
    public function addHtml(string $html): static
185
    {
186
        $this->addBlock(new Text($html, false));
187
        return $this;
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function addComment(string $comment): static
194
    {
195
        $this->addBlock(new Comment($comment));
196
        return $this;
197
    }
198
199
    /**
200
     * @param string $name
201
     * @param array $arguments
202
     *
203
     * @return void
204
     */
205
    protected function addWrapper(string $name, array $arguments = [])
206
    {
207
        $wrapper = $this->builder->createElement($name, [$arguments]);
208
        $this->wrappers[] = $wrapper;
209
        return $wrapper;
210
    }
211
212
    /**
213
     * @param int $index
214
     * @param string $class
215
     *
216
     * @return static
217
     */
218
    protected function setWrapperClass(int $index, string $class): static
219
    {
220
        $this->wrappers[$index]?->setClass($class);
221
        return $this;
222
    }
223
224
    /**
225
     * @param int $index
226
     * @param string $name
227
     * @param string $value
228
     *
229
     * @return static
230
     */
231
    public function setWrapperAttribute(int $index, string $name, string $value): static
232
    {
233
        $this->wrappers[$index]?->setAttribute($name, $value);
234
        return $this;
235
    }
236
237
    /**
238
     * @param int $index
239
     * @param array $attributes
240
     *
241
     * @return static
242
     */
243
    public function setWrapperAttributes(int $index, array $attributes): static
244
    {
245
        $this->wrappers[$index]?->setAttributes($attributes);
246
        return $this;
247
    }
248
249
    /**
250
     * @inheritDoc
251
     */
252
    public function when(bool $condition, Closure $closure): static
253
    {
254
        $condition && $closure($this);
255
        return $this;
256
    }
257
}
258