Passed
Push — main ( 47a406...6037b2 )
by Thierry
02:35
created

HtmlComponent::setWrapperAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lagdo\UiBuilder\Component\Base;
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\Html\HtmlBuilder;
9
use Closure;
10
11
use function is_array;
12
use function is_string;
13
14
/**
15
 * @method static setId(string $id)
16
 * @method static setClass(string $class)
17
 * @method static setFor(string $for)
18
 * @method static setName(string $name)
19
 * @method static setValue(string $value)
20
 * @method static setType(string $type)
21
 * @method static setTitle(string $type)
22
 * @method static setStyle(string $type)
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 HtmlBuilder $builder
45
     * @param string $name
46
     * @param array $arguments
47
     */
48
    public function __construct(private HtmlBuilder $builder, string $name, array $arguments = [])
49
    {
50
        $this->element = new HtmlElement($builder, $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->builder->callComponentFactory($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 array $arguments
176
     *
177
     * @return HtmlElement
178
     */
179
    protected function addWrapper(string $name, array $arguments = []): HtmlElement
180
    {
181
        $wrapper = new HtmlElement($this->builder, $name, $arguments);
182
        $this->wrappers[] = $wrapper;
183
        return $wrapper;
184
    }
185
186
    /**
187
     * @param int $index
188
     *
189
     * @return HtmlElement|null
190
     */
191
    protected function wrapper(int $index): HtmlElement|null
192
    {
193
        return $this->wrappers[$index] ?? null;
194
    }
195
196
    /**
197
     * @inheritDoc
198
     */
199
    public function when(bool $condition, Closure $closure): static
200
    {
201
        $condition && $closure($this);
202
        return $this;
203
    }
204
}
205