|
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
|
|
|
use Valdi\Validator; |
|
15
|
|
|
use Valdi\ValidationException; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Validator to combine other validators with a logical "or". |
|
19
|
|
|
*/ |
|
20
|
|
|
class OrCombine implements ValidatorInterface { |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Holds the invalid validators. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $invalid; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Checks whether the given parameters fullfil: |
|
29
|
|
|
* - At least three given |
|
30
|
|
|
* - The first one is a Validator or a subclass of it |
|
31
|
|
|
* |
|
32
|
|
|
* @param array $parameters |
|
33
|
|
|
* the validation parameters |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function checkParameters($parameters) { |
|
36
|
|
|
if (count($parameters) < 3) { |
|
37
|
|
|
throw new ValidationException('"or" expects at least 3 parameters.'); |
|
38
|
|
|
} |
|
39
|
|
|
if (!($parameters[0] instanceof Validator)) { |
|
40
|
|
|
throw new ValidationException('"or" expects the first parameter to be a Validator or a subclass of it.'); |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function isValid($value, array $parameters) { |
|
48
|
|
|
|
|
49
|
|
|
$this->checkParameters($parameters); |
|
50
|
|
|
|
|
51
|
|
|
$validator = array_shift($parameters); |
|
|
|
|
|
|
52
|
|
|
$this->invalidDetails = array(); |
|
|
|
|
|
|
53
|
|
|
$valid = false; |
|
|
|
|
|
|
54
|
|
|
foreach ($parameters as $rules) { |
|
55
|
|
|
$failedValidations = $validator->isValidValue(array($rules), $value); |
|
56
|
|
|
foreach ($failedValidations as $failedValidation) { |
|
57
|
|
|
$this->invalidDetails[] = $failedValidation; |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
$valid = $valid || empty($failedValidations); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $valid; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
public function getInvalidDetails() { |
|
69
|
|
|
return array('or' => $this->invalidDetails); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.