Passed
Push — main ( ed843a...1402e3 )
by Thierry
02:16
created

Builder::tag()   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;
4
5
use Lagdo\UiBuilder\Scope\UiBuilder;
6
use LogicException;
7
8
use function ltrim;
9
use function rtrim;
10
11
abstract class Builder implements BuilderInterface
12
{
13
    const BTN_PRIMARY = 1;
14
    const BTN_SECONDARY = 2;
15
    const BTN_SUCCESS = 4;
16
    const BTN_INFO = 8;
17
    const BTN_WARNING = 16;
18
    const BTN_DANGER = 32;
19
    const BTN_OUTLINE = 64;
20
    const BTN_FULL_WIDTH = 128;
21
    const BTN_LARGE = 256;
22
    const BTN_SMALL = 512;
23
24
    /**
25
     * @var UiBuilder
26
     */
27
    protected $builder;
28
29
    /**
30
     * The constructor
31
     * 
32
     * @param BuilderSetup $setup
33
     */
34
    public function __construct(protected BuilderSetup $setup)
35
    {
36
        $this->builder = $setup->getBuilder();
37
    }
38
39
    /**
40
     * @param string $method
41
     * @param array $arguments
42
     *
43
     * @return self
44
     * @throws LogicException When element is not initialized yet
45
     */
46
    public function __call(string $method, array $arguments)
47
    {
48
        $this->builder->make($method, $arguments);
49
        return $this;
50
    }
51
52
    /**
53
     * @param string $name
54
     * @return void
55
     */
56
    public function tag(string $name, ...$arguments)
57
    {
58
        $this->builder->createScope($name, $arguments);
59
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Lagdo\UiBuilder\Builder which is incompatible with the documented return type void.
Loading history...
60
    }
61
62
    /**
63
     * @param string $name
64
     * @param string $value
65
     *
66
     * @return void
67
     */
68
    public function setAttribute(string $name, string $value)
69
    {
70
        $this->builder->setAttribute($name, $value);
71
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Lagdo\UiBuilder\Builder which is incompatible with the documented return type void.
Loading history...
72
    }
73
74
    /**
75
     * @param array $attributes
76
     *
77
     * @return self
78
     */
79
    public function setAttributes(array $attributes): self
80
    {
81
        foreach ($attributes as $name => $value) {
82
            $this->builder->setAttribute($name, $value);
83
        }
84
        return $this;
85
    }
86
87
    /**
88
     * @param string $class
89
     *
90
     * @return self
91
     */
92
    public function setClass(string $class): self
93
    {
94
        // Don't overwrite the current class.
95
        $this->builder->appendClass($class);
96
        return $this;
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function addText(string $text): self
103
    {
104
        $this->builder->addText($text);
105
        return $this;
106
    }
107
108
    /**
109
     * @inheritDoc
110
     */
111
    public function addHtml(string $html): self
112
    {
113
        $this->builder->addHtml($html);
114
        return $this;
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function addComment(string $comment): self
121
    {
122
        $this->builder->addComment($comment);
123
        return $this;
124
    }
125
126
    /**
127
     * @inheritDoc
128
     */
129
    public function checkbox(bool $checked = false, ...$arguments): self
130
    {
131
        $this->builder->createScope('input', $arguments);
132
        $this->builder->setAttribute('type', 'checkbox');
133
        if ($checked) {
134
            $this->builder->setAttribute('checked', 'checked');
135
        }
136
        return $this;
137
    }
138
139
    /**
140
     * @inheritDoc
141
     */
142
    public function radio(bool $checked = false, ...$arguments): self
143
    {
144
        $this->builder->createScope('input', $arguments);
145
        $this->builder->setAttribute('type', 'radio');
146
        if ($checked) {
147
            $this->builder->setAttribute('checked', 'checked');
148
        }
149
        return $this;
150
    }
151
152
    /**
153
     * @inheritDoc
154
     */
155
    public function option(bool $selected = false, ...$arguments): self
156
    {
157
        $this->builder->createScope('option', $arguments);
158
        if ($selected) {
159
            $this->builder->setAttribute('selected', 'selected');
160
        }
161
        return $this;
162
    }
163
164
    /**
165
     * @param string $tagName
166
     *
167
     * @return string
168
     */
169
    protected abstract function _formTagClass(string $tagName): string;
170
171
    /**
172
     * @inheritDoc
173
     */
174
    public function formTagClass(string $tagName, string $class = ''): string
175
    {
176
        return rtrim($this->_formTagClass($tagName) . ' ' . ltrim($class));
177
    }
178
179
    /**
180
     * @inheritDoc
181
     */
182
    public function formCol(int $width = 12, ...$arguments): self
183
    {
184
        return $this->col($width, ...$arguments);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->col($width, $arguments) returns the type Lagdo\UiBuilder\Builder\HtmlInterface which includes types incompatible with the type-hinted return Lagdo\UiBuilder\Builder.
Loading history...
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    public function formRowTag(): string
191
    {
192
        return 'div';
193
    }
194
195
    /**
196
     * @return self
197
     */
198
    public function clear(): self
199
    {
200
        $this->builder->clear();
201
        return $this;
202
    }
203
204
    /**
205
     * @return self
206
     */
207
    public function end(): self
208
    {
209
        $this->builder->end();
210
        return $this;
211
    }
212
213
    /**
214
     * @return self
215
     */
216
    public function endShorted(): self
217
    {
218
        $this->builder->endShorted();
219
        return $this;
220
    }
221
222
    /**
223
     * @inheritDoc
224
     * @throws RuntimeException When element is not initialized yet.
225
     */
226
    public function endOpened(): self
227
    {
228
        $this->builder->endOpened();
229
        return $this;
230
    }
231
232
    /**
233
     * @inheritDoc
234
     */
235
    public function build(): string
236
    {
237
        return $this->builder->build();
238
    }
239
240
    /**
241
     * @return string
242
     */
243
    public function __toString(): string
244
    {
245
        return $this->build();
246
    }
247
}
248