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