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