|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AnilcanCakir\Former; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Support\HtmlString; |
|
7
|
|
|
use AnilcanCakir\Former\Fields\Field; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use AnilcanCakir\Former\Contracts\FormerHelper; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
class Form |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The form fields. |
|
15
|
|
|
* |
|
16
|
|
|
* @var Collection|Field[] |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $fields; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The form model. |
|
22
|
|
|
* |
|
23
|
|
|
* @var Model |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $model; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The form theme. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $theme; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The helper of former. |
|
36
|
|
|
* |
|
37
|
|
|
* @var FormerHelper |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $helper; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The action of form. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $action; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* The method of form. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $method = 'POST'; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Form constructor. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Model|null $model |
|
59
|
|
|
* @param FormerHelper $formerHelper |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __construct(Model $model = null, FormerHelper $formerHelper) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->model = $model; |
|
64
|
|
|
$this->helper = $formerHelper; |
|
65
|
|
|
|
|
66
|
|
|
$this->fields = new Collection(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get the model fields. |
|
71
|
|
|
* |
|
72
|
|
|
* @return Collection |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getFields(): Collection |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->fields; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set the model fields. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $input |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @param array $rules |
|
85
|
|
|
*/ |
|
86
|
|
|
public function addField($input, $name, array $rules) |
|
87
|
|
|
{ |
|
88
|
|
|
/** @var Field $field */ |
|
89
|
|
|
$field = new $input(); |
|
90
|
|
|
$field->setForm($this); |
|
91
|
|
|
$field->setName($name); |
|
92
|
|
|
$field->setRules($rules); |
|
93
|
|
|
|
|
94
|
|
|
$this->fields->put($name, $field); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Get the form model. |
|
99
|
|
|
* |
|
100
|
|
|
* @return Model |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getModel(): Model |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->model; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public function getHelper(): FormerHelper |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->helper; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function getAction(): string |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->action; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return string |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getMethod(): string |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->method; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the form model. |
|
130
|
|
|
* |
|
131
|
|
|
* @param Model $model |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setModel(Model $model) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->model = $model; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Set the form theme. |
|
140
|
|
|
* |
|
141
|
|
|
* @param string|null $theme |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setTheme($theme) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->theme = $theme; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function setAction($action) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->action = $action; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function setActionFromRoute($route) |
|
154
|
|
|
{ |
|
155
|
|
|
$this->action = $this->helper->getUrlByRoute($route); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function setMethod($method) |
|
159
|
|
|
{ |
|
160
|
|
|
$this->method = $method; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function start() |
|
164
|
|
|
{ |
|
165
|
|
|
return new HtmlString( |
|
166
|
|
|
view($this->helper->getViewPath('form.start', $this->theme), [ |
|
167
|
|
|
'form' => $this, |
|
168
|
|
|
]) |
|
169
|
|
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function end() |
|
173
|
|
|
{ |
|
174
|
|
|
return new HtmlString( |
|
175
|
|
|
view($this->helper->getViewPath('form.start', $this->theme), [ |
|
176
|
|
|
'form' => $this, |
|
177
|
|
|
]) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
View Code Duplication |
public function field(string $name) |
|
|
|
|
|
|
182
|
|
|
{ |
|
183
|
|
|
/** @var Field $field */ |
|
184
|
|
|
$field = $this->fields->get($name); |
|
185
|
|
|
|
|
186
|
|
|
return new HtmlString( |
|
187
|
|
|
view($this->helper->getViewPath($field->getTemplate(), $this->theme), [ |
|
188
|
|
|
'field' => $field, |
|
189
|
|
|
]) |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
View Code Duplication |
public function fields() |
|
|
|
|
|
|
194
|
|
|
{ |
|
195
|
|
|
$html = ''; |
|
196
|
|
|
|
|
197
|
|
|
foreach ($this->fields as $field) { |
|
198
|
|
|
$html .= view($this->helper->getViewPath($field->getTemplate(), $this->theme), [ |
|
199
|
|
|
'field' => $field, |
|
200
|
|
|
]); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return new HtmlString($html); |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: