|
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\ValidationException; |
|
15
|
|
|
use Valdi\Validator; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Validator for array data, be it maps or lists. |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class AbstractArray implements ValidatorInterface { |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Holds the invalid values. |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $invalidDetails; |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Checks whether the given values are of the expected array data. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $values |
|
33
|
|
|
* the potential array values to check |
|
34
|
|
|
* @param Validator $validator |
|
35
|
|
|
* the validator to check with |
|
36
|
|
|
* @param array $rules |
|
37
|
|
|
* the rules which the array data must fulfill |
|
38
|
|
|
* |
|
39
|
|
|
* @return boolean |
|
40
|
|
|
* true if all the $values are valid, else false with the invalid details set |
|
41
|
|
|
*/ |
|
42
|
|
|
protected abstract function isValidArray($values, Validator $validator, array $rules); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function isValid($value, array $parameters) { |
|
48
|
|
|
if (count($parameters) !== 2) { |
|
49
|
|
|
throw new ValidationException('Expecting two parameters.'); |
|
50
|
|
|
} |
|
51
|
|
|
if (!($parameters[0] instanceof Validator)) { |
|
52
|
|
|
throw new ValidationException('Expecting the first parameter to be an instance of a Validator.'); |
|
53
|
|
|
} |
|
54
|
|
|
return in_array($value, ['', null], true) || |
|
55
|
|
|
$this->isValidArray($value, $parameters[0], $parameters[1]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|