|
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; |
|
|
|
|
|
|
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'])) { |
|
|
|
|
|
|
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'])) { |
|
|
|
|
|
|
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() |
|
|
|
|
|
|
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
|
|
|
} |
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: