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