1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Util |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Pimf\Util; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Validator |
13
|
|
|
* |
14
|
|
|
* @package Util |
15
|
|
|
* @author Gjero Krsteski <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class Validator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $errors = array(); |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var \Pimf\Param |
26
|
|
|
*/ |
27
|
|
|
protected $attributes; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param \Pimf\Param $attributes |
31
|
|
|
*/ |
32
|
|
|
public function __construct(\Pimf\Param $attributes) |
33
|
|
|
{ |
34
|
|
|
$this->attributes = $attributes; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function getErrors() |
41
|
|
|
{ |
42
|
|
|
return $this->errors; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
|
|
public function isValid() |
49
|
|
|
{ |
50
|
|
|
return empty($this->errors); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* check to see if valid email address |
55
|
|
|
* |
56
|
|
|
* @param string $field |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
public function email($field) |
61
|
|
|
{ |
62
|
|
|
return (filter_var(trim($this->get($field)), FILTER_VALIDATE_EMAIL) !== false) ?: $this->error($field, |
63
|
|
|
__FUNCTION__); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Check is a valid IP. |
68
|
|
|
* |
69
|
|
|
* @param $field |
70
|
|
|
* |
71
|
|
|
* @return bool |
72
|
|
|
*/ |
73
|
|
|
public function ip($field) |
74
|
|
|
{ |
75
|
|
|
return (filter_var(trim($this->get($field)), FILTER_VALIDATE_IP) !== false) ?: $this->error($field, |
76
|
|
|
__FUNCTION__); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Check is a valid URL. |
81
|
|
|
* |
82
|
|
|
* @param $field |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function url($field) |
87
|
|
|
{ |
88
|
|
|
return (filter_var(trim($this->get($field)), FILTER_VALIDATE_URL) !== false) ?: $this->error($field, |
89
|
|
|
__FUNCTION__); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check to see if two fields are equal. |
94
|
|
|
* |
95
|
|
|
* @param string $field1 |
96
|
|
|
* @param string $field2 |
97
|
|
|
* @param bool $caseInsensitive |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function compare($field1, $field2, $caseInsensitive = false) |
102
|
|
|
{ |
103
|
|
|
$field1value = $this->get($field1); |
104
|
|
|
$field2value = $this->get($field2); |
105
|
|
|
|
106
|
|
|
$valid = (strcmp($field1value, $field2value) == 0); |
107
|
|
|
|
108
|
|
|
if ($caseInsensitive) { |
109
|
|
|
$valid = (strcmp(strtolower($field1value), strtolower($field2value)) == 0); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return ($valid === true) ?: $this->error($field1 . "|" . $field2, __FUNCTION__); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check to see if the length of a field is between two numbers |
117
|
|
|
* |
118
|
|
|
* @param string $field |
119
|
|
|
* @param int $min |
120
|
|
|
* @param int $max |
121
|
|
|
* @param bool $inclusive |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function lengthBetween($field, $min, $max, $inclusive = false) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$fieldValue = strlen(trim($this->get($field))); |
128
|
|
|
|
129
|
|
|
$valid = ($fieldValue <= $max && $fieldValue >= $min); |
130
|
|
|
|
131
|
|
|
if (!$inclusive) { |
132
|
|
|
$valid = ($fieldValue < $max && $fieldValue > $min); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return ($valid === true) ?: $this->error($field, __FUNCTION__); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Check to see if there is punctuation |
140
|
|
|
* |
141
|
|
|
* @param string $field |
142
|
|
|
* |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function punctuation($field) |
146
|
|
|
{ |
147
|
|
|
return (preg_match("/[^\w\s\p{P}]/", '' . $this->get($field)) > 0) ? $this->error($field, __FUNCTION__) : true; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* length functions on a field takes <, >, ==, <=, and >= as operators. |
152
|
|
|
* |
153
|
|
|
* @param string $field |
154
|
|
|
* @param string $operator |
155
|
|
|
* @param int $length |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function length($field, $operator, $length) |
160
|
|
|
{ |
161
|
|
|
return $this->middleware($field, strlen(trim($this->get($field))), $operator, $length); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Number value functions takes <, >, ==, <=, and >= as operators. |
166
|
|
|
* |
167
|
|
|
* @param string $field |
168
|
|
|
* @param string $operator |
169
|
|
|
* @param string|int $value |
170
|
|
|
* |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
|
public function value($field, $operator, $value) |
174
|
|
|
{ |
175
|
|
|
return $this->middleware($field, $this->get($field), $operator, $value); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if a number value is between $max and $min |
180
|
|
|
* |
181
|
|
|
* @param string $field |
182
|
|
|
* @param int $min |
183
|
|
|
* @param int $max |
184
|
|
|
* @param bool $inclusive |
185
|
|
|
* |
186
|
|
|
* @return bool |
187
|
|
|
*/ |
188
|
|
View Code Duplication |
public function valueBetween($field, $min, $max, $inclusive = false) |
|
|
|
|
189
|
|
|
{ |
190
|
|
|
$fieldValue = $this->get($field); |
191
|
|
|
|
192
|
|
|
$valid = ($fieldValue <= $max && $fieldValue >= $min); |
193
|
|
|
|
194
|
|
|
if (!$inclusive) { |
195
|
|
|
$valid = ($fieldValue < $max && $fieldValue > $min); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return ($valid === true) ?: $this->error($field, __FUNCTION__); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Check if a field contains only decimal digit |
203
|
|
|
* |
204
|
|
|
* @param string $field |
205
|
|
|
* |
206
|
|
|
* @return bool |
207
|
|
|
*/ |
208
|
|
|
public function digit($field) |
209
|
|
|
{ |
210
|
|
|
return (ctype_digit((string)$this->get($field)) === true) ?: $this->error($field, __FUNCTION__); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Check if a field contains only alphabetic characters |
215
|
|
|
* |
216
|
|
|
* @param string $field |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
public function alpha($field) |
221
|
|
|
{ |
222
|
|
|
return (ctype_alpha((string)$this->get($field)) === true) ?: $this->error($field, __FUNCTION__); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Check if a field contains only alphanumeric characters |
227
|
|
|
* |
228
|
|
|
* @param string $field |
229
|
|
|
* |
230
|
|
|
* @return bool |
231
|
|
|
*/ |
232
|
|
|
public function alphaNumeric($field) |
233
|
|
|
{ |
234
|
|
|
return (ctype_alnum((string)$this->get($field)) === true) ?: $this->error($field, __FUNCTION__); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Check if field is a date by specified format. |
239
|
|
|
* |
240
|
|
|
* @param string $field |
241
|
|
|
* @param string $format Find formats here http://www.php.net/manual/en/function.date.php |
242
|
|
|
* |
243
|
|
|
* @return boolean |
244
|
|
|
*/ |
245
|
|
|
public function date($field, $format) |
246
|
|
|
{ |
247
|
|
|
$fieldValue = $this->get($field); |
248
|
|
|
|
249
|
|
|
try { |
250
|
|
|
|
251
|
|
|
$date = new \DateTime($fieldValue); |
252
|
|
|
|
253
|
|
|
return $fieldValue === $date->format($format); |
254
|
|
|
} catch (\Exception $exception) { |
255
|
|
|
return $this->error($field, __FUNCTION__); |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* @param string $field |
261
|
|
|
* @param int $error |
262
|
|
|
* |
263
|
|
|
* @return boolean |
264
|
|
|
*/ |
265
|
|
|
protected function error($field, $error) |
266
|
|
|
{ |
267
|
|
|
$this->errors = array_merge_recursive($this->errors, array($field => $error)); |
268
|
|
|
|
269
|
|
|
return false; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param string $attribute |
274
|
|
|
* |
275
|
|
|
* @return string |
276
|
|
|
* @throws \OutOfBoundsException If attribute not at range |
277
|
|
|
*/ |
278
|
|
|
protected function get($attribute) |
279
|
|
|
{ |
280
|
|
|
if (!$value = $this->attributes->get($attribute, null, false)) { |
281
|
|
|
throw new \OutOfBoundsException('no attribute with name "' . $attribute . '" set'); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
return $value; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param string $fieldName |
289
|
|
|
* @param string $comparing |
290
|
|
|
* @param string $operator |
291
|
|
|
* @param string|integer $expecting |
292
|
|
|
* |
293
|
|
|
* @return bool |
294
|
|
|
*/ |
295
|
|
|
protected function middleware($fieldName, $comparing, $operator, $expecting) |
296
|
|
|
{ |
297
|
|
|
if (in_array($operator, array("<", ">", "==", "<=", ">="), true)) { |
298
|
|
|
$func = function($a,$b) use ($operator) { |
299
|
|
|
switch ($operator){ |
300
|
|
|
case "<": |
301
|
|
|
return ($a < $b); |
302
|
|
|
case ">": |
303
|
|
|
return ($a > $b); |
304
|
|
|
case "==": |
305
|
|
|
return ($a == $b); |
306
|
|
|
case ">=": |
307
|
|
|
return ($a >= $b); |
308
|
|
|
case "<=": |
309
|
|
|
return ($a <= $b); |
310
|
|
|
} |
311
|
|
|
}; |
312
|
|
|
|
313
|
|
|
return ($func($comparing, $expecting) === true) ?: $this->error($fieldName, $operator); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return false; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.