|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AnilcanCakir\Former\Fields; |
|
4
|
|
|
|
|
5
|
|
|
use AnilcanCakir\Former\Form; |
|
6
|
|
|
use Illuminate\Support\HtmlString; |
|
7
|
|
|
|
|
8
|
|
|
abstract class Field |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* The form of field. |
|
12
|
|
|
* |
|
13
|
|
|
* @var Form |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $form; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The name of field. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $name; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The template of field. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $template = 'fields.input'; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The rules of field. |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $rules; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The map of rules. |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $ruleMap = []; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The default map of rules. |
|
47
|
|
|
* |
|
48
|
|
|
* @var array |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $defaultRulesMap = [ |
|
51
|
|
|
'required' => 'required' |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the name of field. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getName(): string |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->name; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the template of field. |
|
66
|
|
|
* |
|
67
|
|
|
* @return string |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getTemplate(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->template; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the label of field. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getLabel(): string |
|
80
|
|
|
{ |
|
81
|
|
|
// Check the "labels" first. |
|
82
|
|
|
if ($trans = $this->form->getHelper()->trans("validation.labels.{$this->name}")) { |
|
83
|
|
|
return $trans; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if ($trans = $this->form->getHelper()->trans("validation.attributes.{$this->name}")) { |
|
87
|
|
|
return $trans; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $this->name; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get the placeholder of field. |
|
95
|
|
|
* |
|
96
|
|
|
* @return null|string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getPlaceholder() |
|
99
|
|
|
{ |
|
100
|
|
|
if ($trans = $this->form->getHelper()->trans("validation.placeholders.{$this->name}")) { |
|
101
|
|
|
return $trans; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Get the text of field. |
|
109
|
|
|
* |
|
110
|
|
|
* @return null|string |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getText() |
|
113
|
|
|
{ |
|
114
|
|
|
if ($trans = $this->form->getHelper()->trans("validation.texts.{$this->name}")) { |
|
115
|
|
|
return $trans; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set the name of field. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $name |
|
125
|
|
|
*/ |
|
126
|
|
|
public function setName(string $name) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->name = $name; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Set the rules of field. |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $rules |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setRules(array $rules) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->rules = $rules; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Set the form of field. |
|
143
|
|
|
* |
|
144
|
|
|
* @param Form $form |
|
145
|
|
|
*/ |
|
146
|
|
|
public function setForm(Form $form) |
|
147
|
|
|
{ |
|
148
|
|
|
$this->form = $form; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Get html of attributes. |
|
153
|
|
|
* |
|
154
|
|
|
* @return HtmlString |
|
155
|
|
|
*/ |
|
156
|
|
|
public function attributes() |
|
157
|
|
|
{ |
|
158
|
|
|
$html = ''; |
|
159
|
|
|
|
|
160
|
|
|
foreach ($this->getRuleMaps() as $rule => $map) { |
|
161
|
|
|
// If the html rule is a boolean attribute, add this. |
|
162
|
|
|
if (is_string($map)) { |
|
163
|
|
|
$html .= $this->getHtmlOfBooleanAttribute($map, $rule); |
|
164
|
|
|
} else { |
|
165
|
|
|
// If the html rule has a variable, get it and use. |
|
166
|
|
|
$html .= $this->getHtmlOfAttribute($map, $rule); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return new HtmlString($html); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Get default and field rule maps. |
|
175
|
|
|
* |
|
176
|
|
|
* @return array |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function getRuleMaps() |
|
179
|
|
|
{ |
|
180
|
|
|
return array_merge( |
|
181
|
|
|
$this->defaultRulesMap, $this->ruleMap |
|
182
|
|
|
); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Get html of boolean attribute. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $map |
|
189
|
|
|
* @param string $rule |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function getHtmlOfBooleanAttribute($map, $rule): string |
|
193
|
|
|
{ |
|
194
|
|
|
if (in_array($rule, $this->rules)) { |
|
195
|
|
|
return " {$map}"; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return ''; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Get html of attribute. |
|
203
|
|
|
* |
|
204
|
|
|
* @param array $map |
|
205
|
|
|
* @param string $rule |
|
206
|
|
|
* @return string |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function getHtmlOfAttribute($map, $rule): string |
|
209
|
|
|
{ |
|
210
|
|
|
list($mapAttribute, $index) = $map; |
|
211
|
|
|
|
|
212
|
|
|
foreach ($this->rules as $fieldRule) { |
|
213
|
|
|
if (preg_match("/^{$rule}\:/", $fieldRule)) { |
|
214
|
|
|
$explode = explode(':', $fieldRule); |
|
215
|
|
|
|
|
216
|
|
|
return " {$mapAttribute}=\"{$explode[$index]}\""; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return ''; |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|