Completed
Push — master ( 955961...592efd )
by Jeroen De
08:55 queued 05:31
created

ValueValidatorObject::runSubValidator()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 3
crap 6
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
 * @license GPL-2.0+
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 = [];
41
42
	/**
43
	 * @since 0.1
44
	 *
45
	 * @var Error[]
46
	 */
47
	protected $errors = [];
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 = [];
58
59
		if ( $this->enableWhitelistRestrictions() ) {
60
			$this->valueIsAllowed( $value );
61
		}
62
63
		$this->doValidation( $value );
64
65
		if ( $this->errors === [] ) {
66
			return Result::newSuccess();
67
		} else {
68
			return Result::newError( $this->errors );
69
		}
70
	}
71
72
	/**
73
	 * Checks the value against the allowed values and prohibited values lists in case they are set.
74
	 *
75
	 * @since 0.1
76
	 *
77
	 * @param mixed $value
78
	 */
79
	protected function valueIsAllowed( $value ) {
80
		if ( $this->allowedValues !== false && !in_array( $value, $this->allowedValues, true ) ) {
81
			$this->addErrorMessage( 'Value not in whitelist' );
82
		}
83
84
		if ( $this->prohibitedValues !== false && in_array( $value, $this->prohibitedValues, true ) ) {
85
			$this->addErrorMessage( 'Value in blacklist' );
86
		}
87
	}
88
89
	/**
90
	 * @see ValueValidator::validate
91
	 *
92
	 * @since 0.1
93
	 *
94
	 * @param mixed $value
95
	 */
96
	abstract public function doValidation( $value );
97
98
	/**
99
	 * Sets the parameter definition values contained in the provided array.
100
	 * @see ParamDefinition::setArrayValues
101
	 *
102
	 * @param array $param
103
	 */
104
	public function setOptions( array $param ) {
105
		if ( $this->enableWhitelistRestrictions() ) {
106
			if ( array_key_exists( 'values', $param ) ) {
107
				$this->allowedValues = $param['values'];
108
			}
109
110
			if ( array_key_exists( 'excluding', $param ) ) {
111
				$this->prohibitedValues = $param['excluding'];
112
			}
113
		}
114
115
		$this->options = $param;
116
	}
117
118
	/**
119
	 * Registers an error message.
120
	 *
121
	 * @since 0.1
122
	 *
123
	 * @param string $errorMessage
124
	 */
125
	protected function addErrorMessage( $errorMessage ) {
126
		$this->addError( Error::newError( $errorMessage ) );
127
	}
128
129
	/**
130
	 * Registers an error.
131
	 *
132
	 * @since 0.1
133
	 *
134
	 * @param Error $error
135
	 */
136
	protected function addError( Error $error ) {
137
		$this->errors[] = $error;
138
	}
139
140
	/**
141
	 * Registers a list of errors.
142
	 *
143
	 * @since 0.1
144
	 *
145
	 * @param Error[] $errors
146
	 */
147
	protected function addErrors( array $errors ) {
148
		$this->errors = array_merge( $this->errors, $errors );
149
	}
150
151
	/**
152
	 * Runs the value through the provided ValueValidator and registers the errors.
153
	 *
154
	 * @since 0.3
155
	 *
156
	 * @param mixed $value
157
	 * @param ValueValidator $validator
158
	 * @param string|null $property
159
	 */
160
	protected function runSubValidator( $value, ValueValidator $validator, $property = null ) {
161
		foreach ( $validator->validate( $value )->getErrors() as $error ) {
162
			$this->addError( Error::newError( $error->getText(), $property ) );
163
		}
164
	}
165
166
	/**
167
	 * If the "values" and "excluding" arguments should be held into account.
168
	 *
169
	 * @since 0.1
170
	 *
171
	 * @return bool
172
	 */
173
	protected function enableWhitelistRestrictions() {
174
		return true;
175
	}
176
177
	/**
178
	 * Returns the allowed values.
179
	 *
180
	 * TODO: think about how to access set options in general and if we want to have
181
	 * whitelist and baclklist values in the validator objects to begin with.
182
	 *
183
	 * @since 0.1
184
	 *
185
	 * @return array|bool false
186
	 */
187
	public function getWhitelistedValues() {
188
		return $this->allowedValues;
189
	}
190
191
}
192