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
|
|
|
* Get the name of field. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
public function getName(): string |
51
|
|
|
{ |
52
|
|
|
return $this->name; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the template of field. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getTemplate(): string |
61
|
|
|
{ |
62
|
|
|
return $this->template; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get the label of field. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getLabel(): string |
71
|
|
|
{ |
72
|
|
|
return $this->form->getHelper()->getLabel($this->name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the placeholder of field. |
77
|
|
|
* |
78
|
|
|
* @return null|string |
79
|
|
|
*/ |
80
|
|
|
public function getPlaceholder() |
81
|
|
|
{ |
82
|
|
|
return $this->form->getHelper()->getPlaceholder($this->name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the text of field. |
87
|
|
|
* |
88
|
|
|
* @return null|string |
89
|
|
|
*/ |
90
|
|
|
public function getText() |
91
|
|
|
{ |
92
|
|
|
return $this->form->getHelper()->getText($this->name); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set the name of field. |
97
|
|
|
* |
98
|
|
|
* @param string $name |
99
|
|
|
*/ |
100
|
|
|
public function setName(string $name) |
101
|
|
|
{ |
102
|
|
|
$this->name = $name; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the rules of field. |
107
|
|
|
* |
108
|
|
|
* @param array $rules |
109
|
|
|
*/ |
110
|
|
|
public function setRules(array $rules) |
111
|
|
|
{ |
112
|
|
|
$this->rules = $rules; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set the form of field. |
117
|
|
|
* |
118
|
|
|
* @param Form $form |
119
|
|
|
*/ |
120
|
|
|
public function setForm(Form $form) |
121
|
|
|
{ |
122
|
|
|
$this->form = $form; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function attributes() |
126
|
|
|
{ |
127
|
|
|
$html = ''; |
128
|
|
|
|
129
|
|
|
$ruleMap = $this->ruleMap; |
130
|
|
|
|
131
|
|
|
// If "required" is not exists, add it for default. |
132
|
|
|
if (!isset($ruleMap['required'])) { |
133
|
|
|
$ruleMap['required'] = 'required'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
foreach ($this->ruleMap as $rule => $map) |
137
|
|
|
{ |
138
|
|
|
// If the html rule is a boolean attribute, add this. |
139
|
|
|
if (is_string($map)) { |
140
|
|
|
if (in_array($rule, $this->rules)) { |
141
|
|
|
$html .= " {$map}"; |
142
|
|
|
} |
143
|
|
|
} else { |
144
|
|
|
// If the html rule has a variable, get it and use. |
145
|
|
|
list($mapAttribute, $index) = $map; |
146
|
|
|
|
147
|
|
|
foreach ($this->rules as $fieldRule) { |
148
|
|
|
if (preg_match("/^{$rule}\:/", $fieldRule)) { |
149
|
|
|
$explode = explode(':', $fieldRule); |
150
|
|
|
|
151
|
|
|
$html .= " {$mapAttribute}=\"{$explode[$index]}\""; |
152
|
|
|
break; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return new HtmlString($html); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|