|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ValueValidators; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* ValueValidator that validates a list of values. |
|
9
|
|
|
* |
|
10
|
|
|
* @since 0.1 |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GNU GPL v2+ |
|
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
14
|
|
|
*/ |
|
15
|
|
|
class ListValidator extends ValueValidatorObject { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @see ValueValidatorObject::doValidation |
|
19
|
|
|
* |
|
20
|
|
|
* @since 0.1 |
|
21
|
|
|
* |
|
22
|
|
|
* @param mixed $value |
|
23
|
|
|
* |
|
24
|
|
|
* @throws InvalidArgumentException |
|
25
|
|
|
*/ |
|
26
|
|
|
public function doValidation( $value ) { |
|
27
|
|
|
if ( !is_array( $value ) ) { |
|
28
|
|
|
$this->addErrorMessage( 'Not an array' ); |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$validator = new RangeValidator(); |
|
33
|
|
|
|
|
34
|
|
|
if ( array_key_exists( 'elementcount', $this->options ) ) { |
|
35
|
|
|
$range = $this->options['elementcount']; |
|
36
|
|
|
|
|
37
|
|
|
if ( !is_array( $range ) || count( $range ) !== 2 ) { |
|
38
|
|
|
throw new InvalidArgumentException( 'The elementcount option must be an array with two elements' ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$validator->setRange( $range[0], $range[1] ); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// min- and maxelements options are allowed to overwrite the elementcount option! |
|
45
|
|
|
if ( array_key_exists( 'minelements', $this->options ) ) { |
|
46
|
|
|
$validator->setLowerBound( $this->options['minelements'] ); |
|
47
|
|
|
} |
|
48
|
|
|
if ( array_key_exists( 'maxelements', $this->options ) ) { |
|
49
|
|
|
$validator->setUpperBound( $this->options['maxelements'] ); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->runSubValidator( count( $value ), $validator, 'length' ); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @see ValueValidatorObject::enableWhitelistRestrictions |
|
57
|
|
|
* |
|
58
|
|
|
* @since 0.1 |
|
59
|
|
|
* |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function enableWhitelistRestrictions() { |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
|