Passed
Push — main ( 68e606...997043 )
by Thierry
02:29
created

HtmlComponent::wrappers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\UiBuilder\Component;
4
5
use Lagdo\UiBuilder\Component\Html\Element;
6
use Lagdo\UiBuilder\Component\Html\Html;
7
use Lagdo\UiBuilder\Component\Html\Text;
8
use Lagdo\UiBuilder\Engine\Engine;
9
use Closure;
10
11
use function is_array;
12
use function is_string;
13
14
/**
15
 * @method static setId(string $id, bool $escape = true)
16
 * @method static setClass(string $class, bool $escape = true)
17
 * @method static setFor(string $for, bool $escape = true)
18
 * @method static setName(string $name, bool $escape = true)
19
 * @method static setValue(string $value, bool $escape = true)
20
 * @method static setType(string $type, bool $escape = true)
21
 * @method static setTitle(string $type, bool $escape = true)
22
 * @method static setStyle(string $type, bool $escape = true)
23
 */
24
class HtmlComponent extends Component
25
{
26
    /**
27
     * @var HtmlElement
28
     */
29
    private $element;
30
31
    /**
32
     * @var array<HtmlElement>
33
     */
34
    private $wrappers = [];
35
36
    /**
37
     * @var array<Element|Component>
38
     */
39
    private $children = [];
40
41
    /**
42
     * The constructor
43
     *
44
     * @param Engine $engine
45
     * @param string $name
46
     * @param array $arguments
47
     */
48
    public function __construct(private Engine $engine, string $name, array $arguments = [])
49
    {
50
        $this->element = new HtmlElement($engine, $name);
51
52
        // Resolve arguments
53
        $this->contents(...$arguments);
54
55
        $this->onCreate();
56
    }
57
58
    /**
59
     * Called for each child after a perent is expanded.
60
     *
61
     * @param HtmlComponent $parent
62
     *
63
     * @return static
64
     */
65
    final public function expanded(HtmlComponent $parent): static
66
    {
67
        $this->onBuild($parent);
68
        return $this;
69
    }
70
71
    /**
72
     * @return static
73
     */
74
    public function contents(...$arguments): static
75
    {
76
        // Resolve arguments
77
        foreach ($arguments as $argument) {
78
            switch (true) {
79
            case is_array($argument):
80
                $this->element->setAttributes($argument);
81
                break;
82
            case is_string($argument):
83
                $this->children[] = new Html($argument);
84
                break;
85
            case is_a($argument, Element::class):
86
            case is_a($argument, Component::class):
87
                $this->children[] = $argument;
88
            }
89
        }
90
        return $this;
91
    }
92
93
    /**
94
     * @return HtmlElement
95
     */
96
    public function element(): HtmlElement
97
    {
98
        return $this->element;
99
    }
100
101
    /**
102
     * @return array<HtmlElement>
103
     */
104
    public function wrappers(): array
105
    {
106
        return $this->wrappers;
107
    }
108
109
    /**
110
     * @return array<Element|Component>
111
     */
112
    public function children(): array
113
    {
114
        return $this->children;
115
    }
116
117
    /**
118
     * @param string $text
119
     *
120
     * @return static
121
     */
122
    protected function addText(string $text): static
123
    {
124
        $this->children[] = new Text($text);
125
        return $this;
126
    }
127
128
    /**
129
     * @param string $html
130
     *
131
     * @return static
132
     */
133
    protected function addHtml(string $html): static
134
    {
135
        $this->children[] = new Html($html);
136
        return $this;
137
    }
138
139
    /**
140
     * @param string $method
141
     * @param array $arguments
142
     *
143
     * @return static
144
     */
145
    public function __call(string $method, array $arguments): static
146
    {
147
        $this->engine->callComponentHelper($this, $method, $arguments);
148
        return $this;
149
    }
150
151
    /**
152
     * @param string $class
153
     *
154
     * @return static
155
     */
156
    public function setClass(string $class): static
157
    {
158
        // Must actually append the class.
159
        $this->element->addClass($class);
160
        return $this;
161
    }
162
163
    /**
164
     * @param string $class
165
     *
166
     * @return static
167
     */
168
    public function addClass(string $class): static
169
    {
170
        return $this->setClass($class);
171
    }
172
173
    /**
174
     * @param string $name
175
     * @param string|null $value
176
     * @param bool $escape
177
     *
178
     * @return static
179
     */
180
    public function setAttribute(string $name, string|null $value = null, bool $escape = true): static
181
    {
182
        $this->element->setAttribute($name, $value, $escape);
183
        return $this;
184
    }
185
186
    /**
187
     * @param array $attributes
188
     * @param bool $escape
189
     *
190
     * @return static
191
     */
192
    public function setAttributes(array $attributes, bool $escape = true): static
193
    {
194
        $this->element->setAttributes($attributes, $escape);
195
        return $this;
196
    }
197
198
    /**
199
     * @param string $name
200
     * @param array $arguments
201
     *
202
     * @return HtmlElement
203
     */
204
    protected function addWrapper(string $name, array $arguments = []): HtmlElement
205
    {
206
        $wrapper = new HtmlElement($this->engine, $name, $arguments);
207
        $this->wrappers[] = $wrapper;
208
        return $wrapper;
209
    }
210
211
    /**
212
     * @param int $index
213
     *
214
     * @return HtmlElement|null
215
     */
216
    protected function wrapper(int $index): HtmlElement|null
217
    {
218
        return $this->wrappers[$index] ?? null;
219
    }
220
221
    /**
222
     * @param bool $condition
223
     * @param Closure $closure
224
     *
225
     * @return static
226
     */
227
    public function when(bool $condition, Closure $closure): static
228
    {
229
        $condition && $closure($this);
230
        return $this;
231
    }
232
233
    /**
234
     * @param Closure $closure
235
     *
236
     * @return static
237
     */
238
    public function with(Closure $closure): static
239
    {
240
        $closure($this);
241
        return $this;
242
    }
243
}
244