Passed
Push — main ( 7ee73e...ea7c68 )
by Thierry
08:09
created

AbstractBuilder::prependClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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