|
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 */ |
|
30
|
|
|
public $model = null; |
|
31
|
|
|
|
|
32
|
|
|
/** @var bool */ |
|
33
|
|
|
public $autocomplete; |
|
34
|
|
|
|
|
35
|
|
|
/** @var array */ |
|
36
|
|
|
public $attributesList = [ |
|
37
|
|
|
'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; |
|
|
|
|
|
|
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
View Code Duplication |
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: