Completed
Pull Request — master (#11)
by no
05:04 queued 02:15
created

ListValidator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 8
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
C doValidation() 0 28 7
A enableWhitelistRestrictions() 0 3 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
		$validator = new RangeValidator();
34
35
		if ( array_key_exists( 'elementcount', $this->options ) ) {
36
			$range = $this->options['elementcount'];
37
38
			if ( !is_array( $range ) || count( $range ) !== 2 ) {
39
				throw new Exception( 'The elementcount option must be an array with two elements' );
40
			}
41
42
			$validator->setRange( $range[0], $range[1] );
43
		}
44
45
		// min- and maxelements options are allowed to overwrite the elementcount option!
46
		if ( array_key_exists( 'minelements', $this->options ) ) {
47
			$validator->setLowerBound( $this->options['minelements'] );
48
		}
49
		if ( array_key_exists( 'maxelements', $this->options ) ) {
50
			$validator->setUpperBound( $this->options['maxelements'] );
51
		}
52
53
		$this->runSubValidator( count( $value ), $validator, 'length' );
54
	}
55
56
	/**
57
	 * @see ValueValidatorObject::enableWhitelistRestrictions
58
	 *
59
	 * @since 0.1
60
	 *
61
	 * @return boolean
62
	 */
63
	protected function enableWhitelistRestrictions() {
64
		return false;
65
	}
66
67
}
68