Form::setErrors()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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