1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Valdi package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Valdi\Validator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validator for comparing values. |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractComparator extends AbstractParametrizedValidator { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Holds the amount of parameters. |
21
|
|
|
*/ |
22
|
|
|
protected $amountOfParameters = 1; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Holds the type of the validator. |
26
|
|
|
*/ |
27
|
|
|
protected $type; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Performs the comparison. |
31
|
|
|
* |
32
|
|
|
* @param mixed $value |
33
|
|
|
* the first value to compare |
34
|
|
|
* @param mixed $parameters |
35
|
|
|
* the values to compare |
36
|
|
|
* |
37
|
|
|
* @return boolean |
38
|
|
|
* true if value compares to the parameters |
39
|
|
|
*/ |
40
|
|
|
abstract protected function isValidComparison($value, $parameters); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Checks whether all given parameters are numeric. |
44
|
|
|
* |
45
|
|
|
* @return boolean |
46
|
|
|
* true if all values are numeric |
47
|
|
|
*/ |
48
|
|
|
protected function isAllNumeric() { |
49
|
|
|
foreach (func_get_args() as $value) { |
50
|
|
|
if (!is_numeric($value)) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
public function isValid($value, array $parameters) { |
61
|
|
|
|
62
|
|
|
$this->validateParameterCount($this->type, $this->amountOfParameters, $parameters); |
63
|
|
|
|
64
|
|
|
return in_array($value, array('', null), true) || |
65
|
|
|
$this->isValidComparison($value, $parameters); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public function getInvalidDetails() { |
72
|
|
|
return $this->type; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|