Passed
Push — main ( 1402e3...cedac2 )
by Thierry
18:03
created

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