Passed
Push — master ( cb4302...32b1ba )
by Costin
02:24
created

Form::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 7
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 \Illuminate\Support\Collection */
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 */
30
    public $model = null;
31
32
    /** @var string */
33
    public $autocomplete = 'off';
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 = collect($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->params = null;
60
        $this->model = null;
61
        $this->action = null;
62
        $this->method = null;
63
        $this->files = null;
64
    }
65
66
    protected function setModel()
67
    {
68
        $this->model = $this->params->get('model');
69
    }
70
71
    protected function setAction()
72
    {
73
        $this->action = $this->params->get('url');
74
    }
75
76
    protected function setMethod()
77
    {
78
        $this->method = $this->httpMethod = $this->params->get('method', 'post');
79
80
        if (in_array(strtoupper($this->method), ['GET', 'POST', 'PUT', 'DELETE'])) {
81
            $this->method = $this->httpMethod = strtoupper($this->method);
82
83
            // If the form type is POST but we have sent an existing model make it a PUT request
84
            if ($this->httpMethod == 'POST' && ! is_null($this->model)) {
85
                $this->httpMethod = 'PUT';
86
            }
87
88
            // The 'method' attribute is used to be used on the <form> element and can only by GET or POST
89
            if ($this->method !== 'GET') {
90
                $this->method = 'POST';
91
            }
92
        }
93
    }
94
95
    protected function setFiles()
96
    {
97
        $this->files = $this->params->get('files', 'false');
98
        $this->enctype = $this->files ? 'multipart/form-data' : '';
99
    }
100
101
    protected function setAutocomplete()
102
    {
103
        // Set default autocomplete option (on/off) from cofing file
104
        $this->autocomplete = $this->params->get('autocomplete', config('blade-form-components.autocomplete'));
105
    }
106
}
107