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