1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\Filter\FilterInterface; |
6
|
|
|
use Fiv\Form\FormData; |
7
|
|
|
use Fiv\Form\Validator\ValidatorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @method addFilters |
11
|
|
|
* @package Fiv\Form\Element |
12
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
13
|
|
|
*/ |
14
|
|
|
abstract class BaseElement extends Html implements ElementInterface { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ValidatorInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected $validators = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var FilterInterface[] |
23
|
|
|
*/ |
24
|
|
|
protected $filters = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $text = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var null|string |
33
|
|
|
*/ |
34
|
|
|
protected $value = null; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @inheritdoc |
39
|
|
|
*/ |
40
|
|
|
public abstract function handle(FormData $data); |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Attach validator to current element |
45
|
|
|
* |
46
|
|
|
* @param ValidatorInterface $validator |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
7 |
|
public function addValidator(ValidatorInterface $validator) { |
50
|
7 |
|
$this->validators[] = $validator; |
51
|
7 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return ValidatorInterface[] |
57
|
|
|
*/ |
58
|
11 |
|
public function getValidators() { |
59
|
11 |
|
return $this->validators; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* |
65
|
|
|
* @param FilterInterface $filter |
66
|
|
|
* @throws \Exception |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
4 |
|
public function addFilter(FilterInterface $filter) { |
70
|
4 |
|
$this->filters[] = $filter; |
71
|
4 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return FilterInterface[] |
77
|
|
|
*/ |
78
|
36 |
|
public function getFilters() { |
79
|
36 |
|
return $this->filters; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Alias of setAttribute('value', $value) |
85
|
|
|
* |
86
|
|
|
* @param $value |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
6 |
|
public function setValue($value) { |
90
|
6 |
|
$this->setAttribute('value', $value); |
91
|
6 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
40 |
|
public function getValue() { |
99
|
40 |
|
return $this->value; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
44 |
|
public function setAttribute($name, $value) { |
107
|
44 |
|
if ($name === 'value') { |
108
|
|
|
|
109
|
|
|
# apply filters to the value |
110
|
36 |
|
$filters = $this->getFilters(); |
111
|
36 |
|
foreach ($filters as $filter) { |
112
|
4 |
|
$value = $filter->apply($value); |
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
36 |
|
$this->value = $value; |
116
|
|
|
} |
117
|
|
|
|
118
|
44 |
|
return parent::setAttribute($name, $value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Return true if element is valid |
124
|
|
|
* @return boolean |
125
|
|
|
*/ |
126
|
11 |
|
public function isValid() { |
127
|
11 |
|
$value = $this->getValue(); |
128
|
11 |
|
foreach ($this->getValidators() as $validator) { |
129
|
7 |
|
if (!$validator->isValid($value)) { |
130
|
7 |
|
return false; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
11 |
|
return true; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
6 |
|
public function getValidatorsErrors() { |
142
|
6 |
|
$errors = []; |
143
|
6 |
|
foreach ($this->validators as $validator) { |
144
|
6 |
|
foreach ($validator->getErrors() as $error) { |
145
|
6 |
|
$errors[] = $error; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
6 |
|
return $errors; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @inheritdoc |
155
|
|
|
*/ |
156
|
7 |
|
public function render() { |
157
|
7 |
|
$value = $this->getValue(); |
158
|
7 |
|
$this->attributes['value'] = htmlentities($value, ENT_QUOTES); |
159
|
7 |
|
return parent::render(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param string $text |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
17 |
|
public function setText($text) { |
168
|
17 |
|
$this->text = $text; |
169
|
17 |
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @return mixed |
175
|
|
|
*/ |
176
|
1 |
|
public function getText() { |
177
|
1 |
|
return $this->text; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param string $name |
183
|
|
|
* @return $this |
184
|
|
|
*/ |
185
|
42 |
|
public function setName($name) { |
186
|
42 |
|
$this->setAttribute('name', $name); |
187
|
42 |
|
return $this; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @return null|string |
193
|
|
|
*/ |
194
|
38 |
|
public function getName() { |
195
|
38 |
|
return $this->getAttribute('name'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param string $class |
201
|
|
|
* @return $this |
202
|
|
|
*/ |
203
|
1 |
|
public function setClass($class) { |
204
|
1 |
|
$this->setAttribute('class', $class); |
205
|
1 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return null|string |
211
|
|
|
*/ |
212
|
1 |
|
public function getClass() { |
213
|
1 |
|
return $this->getAttribute('class'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $id |
219
|
|
|
* @return $this |
220
|
|
|
*/ |
221
|
|
|
public function setId($id) { |
222
|
|
|
$this->setAttribute('id', $id); |
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return null|string |
229
|
|
|
*/ |
230
|
|
|
public function getId() { |
231
|
|
|
return $this->getAttribute('id'); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
} |