1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
4
|
|
|
|
5
|
|
|
use Fiv\Form\Filter\FilterInterface; |
6
|
|
|
use Fiv\Form\Validator; |
7
|
|
|
use Fiv\Form\Validator\ValidatorInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @package Fiv\Form\Element |
11
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
12
|
|
|
*/ |
13
|
|
|
class BaseElement extends \Fiv\Form\Element\Html { |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var null|boolean |
17
|
|
|
*/ |
18
|
|
|
protected $validationResult = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Fiv\Form\Validator\Base[] |
22
|
|
|
*/ |
23
|
|
|
protected $validators = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \Fiv\Form\Filter\FilterInterface[] |
27
|
|
|
*/ |
28
|
|
|
protected $filters = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $text = ''; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var null|string |
37
|
|
|
*/ |
38
|
|
|
protected $value = null; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Attach validator to current element |
43
|
|
|
* |
44
|
|
|
* @deprecated |
45
|
|
|
* @see addValidators |
46
|
|
|
* |
47
|
|
|
* @param \Fiv\Form\Validator\ValidatorInterface[]|\Fiv\Form\Validator\ValidatorInterface $validator |
48
|
|
|
* @throws \Exception |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function addValidator($validator) { |
52
|
|
|
trigger_error('Deprecates. See addValidators', E_USER_DEPRECATED); |
53
|
|
|
if (!is_array($validator)) { |
54
|
|
|
$validator = [$validator]; |
55
|
|
|
} |
56
|
|
|
$this->addValidators($validator); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Attach validators to current element |
63
|
|
|
* |
64
|
|
|
* @param ValidatorInterface[] $validators |
65
|
|
|
* @return $this |
66
|
|
|
* @throws \Exception |
67
|
|
|
*/ |
68
|
6 |
|
public function addValidators(array $validators) { |
69
|
6 |
|
$this->validationResult = null; |
70
|
6 |
|
foreach ($validators as $validator) { |
71
|
6 |
|
if (!($validator instanceof Validator\ValidatorInterface)) { |
72
|
|
|
throw new \Exception('Invalid validator class: ' . get_class($validator)); |
73
|
|
|
} |
74
|
6 |
|
$this->validators[] = $validator; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
6 |
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return \Fiv\Form\Validator\Base[] |
84
|
|
|
*/ |
85
|
8 |
|
public function getValidators() { |
86
|
8 |
|
return $this->validators; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @deprecated |
92
|
|
|
* @see addFilters |
93
|
|
|
* |
94
|
|
|
* @param \Fiv\Form\Filter\FilterInterface|\Fiv\Form\Filter\FilterInterface[] $filter |
95
|
|
|
* @throws \Exception |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function addFilter($filter) { |
99
|
|
|
trigger_error('Deprecates. See addFilters', E_USER_DEPRECATED); |
100
|
|
|
if (!is_array($filter)) { |
101
|
|
|
$filter = [$filter]; |
102
|
|
|
} |
103
|
|
|
$this->addFilters($filter); |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Attach filters to current element |
110
|
|
|
* |
111
|
|
|
* @param FilterInterface[] $filters |
112
|
|
|
* @return $this |
113
|
|
|
* @throws \Exception |
114
|
|
|
*/ |
115
|
3 |
|
public function addFilters(array $filters) { |
116
|
3 |
|
foreach ($filters as $filter) { |
117
|
3 |
|
if (!($filter instanceof FilterInterface)) { |
118
|
|
|
throw new \Exception('Invalid filter class: ' . get_class($filter)); |
119
|
|
|
} |
120
|
3 |
|
$this->filters[] = $filter; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return \Fiv\Form\Filter\FilterInterface[] |
129
|
|
|
*/ |
130
|
16 |
|
public function getFilters() { |
131
|
16 |
|
return $this->filters; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Alias of setAttribute('value', $value) |
137
|
|
|
* |
138
|
|
|
* @param $value |
139
|
|
|
* @return $this |
140
|
|
|
*/ |
141
|
2 |
|
public function setValue($value) { |
142
|
2 |
|
$this->setAttribute('value', $value); |
143
|
2 |
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
14 |
|
public function getValue() { |
151
|
14 |
|
return $this->value; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @inheritdoc |
157
|
|
|
*/ |
158
|
18 |
|
public function setAttribute($name, $value) { |
159
|
18 |
|
if ($name === 'value') { |
160
|
|
|
|
161
|
16 |
|
$this->validationResult = null; |
162
|
|
|
|
163
|
|
|
# apply filters to the value |
164
|
16 |
|
$filters = $this->getFilters(); |
165
|
16 |
|
foreach ($filters as $filter) { |
166
|
3 |
|
$value = $filter->apply($value); |
167
|
|
|
} |
168
|
|
|
|
169
|
16 |
|
$this->value = $value; |
170
|
|
|
} |
171
|
|
|
|
172
|
18 |
|
return parent::setAttribute($name, $value); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return true if element is valid |
178
|
|
|
* @return boolean |
179
|
|
|
*/ |
180
|
8 |
|
public function validate() { |
181
|
|
|
|
182
|
8 |
|
if ($this->validationResult !== null) { |
183
|
|
|
return $this->validationResult; |
184
|
|
|
} |
185
|
|
|
|
186
|
8 |
|
$this->validationResult = true; |
187
|
8 |
|
$value = $this->getValue(); |
188
|
8 |
|
foreach ($this->getValidators() as $validator) { |
189
|
6 |
|
$validator->flushErrors(); |
190
|
6 |
|
if (!$validator->isValid($value)) { |
191
|
6 |
|
$this->validationResult = false; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
8 |
|
return $this->validationResult; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @return array |
201
|
|
|
*/ |
202
|
|
|
public function getValidatorsErrors() { |
203
|
|
|
$errors = []; |
204
|
|
|
foreach ($this->validators as $validator) { |
205
|
|
|
$errors = array_merge($errors, $validator->getErrors()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $errors; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @inheritdoc |
214
|
|
|
*/ |
215
|
3 |
|
public function render() { |
216
|
3 |
|
$value = $this->getValue(); |
217
|
3 |
|
$this->attributes['value'] = htmlentities($value, ENT_QUOTES); |
218
|
3 |
|
return parent::render(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @deprecated |
224
|
|
|
* @see addValidators |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function required() { |
228
|
|
|
trigger_error('Deprecated', E_USER_DEPRECATED); |
229
|
|
|
return $this->addValidators([new \Fiv\Form\Validator\Required()]); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param string $text |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
10 |
|
public function setText($text) { |
238
|
10 |
|
$this->text = $text; |
239
|
10 |
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* @return mixed |
245
|
|
|
*/ |
246
|
|
|
public function getText() { |
247
|
|
|
return $this->text; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param string $name |
253
|
|
|
* @return $this |
254
|
|
|
*/ |
255
|
17 |
|
public function setName($name) { |
256
|
17 |
|
$this->setAttribute('name', $name); |
257
|
17 |
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return null|string |
263
|
|
|
*/ |
264
|
16 |
|
public function getName() { |
265
|
16 |
|
return $this->getAttribute('name'); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param string $class |
271
|
|
|
* @return $this |
272
|
|
|
*/ |
273
|
1 |
|
public function setClass($class) { |
274
|
1 |
|
$this->setAttribute('class', $class); |
275
|
1 |
|
return $this; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return null|string |
281
|
|
|
*/ |
282
|
1 |
|
public function getClass() { |
283
|
1 |
|
return $this->getAttribute('class'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $id |
289
|
|
|
* @return $this |
290
|
|
|
*/ |
291
|
|
|
public function setId($id) { |
292
|
|
|
$this->setAttribute('id', $id); |
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @return null|string |
299
|
|
|
*/ |
300
|
|
|
public function getId() { |
301
|
|
|
return $this->getAttribute('id'); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
} |