1 | <?php |
||
14 | class BaseElement extends \Fiv\Form\Element\Html implements ElementInterface { |
||
15 | |||
16 | /** |
||
17 | * @var null|boolean |
||
18 | */ |
||
19 | protected $validationResult = null; |
||
20 | |||
21 | /** |
||
22 | * @var \Fiv\Form\Validator\Base[] |
||
23 | */ |
||
24 | protected $validators = []; |
||
25 | |||
26 | /** |
||
27 | * @var FilterInterface[] |
||
28 | */ |
||
29 | protected $filters = []; |
||
30 | |||
31 | /** |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $text = ''; |
||
35 | |||
36 | /** |
||
37 | * @var null|string |
||
38 | */ |
||
39 | protected $value = null; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Attach validator to current element |
||
44 | * |
||
45 | * @param ValidatorInterface $validator |
||
46 | * @return $this |
||
47 | */ |
||
48 | 7 | public function addValidator(ValidatorInterface $validator) { |
|
52 | |||
53 | |||
54 | /** |
||
55 | * @return \Fiv\Form\Validator\Base[] |
||
56 | */ |
||
57 | 10 | public function getValidators() { |
|
60 | |||
61 | |||
62 | /** |
||
63 | * |
||
64 | * @param FilterInterface $filter |
||
65 | * @throws \Exception |
||
66 | * @return $this |
||
67 | */ |
||
68 | 3 | public function addFilter(FilterInterface $filter) { |
|
72 | |||
73 | |||
74 | /** |
||
75 | * @return FilterInterface[] |
||
76 | */ |
||
77 | 23 | public function getFilters() { |
|
80 | |||
81 | |||
82 | /** |
||
83 | * Alias of setAttribute('value', $value) |
||
84 | * |
||
85 | * @param $value |
||
86 | * @return $this |
||
87 | */ |
||
88 | 2 | public function setValue($value) { |
|
92 | |||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | 23 | public function getValue() { |
|
100 | |||
101 | |||
102 | /** |
||
103 | * @inheritdoc |
||
104 | */ |
||
105 | 26 | public function setAttribute($name, $value) { |
|
121 | |||
122 | |||
123 | /** |
||
124 | * Return true if element is valid |
||
125 | * @return boolean |
||
126 | */ |
||
127 | 10 | public function isValid() { |
|
128 | |||
129 | 10 | if ($this->validationResult !== null) { |
|
130 | 1 | return $this->validationResult; |
|
131 | } |
||
132 | |||
133 | 10 | $this->validationResult = true; |
|
134 | 10 | $value = $this->getValue(); |
|
135 | 10 | foreach ($this->getValidators() as $validator) { |
|
136 | 7 | $validator->flushErrors(); |
|
137 | 7 | if (!$validator->isValid($value)) { |
|
138 | 7 | $this->validationResult = false; |
|
139 | } |
||
140 | } |
||
141 | |||
142 | 10 | return $this->validationResult; |
|
143 | } |
||
144 | |||
145 | |||
146 | /** |
||
147 | * @return bool |
||
148 | */ |
||
149 | public function validate() { |
||
153 | |||
154 | |||
155 | /** |
||
156 | * @return array |
||
157 | */ |
||
158 | 6 | public function getValidatorsErrors() { |
|
159 | 6 | $errors = []; |
|
160 | 6 | foreach ($this->validators as $validator) { |
|
161 | 6 | foreach ($validator->getErrors() as $error) { |
|
162 | 6 | $errors[] = $error; |
|
163 | } |
||
164 | } |
||
165 | |||
166 | 6 | return $errors; |
|
167 | } |
||
168 | |||
169 | |||
170 | /** |
||
171 | * @inheritdoc |
||
172 | */ |
||
173 | 6 | public function render() { |
|
178 | |||
179 | |||
180 | /** |
||
181 | * @deprecated |
||
182 | * @see addValidator |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function required() { |
||
189 | |||
190 | |||
191 | /** |
||
192 | * @param string $text |
||
193 | * @return $this |
||
194 | */ |
||
195 | 14 | public function setText($text) { |
|
199 | |||
200 | |||
201 | /** |
||
202 | * @return mixed |
||
203 | */ |
||
204 | 1 | public function getText() { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $name |
||
211 | * @return $this |
||
212 | */ |
||
213 | 25 | public function setName($name) { |
|
217 | |||
218 | |||
219 | /** |
||
220 | * @return null|string |
||
221 | */ |
||
222 | 23 | public function getName() { |
|
225 | |||
226 | |||
227 | /** |
||
228 | * @param string $class |
||
229 | * @return $this |
||
230 | */ |
||
231 | 1 | public function setClass($class) { |
|
235 | |||
236 | |||
237 | /** |
||
238 | * @return null|string |
||
239 | */ |
||
240 | 1 | public function getClass() { |
|
243 | |||
244 | |||
245 | /** |
||
246 | * @param string $id |
||
247 | * @return $this |
||
248 | */ |
||
249 | public function setId($id) { |
||
253 | |||
254 | |||
255 | /** |
||
256 | * @return null|string |
||
257 | */ |
||
258 | public function getId() { |
||
261 | |||
262 | } |