Completed
Push — setOptions ( f85cb9 )
by no
01:59
created

ValueValidatorObject::runSubValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 2
eloc 3
nc 2
nop 3
1
<?php
2
3
namespace ValueValidators;
4
5
/**
6
 * ValueValidator that holds base validation functions for any type of object.
7
 *
8
 * @since 0.1
9
 *
10
 * @licence GNU GPL v2+
11
 * @author Jeroen De Dauw < [email protected] >
12
 */
13
abstract class ValueValidatorObject implements ValueValidator {
14
15
	/**
16
	 * A list of allowed values. This means the parameters value(s) must be in the list
17
	 * during validation. False for no restriction.
18
	 *
19
	 * @since 0.1
20
	 *
21
	 * @var array|false
22
	 */
23
	protected $allowedValues = false;
24
25
	/**
26
	 * A list of prohibited values. This means the parameters value(s) must
27
	 * not be in the list during validation. False for no restriction.
28
	 *
29
	 * @since 0.1
30
	 *
31
	 * @var array|false
32
	 */
33
	protected $prohibitedValues = false;
34
35
	/**
36
	 * @since 0.1
37
	 *
38
	 * @var array
39
	 */
40
	protected $options = array();
41
42
	/**
43
	 * @since 0.1
44
	 *
45
	 * @var Error[]
46
	 */
47
	protected $errors = array();
48
49
	/**
50
	 * @see ValueValidator::validate
51
	 *
52
	 * @param mixed $value
53
	 *
54
	 * @return Result
55
	 */
56
	final public function validate( $value ) {
57
		$this->errors = array();
58
59
		if ( $this->enableWhitelistRestrictions() ) {
60
			$this->valueIsAllowed( $value );
61
		}
62
63
		$this->doValidation( $value );
64
65
		if ( $this->errors === array() ) {
66
			return Result::newSuccess();
67
		}
68
		else {
69
			return Result::newError( $this->errors );
70
		}
71
	}
72
73
	/**
74
	 * Checks the value against the allowed values and prohibited values lists in case they are set.
75
	 *
76
	 * @since 0.1
77
	 *
78
	 * @param mixed $value
79
	 */
80
	protected function valueIsAllowed( $value ) {
81
		if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues, true ) ) {
82
			$this->addErrorMessage( 'Value not in whitelist' );
83
		}
84
85
		if ( $this->prohibitedValues !== false && in_array( $value, $this->prohibitedValues, true ) ) {
86
			$this->addErrorMessage( 'Value in blacklist' );
87
		}
88
	}
89
90
	/**
91
	 * @see ValueValidator::validate
92
	 *
93
	 * @since 0.1
94
	 *
95
	 * @param mixed $value
96
	 */
97
	public abstract function doValidation( $value );
98
99
	/**
100
	 * Sets the parameter definition values contained in the provided array.
101
	 * @see ParamDefinition::setArrayValues
102
	 *
103
	 * @param array $param
104
	 */
105
	public function setOptions( array $param ) {
106
		if ( $this->enableWhitelistRestrictions() ) {
107
			if ( array_key_exists( 'values', $param ) ) {
108
				$this->allowedValues = $param['values'];
109
			}
110
111
			if ( array_key_exists( 'excluding', $param ) ) {
112
				$this->prohibitedValues = $param['excluding'];
113
			}
114
		}
115
116
		$this->options = $param;
117
	}
118
119
	/**
120
	 * Registers an error message.
121
	 *
122
	 * @since 0.1
123
	 *
124
	 * @param string $errorMessage
125
	 */
126
	protected function addErrorMessage( $errorMessage ) {
127
		$this->addError( Error::newError( $errorMessage ) );
128
	}
129
130
	/**
131
	 * Registers an error.
132
	 *
133
	 * @since 0.1
134
	 *
135
	 * @param Error $error
136
	 */
137
	protected function addError( Error $error ) {
138
		$this->errors[] = $error;
139
	}
140
141
	/**
142
	 * Registers a list of errors.
143
	 *
144
	 * @since 0.1
145
	 *
146
	 * @param Error[] $errors
147
	 */
148
	protected function addErrors( array $errors ) {
149
		$this->errors = array_merge( $this->errors, $errors );
150
	}
151
152
	/**
153
	 * Runs the value through the provided ValueValidator and registers the errors.
154
	 *
155
	 * @since 0.3
156
	 *
157
	 * @param mixed $value
158
	 * @param ValueValidator $validator
159
	 * @param string|null $property
160
	 */
161
	protected function runSubValidator( $value, ValueValidator $validator, $property = null ) {
162
		foreach ( $validator->validate( $value )->getErrors() as $error ) {
163
			$this->addError( Error::newError( $error->getText(), $property ) );
164
		}
165
	}
166
167
	/**
168
	 * If the "values" and "excluding" arguments should be held into account.
169
	 *
170
	 * @since 0.1
171
	 *
172
	 * @return bool
173
	 */
174
	protected function enableWhitelistRestrictions() {
175
		return true;
176
	}
177
178
	/**
179
	 * Returns the allowed values.
180
	 *
181
	 * TODO: think about how to access set options in general and if we want to have
182
	 * whitelist and baclklist values in the validator objects to begin with.
183
	 *
184
	 * @since 0.1
185
	 *
186
	 * @return array|bool false
187
	 */
188
	public function getWhitelistedValues() {
189
		return $this->allowedValues;
190
	}
191
192
}
193