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
|
|
|
/** |
21
|
|
|
* Holds the amount of parameters. |
22
|
|
|
*/ |
23
|
|
|
protected $amountOfParameters = 1; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Holds the type of the validator. |
27
|
|
|
*/ |
28
|
|
|
protected $type; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Performs the comparison. |
32
|
|
|
* |
33
|
|
|
* @param mixed $value the first value to compare |
34
|
|
|
* @param mixed $parameters the values to compare |
35
|
|
|
* |
36
|
|
|
* @return boolean - true if value compares to the parameters |
37
|
|
|
*/ |
38
|
|
|
abstract protected function isValidComparison($value, $parameters); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Checks whether all given parameters are numeric. |
42
|
|
|
* |
43
|
|
|
* @return boolean - true if all values are numeric |
44
|
|
|
*/ |
45
|
7 |
|
protected function isAllNumeric() |
46
|
|
|
{ |
47
|
7 |
|
foreach (func_get_args() as $value) { |
48
|
7 |
|
if (!is_numeric($value)) { |
49
|
7 |
|
return false; |
50
|
|
|
} |
51
|
|
|
} |
52
|
7 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
15 |
|
public function isValid($value, array $parameters) |
59
|
|
|
{ |
60
|
|
|
|
61
|
15 |
|
$this->validateParameterCount($this->type, $this->amountOfParameters, $parameters); |
62
|
|
|
|
63
|
15 |
|
return in_array($value, ['', null], true) || |
64
|
15 |
|
$this->isValidComparison($value, $parameters); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
3 |
|
public function getInvalidDetails() |
71
|
|
|
{ |
72
|
3 |
|
return $this->type; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|