Builder   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
dl 0
loc 178
rs 10
c 1
b 0
f 0
wmc 15

15 Methods

Rating   Name   Duplication   Size   Complexity  
A createComponent() 0 3 1
A createComponentOfClass() 0 3 1
A pick() 0 3 1
A build() 0 3 1
A when() 0 3 1
A __call() 0 3 1
A comment() 0 3 1
A html() 0 3 1
A tag() 0 3 1
A text() 0 3 1
A registerHelper() 0 3 1
A each() 0 3 1
A list() 0 3 1
A __construct() 0 4 1
A _createComponent() 0 4 1
1
<?php
2
3
namespace Lagdo\UiBuilder;
4
5
use Lagdo\UiBuilder\Builder\Engine\Engine;
6
use Lagdo\UiBuilder\Component\Component;
7
use Lagdo\UiBuilder\Component\HtmlComponent;
8
use Lagdo\UiBuilder\Component\Html\Comment;
9
use Lagdo\UiBuilder\Component\Html\Element;
10
use Lagdo\UiBuilder\Component\Html\Html;
11
use Lagdo\UiBuilder\Component\Html\Text;
12
use Lagdo\UiBuilder\Component\Virtual\EachComponent;
13
use Lagdo\UiBuilder\Component\Virtual\ListComponent;
14
use Lagdo\UiBuilder\Component\Virtual\PickComponent;
15
use Lagdo\UiBuilder\Component\Virtual\WhenComponent;
16
use Closure;
17
18
abstract class Builder implements BuilderInterface
19
{
20
    use Builder\LayoutBuilderTrait;
21
    use Builder\ButtonBuilderTrait;
22
    use Builder\DropdownBuilderTrait;
23
    use Builder\PanelBuilderTrait;
24
    use Builder\FormBuilderTrait;
25
    use Builder\MenuBuilderTrait;
26
    use Builder\TabBuilderTrait;
27
    use Builder\PaginationBuilderTrait;
28
    use Builder\TableBuilderTrait;
29
30
    /**
31
     * @var int
32
     */
33
    public const TARGET_BUILDER = 0;
34
35
    /**
36
     * @var int
37
     */
38
    public const TARGET_COMPONENT = 1;
39
40
    /**
41
     * @var int
42
     */
43
    public const TARGET_ELEMENT = 2;
44
45
    /**
46
     * @var Engine
47
     */
48
    private $engine;
49
50
    /**
51
     * The constructor
52
     */
53
    public function __construct()
54
    {
55
        $this->engine = new Engine($this);
56
        $this->_init();
57
    }
58
59
    /**
60
     * @return void
61
     */
62
    abstract protected function _init(): void;
63
64
    /**
65
     * @param string $method
66
     * @param array $arguments
67
     *
68
     * @return HtmlComponent
69
     */
70
    public function __call(string $method, array $arguments): mixed
71
    {
72
        return $this->engine->callBuilderHelper($method, $arguments);
73
    }
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function registerHelper(string $tagPrefix, string $tagTarget, Closure $tagHelper): void
79
    {
80
        $this->engine->registerHelper($tagPrefix, $tagTarget, $tagHelper);
81
    }
82
83
    /**
84
     * Create a component
85
     *
86
     * @template T of HtmlComponent
87
     * @param string $name
88
     * @param array $arguments
89
     * @psalm-param class-string<T> $class
90
     *
91
     * @return T
0 ignored issues
show
Bug introduced by
The type Lagdo\UiBuilder\T was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
     */
93
    private function _createComponent(string $name, array $arguments = [],
94
        string $class = HtmlComponent::class): mixed
95
    {
96
        return new $class($this->engine, $name, $arguments);
97
    }
98
99
    /**
100
     * @inheritDoc
101
     */
102
    public function tag(string $name, ...$arguments): HtmlComponent
103
    {
104
        return $this->_createComponent($name, $arguments);
105
    }
106
107
    /**
108
     * Create a component
109
     *
110
     * @param string $name
111
     * @param array $arguments
112
     *
113
     * @return HtmlComponent
114
     */
115
    public function createComponent(string $name, $arguments = []): HtmlComponent
116
    {
117
        return $this->_createComponent($name, $arguments);
118
    }
119
120
    /**
121
     * Create a component of a given class name
122
     *
123
     * @template T of HtmlComponent
124
     * @psalm-param class-string<T> $class
125
     * @param array $arguments
126
     *
127
     * @return T
128
     */
129
    protected function createComponentOfClass(string $class, $arguments = []): HtmlComponent
130
    {
131
        return $this->_createComponent($class::$tag, $arguments, $class);
0 ignored issues
show
Bug introduced by
The property tag does not exist on string.
Loading history...
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function each(array $values, Closure $closure): Component
138
    {
139
        return new EachComponent($values, $closure);
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    public function list(...$arguments): Component
146
    {
147
        return new ListComponent($arguments);
148
    }
149
150
    /**
151
     * @inheritDoc
152
     */
153
    public function when(bool $condition, Closure $closure): Component
154
    {
155
        return new WhenComponent($condition, $closure);
156
    }
157
158
    /**
159
     * @inheritDoc
160
     */
161
    public function pick(...$arguments): Component
162
    {
163
        return new PickComponent($arguments);
164
    }
165
166
    /**
167
     * @inheritDoc
168
     */
169
    public function text(string $text): Element
170
    {
171
        return new Text($text);
172
    }
173
174
    /**
175
     * @inheritDoc
176
     */
177
    public function html(string $html): Element
178
    {
179
        return new Html($html);
180
    }
181
182
    /**
183
     * @inheritDoc
184
     */
185
    public function comment(string $comment): Element
186
    {
187
        return new Comment($comment);
188
    }
189
190
    /**
191
     * @inheritDoc
192
     */
193
    public function build(...$arguments): string
194
    {
195
        return $this->engine->build($arguments);
196
    }
197
}
198