|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Fiv\Form\Element; |
|
4
|
|
|
|
|
5
|
|
|
use Fiv\Form\Filter\FilterInterface; |
|
6
|
|
|
use Fiv\Form\Validator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @package Fiv\Form\Element |
|
10
|
|
|
* @author Ivan Shcherbak <[email protected]> 2016 |
|
11
|
|
|
* @method $this setName($name); |
|
12
|
|
|
* @method string getName(); |
|
13
|
|
|
* @method $this setClass($class); |
|
14
|
|
|
* @method string getClass(); |
|
15
|
|
|
* @method $this setId($id); |
|
16
|
|
|
* @method string getId(); |
|
17
|
|
|
*/ |
|
18
|
|
|
class BaseElement extends \Fiv\Form\Element\Html { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var null|boolean |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $validationResult = null; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var \Fiv\Form\Validator\Base[] |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $validators = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var \Fiv\Form\Filter\FilterInterface[] |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filters = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $text = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var null|string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $value = null; |
|
44
|
|
|
|
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Attach validator to current element |
|
48
|
|
|
* |
|
49
|
|
|
* @param \Fiv\Form\Validator\ValidatorInterface[]|\Fiv\Form\Validator\ValidatorInterface $validator |
|
50
|
|
|
* @throws \Exception |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function addValidator($validator) { |
|
54
|
5 |
|
if (!is_array($validator)) { |
|
55
|
5 |
|
$validator = [$validator]; |
|
56
|
|
|
} |
|
57
|
5 |
|
$this->validationResult = null; |
|
58
|
5 |
|
foreach ($validator as $validatorClass) { |
|
59
|
5 |
|
if (!($validatorClass instanceof Validator\ValidatorInterface)) { |
|
60
|
|
|
throw new \Exception('Invalid validator class: ' . get_class($validatorClass)); |
|
61
|
|
|
} |
|
62
|
5 |
|
$this->validators[] = $validatorClass; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $text |
|
71
|
|
|
* @return $this |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function setText($text) { |
|
74
|
9 |
|
$this->text = $text; |
|
75
|
9 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getText() { |
|
83
|
|
|
return $this->text; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Attach filter to current element |
|
89
|
|
|
* |
|
90
|
|
|
* @param \Fiv\Form\Filter\FilterInterface|\Fiv\Form\Filter\FilterInterface[] $filter |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
* @return $this |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function addFilter($filter) { |
|
95
|
1 |
|
if (!is_array($filter)) { |
|
96
|
1 |
|
$filter = [$filter]; |
|
97
|
|
|
} |
|
98
|
1 |
|
foreach ($filter as $filterClass) { |
|
99
|
1 |
|
if (!($filterClass instanceof FilterInterface)) { |
|
100
|
|
|
throw new \Exception('Invalid filter class: ' . get_class($filterClass)); |
|
101
|
|
|
} |
|
102
|
1 |
|
$this->filters[] = $filterClass; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
1 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $value |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
12 |
|
public function setValue($value) { |
|
114
|
12 |
|
$this->validationResult = null; |
|
115
|
|
|
|
|
116
|
12 |
|
$filters = $this->getFilters(); |
|
117
|
12 |
|
foreach ($filters as $filter) { |
|
118
|
1 |
|
$value = $filter->apply($value); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
12 |
|
$this->value = $value; |
|
122
|
12 |
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return \Fiv\Form\Filter\FilterInterface[] |
|
128
|
|
|
*/ |
|
129
|
12 |
|
public function getFilters() { |
|
130
|
12 |
|
return $this->filters; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return true if element is valid |
|
136
|
|
|
* @return boolean |
|
137
|
|
|
*/ |
|
138
|
6 |
|
public function validate() { |
|
139
|
|
|
|
|
140
|
6 |
|
if ($this->validationResult !== null) { |
|
141
|
|
|
return $this->validationResult; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
$this->validationResult = true; |
|
145
|
6 |
|
$value = $this->getValue(); |
|
146
|
6 |
|
foreach ($this->getValidators() as $validator) { |
|
147
|
5 |
|
$validator->flushErrors(); |
|
148
|
5 |
|
if (!$validator->isValid($value)) { |
|
149
|
5 |
|
$this->validationResult = false; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
return $this->validationResult; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return string |
|
159
|
|
|
*/ |
|
160
|
8 |
|
public function getValue() { |
|
161
|
8 |
|
return $this->value; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* @return \Fiv\Form\Validator\Base[] |
|
167
|
|
|
*/ |
|
168
|
6 |
|
public function getValidators() { |
|
169
|
6 |
|
return $this->validators; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return array |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getValidatorsErrors() { |
|
177
|
|
|
$errors = []; |
|
178
|
|
|
foreach ($this->validators as $validator) { |
|
179
|
|
|
$errors = array_merge($errors, $validator->getErrors()); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $errors; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @deprecated use addValidator(new \Fiv\Form\Validator\Required()) instead |
|
188
|
|
|
* @see addValidator |
|
189
|
|
|
* @return $this |
|
190
|
|
|
*/ |
|
191
|
|
|
public function required() { |
|
192
|
|
|
trigger_error('Deprecated', E_USER_DEPRECATED); |
|
193
|
|
|
return $this->addValidator(new \Fiv\Form\Validator\Required()); |
|
194
|
|
|
} |
|
195
|
|
|
} |