Passed
Push — master ( 004041...5e2fb7 )
by Marius
03:21
created

ListValidator::doValidation()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
rs 8.5125
cc 5
eloc 14
nc 9
nop 1
1
<?php
2
3
namespace ValueValidators;
4
5
use Exception;
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
 * @author Thiemo Mättig
15
 */
16
class ListValidator extends ValueValidatorObject {
17
18
	/**
19
	 * @see ValueValidatorObject::doValidation
20
	 *
21
	 * @since 0.1
22
	 *
23
	 * @param mixed $value
24
	 *
25
	 * @throws Exception
26
	 */
27
	public function doValidation( $value ) {
28
		if ( !is_array( $value ) ) {
29
			$this->addErrorMessage( 'Not an array' );
30
			return;
31
		}
32
33
		$options = array();
34
35
		if ( array_key_exists( 'elementcount', $this->options ) ) {
36
			$options['range'] = $this->options['elementcount'];
37
		}
38
39
		if ( array_key_exists( 'minelements', $this->options ) ) {
40
			$options['lowerbound'] = $this->options['minelements'];
41
		}
42
43
		if ( array_key_exists( 'maxelements', $this->options ) ) {
44
			$options['upperbound'] = $this->options['maxelements'];
45
		}
46
47
		$validator = new RangeValidator();
48
		$validator->setOptions( $options );
49
		$this->runSubValidator( count( $value ), $validator, 'length' );
50
	}
51
52
	/**
53
	 * @see ValueValidatorObject::enableWhitelistRestrictions
54
	 *
55
	 * @since 0.1
56
	 *
57
	 * @return boolean
58
	 */
59
	protected function enableWhitelistRestrictions() {
60
		return false;
61
	}
62
63
}
64