Completed
Push — master ( ebd2ab...759acc )
by Denis
01:24
created

FormBuilder   D

Complexity

Total Complexity 31

Size/Duplication

Total Lines 365
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 365
rs 4.9
c 1
b 0
f 0
wmc 31
lcom 2
cbo 25

29 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A layout() 0 10 2
A open() 0 6 1
A close() 0 6 1
A hidden() 0 4 1
A text() 0 4 1
A email() 0 4 1
A password() 0 4 1
A number() 0 4 1
A tel() 0 4 1
A color() 0 4 1
A datetime() 0 4 1
A date() 0 4 1
A range() 0 4 1
A search() 0 4 1
A time() 0 4 1
A url() 0 4 1
A month() 0 4 1
A week() 0 4 1
A submit() 0 4 1
A reset() 0 4 1
A button() 0 4 1
A textarea() 0 4 1
A file() 0 4 1
A image() 0 4 1
A checkbox() 0 4 1
A radio() 0 4 1
A select() 0 4 1
A __toString() 0 4 2
1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder;
4
5
6
use Ngtfkx\Laradeck\FormBuilder\Elements;
7
use Ngtfkx\Laradeck\FormBuilder\Layouts\AbstractLayout;
8
9
class FormBuilder
10
{
11
    /**
12
     * @var bool Признак, что форма должна быть закрыта
13
     */
14
    protected $isClosed = false;
15
16
    /**
17
     * @var Elements\Form $form
18
     */
19
    protected $form;
20
21
    protected $layout;
22
23
    /**
24
     * FormBuilder constructor.
25
     */
26
    public function __construct()
27
    {
28
29
    }
30
31
    /**
32
     * Сеттер для шаблона верстки (тип фремворка и тип расположения формы)
33
     *
34
     * @param string $cssFramework
35
     * @param null|string $orientation
36
     * @return FormBuilder
37
     */
38
    public function layout(string $cssFramework, ?string $orientation = null): FormBuilder
39
    {
40
        $className = "\\Ngtfkx\\Laradeck\\FormBuilder\\Layouts\\" . studly_case($cssFramework);
41
42
        if(class_exists($className)) {
43
            $this->layout = (new $className)->orientation($orientation);
44
        }
45
46
        return $this;
47
    }
48
49
    /**
50
     * Открыть форму
51
     *
52
     * @param string $action URL для отправки данных формы
53
     * @param string $method Метод отправки данных формы
54
     * @return Elements\Form
55
     */
56
    public function open(?string $action = '', ?string $method = 'get'): Elements\Form
57
    {
58
        $this->form = (new Elements\Form($this->layout))->action($action)->method($method);
59
60
        return $this->form;
61
    }
62
63
    /**
64
     * Закрыть форму
65
     *
66
     * @return FormBuilder
67
     */
68
    public function close(): FormBuilder
69
    {
70
        $this->isClosed = true;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Создать элемент формы типа hidden
77
     *
78
     * @param string|null $name
79
     * @param string|null $value
80
     * @return Elements\Hidden
81
     */
82
    public function hidden(string $name = null, string $value = null): Elements\Hidden
83
    {
84
        return new Elements\Hidden($name, $value);
85
    }
86
87
    /**
88
     * Создать элемент формы типа text
89
     *
90
     * @param string|null $name
91
     * @param string|null $value
92
     * @return Elements\Text\Text
93
     */
94
    public function text(string $name = null, string $value = null): Elements\Text\Text
95
    {
96
        return new Elements\Text\Text($name, $value);
97
    }
98
99
    /**
100
     * Создать элемент формы типа email
101
     *
102
     * @param string|null $name
103
     * @param string|null $value
104
     * @return Elements\Text\Email
105
     */
106
    public function email(string $name = null, string $value = null): Elements\Text\Email
107
    {
108
        return new Elements\Text\Email($name, $value);
109
    }
110
111
    /**
112
     * Создать элемент формы типа password
113
     *
114
     * @param string|null $name
115
     * @param string|null $value
116
     * @return Elements\Text\Password
117
     */
118
    public function password(string $name = null, string $value = null): Elements\Text\Password
119
    {
120
        return new Elements\Text\Password($name, $value);
121
    }
122
123
    /**
124
     * Создать элемент формы типа number
125
     *
126
     * @param string|null $name
127
     * @param string|null $value
128
     * @return Elements\Text\Number
129
     */
130
    public function number(string $name = null, string $value = null): Elements\Text\Number
131
    {
132
        return new Elements\Text\Number($name, $value);
133
    }
134
135
    /**
136
     * Создать элемент формы типа tel
137
     *
138
     * @param string|null $name
139
     * @param string|null $value
140
     * @return Elements\Text\Tel
141
     */
142
    public function tel(string $name = null, string $value = null): Elements\Text\Tel
143
    {
144
        return new Elements\Text\Tel($name, $value);
145
    }
146
147
    /**
148
     * Создать элемент формы типа color
149
     *
150
     * @param string|null $name
151
     * @param string|null $value
152
     * @return Elements\Text\Color
153
     */
154
    public function color(string $name = null, string $value = null): Elements\Text\Color
155
    {
156
        return new Elements\Text\Color($name, $value);
157
    }
158
159
    /**
160
     * Создать элемент формы типа datetime
161
     *
162
     * @param string|null $name
163
     * @param string|null $value
164
     * @return Elements\Text\Datetime
165
     */
166
    public function datetime(string $name = null, string $value = null): Elements\Text\Datetime
167
    {
168
        return new Elements\Text\Datetime($name, $value);
169
    }
170
171
    /**
172
     * Создать элемент формы типа date
173
     *
174
     * @param string|null $name
175
     * @param string|null $value
176
     * @return Elements\Text\Date
177
     */
178
    public function date(string $name = null, string $value = null): Elements\Text\Date
179
    {
180
        return new Elements\Text\Date($name, $value);
181
    }
182
183
    /**
184
     * Создать элемент формы типа range
185
     *
186
     * @param string|null $name
187
     * @param string|null $value
188
     * @return Elements\Text\Range
189
     */
190
    public function range(string $name = null, string $value = null): Elements\Text\Range
191
    {
192
        return new Elements\Text\Range($name, $value);
193
    }
194
195
    /**
196
     * Создать элемент формы типа search
197
     *
198
     * @param string|null $name
199
     * @param string|null $value
200
     * @return Elements\Text\Search
201
     */
202
    public function search(string $name = null, string $value = null): Elements\Text\Search
203
    {
204
        return new Elements\Text\Search($name, $value);
205
    }
206
207
    /**
208
     * Создать элемент формы типа time
209
     *
210
     * @param string|null $name
211
     * @param string|null $value
212
     * @return Elements\Text\Time
213
     */
214
    public function time(string $name = null, string $value = null): Elements\Text\Time
215
    {
216
        return new Elements\Text\Time($name, $value);
217
    }
218
219
    /**
220
     * Создать элемент формы типа url
221
     *
222
     * @param string|null $name
223
     * @param string|null $value
224
     * @return Elements\Text\Url
225
     */
226
    public function url(string $name = null, string $value = null): Elements\Text\Url
227
    {
228
        return new Elements\Text\Url($name, $value);
229
    }
230
231
    /**
232
     * Создать элемент формы типа month
233
     *
234
     * @param string|null $name
235
     * @param string|null $value
236
     * @return Elements\Text\Month
237
     */
238
    public function month(string $name = null, string $value = null): Elements\Text\Month
239
    {
240
        return new Elements\Text\Month($name, $value);
241
    }
242
243
    /**
244
     * Создать элемент формы типа week
245
     *
246
     * @param string|null $name
247
     * @param string|null $value
248
     * @return Elements\Text\Week
249
     */
250
    public function week(string $name = null, string $value = null): Elements\Text\Week
251
    {
252
        return new Elements\Text\Week($name, $value);
253
    }
254
255
    /**
256
     * Создать элемент формы типа submit
257
     *
258
     * @param string|null $name
259
     * @param string|null $value
260
     * @return Elements\Submit\Submit
261
     */
262
    public function submit(string $name = null, string $value = null): Elements\Submit\Submit
263
    {
264
        return new Elements\Submit\Submit($name, $value);
265
    }
266
267
    /**
268
     * Создать элемент формы типа reset
269
     *
270
     * @param string|null $name
271
     * @param string|null $value
272
     * @return Elements\Submit\Reset
273
     */
274
    public function reset(string $name = null, string $value = null): Elements\Submit\Reset
275
    {
276
        return new Elements\Submit\Reset($name, $value);
277
    }
278
279
    /**
280
     * Создать элемент формы типа button
281
     *
282
     * @param string|null $name
283
     * @param string|null $value
284
     * @return Elements\Area\Button
285
     */
286
    public function button(string $name = null, string $value = null): Elements\Area\Button
287
    {
288
        return new Elements\Area\Button($name, $value);
289
    }
290
291
    /**
292
     * Создать элемент формы типа textarea
293
     *
294
     * @param string|null $name
295
     * @param string|null $value
296
     * @return Elements\Area\Textarea
297
     */
298
    public function textarea(string $name = null, string $value = null): Elements\Area\Textarea
299
    {
300
        return new Elements\Area\Textarea($name, $value);
301
    }
302
303
    /**
304
     * Создать элемент формы типа file
305
     *
306
     * @param string|null $name
307
     * @return Elements\File\File
308
     */
309
    public function file(string $name = null): Elements\File\File
310
    {
311
        return new Elements\File\File($name);
312
    }
313
314
    /**
315
     * Создать элемент формы типа image
316
     *
317
     * @param string|null $name
318
     * @return Elements\File\Image
319
     */
320
    public function image(string $name = null): Elements\File\Image
321
    {
322
        return new Elements\File\Image($name);
323
    }
324
325
    /**
326
     * Создать элемент формы типа checkbox
327
     *
328
     * @param string|null $name
329
     * @param string|null $value
330
     * @param bool|null $state
331
     * @return Elements\Checked\Checkbox
332
     */
333
    public function checkbox(string $name = null, ?string $value = '1', ?bool $state = false): Elements\Checked\Checkbox
334
    {
335
        return new Elements\Checked\Checkbox($name, $value, $state);
0 ignored issues
show
Bug introduced by
It seems like $state defined by parameter $state on line 333 can also be of type null; however, Ngtfkx\Laradeck\FormBuil...dElement::__construct() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
336
    }
337
338
    /**
339
     * Создать элемент формы типа radio
340
     *
341
     * @param string|null $name
342
     * @param string|null $value
343
     * @param bool|null $state
344
     * @return Elements\Checked\Radio
345
     */
346
    public function radio(string $name = null, ?string $value = '1', ?bool $state = false): Elements\Checked\Radio
347
    {
348
        return new Elements\Checked\Radio($name, $value, $state);
0 ignored issues
show
Bug introduced by
It seems like $state defined by parameter $state on line 346 can also be of type null; however, Ngtfkx\Laradeck\FormBuil...dElement::__construct() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
349
    }
350
351
    /**
352
     * Создать элемент формы типа select
353
     *
354
     * @param string|null $name
355
     * @param string|null $value
356
     * @param iterable|null $options
357
     * @return Elements\Select
358
     */
359
    public function select(string $name = null, string $value = null, ?iterable $options = null): Elements\Select
360
    {
361
        return new Elements\Select($name, $value, $options);
362
    }
363
364
    /**
365
     * Преобразовать в строку для вывода в HTML
366
     *
367
     * @return string
368
     */
369
    public function __toString()
370
    {
371
        return ($this->isClosed) ? '</form>' : '<form>';
372
    }
373
}