1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* User: delboy1978uk |
4
|
|
|
* Date: 19/11/2016 |
5
|
|
|
* Time: 21:41 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Del\Form\Field; |
9
|
|
|
|
10
|
|
|
use Del\Form\Collection\FilterCollection; |
11
|
|
|
use Del\Form\Collection\ValidatorCollection; |
12
|
|
|
use Del\Form\Filter\FilterInterface; |
13
|
|
|
use Del\Form\Validator\ValidatorInterface; |
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
abstract class FieldAbstract implements FieldInterface |
17
|
|
|
{ |
18
|
|
|
/** @var string $name */ |
19
|
|
|
private $name; |
20
|
|
|
|
21
|
|
|
/** @var string $id */ |
22
|
|
|
private $id; |
23
|
|
|
|
24
|
|
|
/** @var string $class */ |
25
|
|
|
private $class; |
26
|
|
|
|
27
|
|
|
/** @var FilterCollection $filterCollection */ |
28
|
|
|
private $filterCollection; |
29
|
|
|
|
30
|
|
|
/** @var ValidatorCollection $validatorCollection */ |
31
|
|
|
private $validatorCollection; |
32
|
|
|
|
33
|
|
|
private $value; |
34
|
|
|
|
35
|
|
|
/** @var array $errorMessages */ |
36
|
|
|
private $errorMessages; |
37
|
|
|
|
38
|
|
|
/** @var string $label */ |
39
|
|
|
private $label; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
abstract public function getTag(); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return mixed |
48
|
|
|
*/ |
49
|
|
|
abstract public function getTagType(); |
50
|
|
|
|
51
|
9 |
|
public function __construct($name, $value = null) |
52
|
|
|
{ |
53
|
9 |
|
$this->filterCollection = new FilterCollection(); |
54
|
9 |
|
$this->validatorCollection = new ValidatorCollection(); |
55
|
9 |
|
$this->setName($name); |
56
|
9 |
|
is_null($value) ? null : $this->setValue($value); |
57
|
9 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
8 |
|
public function getName() |
63
|
|
|
{ |
64
|
8 |
|
return $this->name; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $name |
69
|
|
|
* @return FieldAbstract |
70
|
|
|
*/ |
71
|
9 |
|
public function setName($name) |
72
|
|
|
{ |
73
|
9 |
|
$this->name = $name; |
74
|
9 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
4 |
|
public function getId() |
81
|
|
|
{ |
82
|
4 |
|
return $this->id; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $id |
87
|
|
|
* @return FieldAbstract |
88
|
|
|
*/ |
89
|
6 |
|
public function setId($id) |
90
|
|
|
{ |
91
|
6 |
|
$this->id = $id; |
92
|
6 |
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
4 |
|
public function getClass() |
99
|
|
|
{ |
100
|
4 |
|
return $this->class ?: 'form-control'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $class |
105
|
|
|
* @return FieldAbstract |
106
|
|
|
*/ |
107
|
2 |
|
public function setClass($class) |
108
|
|
|
{ |
109
|
2 |
|
$this->class = $class; |
110
|
2 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return mixed |
115
|
|
|
*/ |
116
|
7 |
|
public function getValue() |
117
|
|
|
{ |
118
|
7 |
|
return $this->value; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param mixed $value |
123
|
|
|
* @return FieldAbstract |
124
|
|
|
*/ |
125
|
7 |
|
public function setValue($value) |
126
|
|
|
{ |
127
|
7 |
|
$this->value = $value; |
128
|
7 |
|
$this->filterValue(); |
129
|
7 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param ValidatorInterface $validator |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
4 |
|
public function addValidator(ValidatorInterface $validator) |
137
|
|
|
{ |
138
|
4 |
|
$this->validatorCollection->append($validator); |
139
|
4 |
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return ValidatorCollection |
144
|
|
|
*/ |
145
|
1 |
|
public function getValidators() |
146
|
|
|
{ |
147
|
1 |
|
return $this->validatorCollection; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param FilterInterface $filter |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
2 |
|
public function addFilter(FilterInterface $filter) |
155
|
|
|
{ |
156
|
2 |
|
$this->filterCollection->append($filter); |
157
|
2 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return FilterCollection |
162
|
|
|
*/ |
163
|
1 |
|
public function getFilters() |
164
|
|
|
{ |
165
|
1 |
|
return $this->filterCollection; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param mixed $value |
|
|
|
|
170
|
|
|
* @return bool |
171
|
|
|
* @throws Exception If validation of $value is impossible |
172
|
|
|
*/ |
173
|
4 |
|
public function isValid() |
174
|
|
|
{ |
175
|
4 |
|
$this->errorMessages = []; |
176
|
4 |
|
$this->validatorCollection->rewind(); |
177
|
4 |
|
while ($this->validatorCollection->valid()) { |
178
|
3 |
|
$this->checkForErrors($this->validatorCollection->current()); |
179
|
3 |
|
$this->validatorCollection->next(); |
180
|
3 |
|
} |
181
|
4 |
|
$count = count($this->errorMessages); |
182
|
4 |
|
return $count == 0; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param FieldInterface $field |
|
|
|
|
187
|
|
|
*/ |
188
|
3 |
|
private function checkForErrors(ValidatorInterface $validator) |
189
|
|
|
{ |
190
|
3 |
|
$value = $this->getValue(); |
191
|
|
|
|
192
|
3 |
|
if (!$validator->isValid($value)) { |
193
|
3 |
|
$this->errorMessages[] = $validator->getMessages(); |
194
|
3 |
|
} |
195
|
3 |
|
} |
196
|
|
|
|
197
|
7 |
|
private function filterValue() |
198
|
|
|
{ |
199
|
7 |
|
$value = $this->value; |
200
|
7 |
|
$this->filterCollection->rewind(); |
201
|
7 |
|
while ($this->filterCollection->valid()) { |
202
|
1 |
|
$value = $this->filterCollection->current()->filter($value); |
203
|
1 |
|
$this->filterCollection->next(); |
204
|
1 |
|
} |
205
|
7 |
|
$this->filterCollection->rewind(); |
206
|
7 |
|
$this->value = $value; |
207
|
7 |
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return array |
211
|
|
|
*/ |
212
|
2 |
|
public function getMessages() |
213
|
|
|
{ |
214
|
2 |
|
return array_values($this->errorMessages); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
3 |
|
public function getLabel() |
221
|
|
|
{ |
222
|
3 |
|
return $this->label; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param string $label |
227
|
|
|
* @return FieldAbstract |
228
|
|
|
*/ |
229
|
1 |
|
public function setLabel($label) |
230
|
|
|
{ |
231
|
1 |
|
$this->label = $label; |
232
|
1 |
|
return $this; |
233
|
|
|
} |
234
|
|
|
} |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.