Completed
Push — master ( 839e9e...4e091b )
by Costin
03:53
created

Form::setFiles()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 3
nc 2
nop 0
1
<?php
2
3
namespace SoareCostin\BladeFormComponents;
4
5
class Form
6
{
7
    /** @var array */
8
    protected $params;
9
10
    /** @var string */
11
    public $action;
12
13
    /** @var string */
14
    public $method;
15
    
16
    /** @var bool */
17
    public $files;
18
19
    /** @var Illuminate\Database\Eloquent\Model */
20
    public $model = null;
21
    
22
    /** @var bool */
23
    public $autocomplete;
24
    
25
    /**
26
     * Will be called when the form is opened
27
     */
28
    public function setup(array $params)
29
    {
30
        $this->params = $params;
31
32
        $this->setModel();
33
        $this->setAction();
34
        $this->setMethod();
35
        $this->setFiles();
36
        $this->setAutocomplete();
37
    }
38
39
    /**
40
     * Will be called when the form is closed
41
     */
42
    public function delete()
43
    {
44
        $this->parmas = null;
0 ignored issues
show
Bug introduced by
The property parmas does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
        $this->model = null;
46
        $this->action = null;
47
        $this->method = null;
48
        $this->files = null;
49
    }
50
51
    protected function setModel()
52
    {
53
        if (isset($this->params['model']) && !empty($this->params['model'])) {
54
            $this->model = $this->params['model'];
55
        }
56
    }
57
58
    protected function setAction()
59
    {
60 View Code Duplication
        if (isset($this->params['url']) && !empty($this->params['url'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
61
            $this->action = $this->params['url'];
62
        }
63
    }
64
    
65
    protected function setMethod()
66
    {
67
        // Set default
68
        $this->method = 'post';
69
70
        // Check method in params
71 View Code Duplication
        if (isset($this->params['method']) && !empty($this->params['method'])) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
            $this->method = $this->params['method'];
73
        }
74
75
        if (in_array(strtoupper($this->method), ['GET', 'POST', 'PUT', 'DELETE'])) {
76
            $this->method = strtoupper($this->method);
77
78
            // If the form type is POST but we have sent an existing model make it a PUT request
79
            if ($this->method == 'POST' && !is_null($this->model)) {
80
                $this->method = 'PUT';
81
            }
82
        }
83
    }
84
    
85
    protected function setFiles()
86
    {
87
        if (isset($this->params['files']) && !empty($this->params['files'])) {
88
            $this->files = $this->params['files'];
89
        }
90
    }
91
    
92 View Code Duplication
    protected function setAutocomplete()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94
        // Set default autocomplete option (true/false) from cofing file
95
        $this->autocomplete = config('blade-form-components.autocomplete');
96
97
        if (isset($this->params['autocomplete'])) {
98
            $this->autocomplete = $this->params['autocomplete'];
99
        }
100
    }
101
}