Completed
Push — master ( b78aa7...24e372 )
by Denis
06:09
created

FormBuilder::open()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Ngtfkx\Laradeck\FormBuilder;
4
5
6
use Illuminate\Support\Collection;
7
use Ngtfkx\Laradeck\FormBuilder\Elements\Form;
8
use Ngtfkx\Laradeck\FormBuilder\Traits\Common;
9
10
class FormBuilder
11
{
12
    use Common;
13
14
    /**
15
     * @var string
16
     */
17
    protected $action = '';
18
19
    /**
20
     * @var string
21
     */
22
    protected $method = 'get';
23
24
    /**
25
     * @var bool
26
     */
27
    protected $autocomplete = true;
28
29
    /**
30
     * @var bool
31
     */
32
    protected $novalidate = false;
33
34
    /**
35
     * @var string
36
     */
37
    protected $target = '_self';
38
39
    /**
40
     * @var string
41
     */
42
    protected $enctype = 'application/x-www-form-urlencoded';
43
44
    /**
45
     * @var bool
46
     */
47
    protected $isClosed = false;
48
49
    /**
50
     * FormBuilder constructor.
51
     */
52
    public function __construct()
53
    {
54
        $this->classes = new Collection();
55
56
        $this->attributes = new Collection();
57
58
        $this->styles = new Collection();
59
    }
60
61
    public function open($action, $method = 'get'): FormBuilder
62
    {
63
        $this->action = $action;
64
65
        $this->method = strtolower($method);
66
67
        $this->isClosed = false;
68
69
        return $this;
70
    }
71
72
    public function close(): FormBuilder
73
    {
74
        $this->isClosed = true;
75
76
        return $this;
77
    }
78
79
    protected function enctype($value): FormBuilder
80
    {
81
        $this->enctype = $value;
82
83
        return $this;
84
    }
85
86
    public function multipart(): FormBuilder
87
    {
88
        return $this->enctype('multipart/form-data');
89
    }
90
91
    public function plain(): FormBuilder
92
    {
93
        return $this->enctype('text/plain');
94
    }
95
96
    public function urlencoded (): FormBuilder
97
    {
98
        return $this->enctype('application/x-www-form-urlencoded');
99
    }
100
101
    public function autocomplete(bool $state = true): FormBuilder
102
    {
103
        $this->autocomplete = $state;
104
105
        return $this;
106
    }
107
108
    public function novalidate(bool $state = true): FormBuilder
109
    {
110
        $this->novalidate = $state;
111
112
        return $this;
113
    }
114
115
    public function __toString()
116
    {
117
        if ($this->isClosed) {
118
            $html = '</form>';
119
        } else {
120
            $form = (new Form())
121
                ->id($this->id)
122
                ->name($this->name)
123
                ->attrs($this->attributes)
124
                ->attrs([
125
                    'action' => $this->action,
126
                    'method' => $this->method,
127
                    'enctype' => $this->enctype,
128
                    'autocomplete' => $this->autocomplete,
129
                    'novalidate' => $this->novalidate,
130
                ])
131
                ->styles($this->styles)
132
                ->classes($this->classes);
133
134
            $html = '' . $form;
135
        }
136
137
        return $html;
138
    }
139
}