Completed
Push — master ( 5e1312...160e4d )
by Denis
01:45
created

FormBuilder   C

Complexity

Total Complexity 29

Size/Duplication

Total Lines 345
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 345
rs 5
c 1
b 0
f 0
wmc 29
lcom 2
cbo 25

28 Methods

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