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