1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AnilcanCakir\Former; |
4
|
|
|
|
5
|
|
|
use AnilcanCakir\Former\Contracts\FormerHelper; |
|
|
|
|
6
|
|
|
use AnilcanCakir\Former\Fields\Field; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\HtmlString; |
10
|
|
|
|
11
|
|
|
class Form |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The form fields. |
15
|
|
|
* |
16
|
|
|
* @var Collection |
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
|
|
|
* Form constructor. |
43
|
|
|
* |
44
|
|
|
* @param Model|null $model |
45
|
|
|
* @param FormerHelper $formerHelper |
46
|
|
|
*/ |
47
|
|
|
public function __construct(Model $model = null, FormerHelper $formerHelper) |
48
|
|
|
{ |
49
|
|
|
$this->model = $model; |
50
|
|
|
$this->helper = $formerHelper; |
51
|
|
|
|
52
|
|
|
$this->fields = new Collection(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the model fields. |
57
|
|
|
* |
58
|
|
|
* @return Collection |
59
|
|
|
*/ |
60
|
|
|
public function getFields(): Collection |
61
|
|
|
{ |
62
|
|
|
return $this->fields; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the model fields. |
67
|
|
|
* |
68
|
|
|
* @param string $input |
69
|
|
|
* @param string $name |
70
|
|
|
* @param array $rules |
71
|
|
|
*/ |
72
|
|
|
public function addField($input, $name, array $rules) |
73
|
|
|
{ |
74
|
|
|
/** @var Field $field */ |
75
|
|
|
$field = new $input(); |
76
|
|
|
$field->setForm($this); |
77
|
|
|
$field->setName($name); |
78
|
|
|
$field->setRules($rules); |
79
|
|
|
|
80
|
|
|
$this->fields->put($name, $field); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the form model. |
85
|
|
|
* |
86
|
|
|
* @return Model |
87
|
|
|
*/ |
88
|
|
|
public function getModel(): Model |
89
|
|
|
{ |
90
|
|
|
return $this->model; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function getHelper(): FormerHelper |
94
|
|
|
{ |
95
|
|
|
return $this->helper; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Set the form model. |
100
|
|
|
* |
101
|
|
|
* @param Model $model |
102
|
|
|
*/ |
103
|
|
|
public function setModel(Model $model) |
104
|
|
|
{ |
105
|
|
|
$this->model = $model; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Set the form theme. |
110
|
|
|
* |
111
|
|
|
* @param string|null $theme |
112
|
|
|
*/ |
113
|
|
|
public function setTheme($theme) |
114
|
|
|
{ |
115
|
|
|
$this->theme = $theme; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function start() |
119
|
|
|
{ |
120
|
|
|
return new HtmlString( |
121
|
|
|
view($this->helper->getViewPath('form.start', $this->theme), [ |
122
|
|
|
'form' => $this |
123
|
|
|
]) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function end() |
128
|
|
|
{ |
129
|
|
|
return new HtmlString( |
130
|
|
|
view($this->helper->getViewPath('form.start', $this->theme), [ |
131
|
|
|
'form' => $this |
132
|
|
|
]) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function field(string $name) |
137
|
|
|
{ |
138
|
|
|
/** @var Field $field */ |
139
|
|
|
$field = $this->fields->get($name); |
140
|
|
|
|
141
|
|
|
return new HtmlString( |
142
|
|
|
view($this->helper->getViewPath($field->getTemplate(), $this->theme), [ |
143
|
|
|
'field' => $field |
144
|
|
|
]) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are 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.php
However, as
OtherDir/Foo.php
does 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: