Completed
Push — master ( b5f959...da0e5f )
by Costin
02:20
created

Form::attributesList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SoareCostin\BladeFormComponents;
4
5
use SoareCostin\BladeFormComponents\Traits\GluesAttributes;
6
7
class Form
8
{
9
    use GluesAttributes;
10
11
    /** @var array */
12
    protected $params;
13
14
    /** @var string */
15
    public $action;
16
17
    /** @var string */
18
    public $method;
19
20
    /** @var string */
21
    public $httpMethod;
22
23
    /** @var bool */
24
    public $files;
25
26
    /** @var string */
27
    public $enctype;
28
29
    /** @var Illuminate\Database\Eloquent\Model */
0 ignored issues
show
Bug introduced by
The type SoareCostin\BladeFormCom...Database\Eloquent\Model was not found. Did you mean Illuminate\Database\Eloquent\Model? If so, make sure to prefix the type with \.
Loading history...
30
    public $model = null;
31
32
    /** @var bool */
33
    public $autocomplete;
34
35
    protected function attributesList()
36
    {
37
        return ['action', 'method', 'enctype', 'autocomplete'];
38
    }
39
40
    /**
41
     * Will be called when the form is opened.
42
     */
43
    public function setup(array $params)
44
    {
45
        $this->params = $params;
46
47
        $this->setModel();
48
        $this->setAction();
49
        $this->setMethod();
50
        $this->setFiles();
51
        $this->setAutocomplete();
52
    }
53
54
    /**
55
     * Will be called when the form is closed.
56
     */
57
    public function delete()
58
    {
59
        $this->parmas = null;
0 ignored issues
show
Bug Best Practice introduced by
The property parmas does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
60
        $this->model = null;
61
        $this->action = null;
62
        $this->method = null;
63
        $this->files = null;
64
    }
65
66
    protected function setModel()
67
    {
68
        if (isset($this->params['model']) && ! empty($this->params['model'])) {
69
            $this->model = $this->params['model'];
70
        }
71
    }
72
73
    protected function setAction()
74
    {
75
        if (isset($this->params['url']) && ! empty($this->params['url'])) {
76
            $this->action = $this->params['url'];
77
        }
78
    }
79
80
    protected function setMethod()
81
    {
82
        // Set default
83
        $this->method = $this->httpMethod = 'post';
84
85
        // Check method in params
86
87
        if (isset($this->params['method']) && ! empty($this->params['method'])) {
88
            $this->method = $this->httpMethod = $this->params['method'];
89
        }
90
91
        if (in_array(strtoupper($this->method), ['GET', 'POST', 'PUT', 'DELETE'])) {
92
            $this->method = $this->httpMethod = strtoupper($this->method);
93
94
            // If the form type is POST but we have sent an existing model make it a PUT request
95
            if ($this->httpMethod == 'POST' && ! is_null($this->model)) {
96
                $this->httpMethod = 'PUT';
97
            }
98
99
            // The 'method' attribute is used to be used on the <form> element and can only by GET or POST
100
            if ($this->method !== 'GET') {
101
                $this->method = 'POST';
102
            }
103
        }
104
    }
105
106
    protected function setFiles()
107
    {
108
        if (isset($this->params['files']) && ! empty($this->params['files'])) {
109
            $this->files = $this->params['files'];
110
            $this->enctype = 'multipart/form-data';
111
        }
112
    }
113
114
    protected function setAutocomplete()
115
    {
116
        // Set default autocomplete option (true/false) from cofing file
117
        $this->autocomplete = config('blade-form-components.autocomplete');
118
119
        if (isset($this->params['autocomplete'])) {
120
            $this->autocomplete = $this->params['autocomplete'];
121
        }
122
    }
123
}
124