Index: trunk/extensions/Validator/Validator.class.php |
— | — | @@ -22,8 +22,6 @@ |
23 | 23 | */ |
24 | 24 | final class Validator { |
25 | 25 | |
26 | | - // TODO: add lower/upper case nullification |
27 | | - |
28 | 26 | /** |
29 | 27 | * @var boolean Indicates whether parameters not found in the criteria list |
30 | 28 | * should be stored in case they are not accepted. The default is false. |
— | — | @@ -35,7 +33,7 @@ |
36 | 34 | * should be stored in case they are not accepted. The default is false. |
37 | 35 | */ |
38 | 36 | public static $accumulateParameterErrors = false; |
39 | | - |
| 37 | + |
40 | 38 | /** |
41 | 39 | * @var array Holder for the validation functions. |
42 | 40 | */ |
— | — | @@ -43,11 +41,10 @@ |
44 | 42 | 'in_array' => 'in_array', |
45 | 43 | 'in_range' => array( 'ValidatorFunctions', 'in_range' ), |
46 | 44 | 'is_numeric' => 'is_numeric', |
| 45 | + 'is_integer' => array( 'ValidatorFunctions', 'is_integer' ), |
47 | 46 | 'not_empty' => array( 'ValidatorFunctions', 'not_empty' ), |
48 | 47 | 'all_in_array' => array( 'ValidatorFunctions', 'all_in_array' ), |
49 | 48 | 'any_in_array' => array( 'ValidatorFunctions', 'any_in_array' ), |
50 | | - 'all_str_in_array' => array( 'ValidatorFunctions', 'all_str_in_array' ), |
51 | | - 'any_str_in_array' => array( 'ValidatorFunctions', 'any_str_in_array' ), |
52 | 49 | ); |
53 | 50 | |
54 | 51 | private $parameterInfo; |
— | — | @@ -107,25 +104,31 @@ |
108 | 105 | if ( array_key_exists( $paramName, $this->rawParameters ) ) { |
109 | 106 | |
110 | 107 | $paramValue = $this->rawParameters[$paramName]; |
| 108 | + $this->cleanParameter( $paramName, $paramValue ); |
111 | 109 | $validationErrors = $this->validateParameter( $paramName, $paramValue ); |
112 | 110 | |
113 | 111 | if ( count( $validationErrors ) == 0 ) { |
| 112 | + // If the validation succeeded, add the parameter to the list of valid ones. |
114 | 113 | $this->valid[$paramName] = $paramValue; |
115 | 114 | } |
116 | 115 | else { |
| 116 | + // If the validation failed, add the parameter to the list of invalid ones and add the errors to the error list. |
117 | 117 | $this->invalid[$paramName] = $paramValue; |
118 | 118 | foreach ( $validationErrors as $error ) { |
119 | 119 | $this->errors[] = array( 'error' => $error, 'name' => $paramName ); |
120 | 120 | } |
121 | 121 | } |
122 | 122 | } |
123 | | - else { // If the user did not provide a value for this parameter, set the default if there is one. |
124 | | - if ( array_key_exists( 'default', $paramInfo ) ) { |
125 | | - $this->valid[$paramName] = $paramInfo['default']; |
126 | | - } |
127 | | - else { // If there is no default, the parameter must be provided, so add an error. |
| 123 | + else { |
| 124 | + // If the parameter is required, add a new error of type 'missing'. |
| 125 | + if ( array_key_exists( 'required', $paramInfo ) && $paramInfo['required'] ) { |
128 | 126 | $this->errors[] = array( 'error' => array( 'missing' ), 'name' => $paramName ); |
129 | 127 | } |
| 128 | + else { |
| 129 | + // Set the default value (or default 'default value' if none is provided), and ensure the type is correct. |
| 130 | + $this->valid[$paramName] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : ''; |
| 131 | + $this->setFinalType($this->valid[$paramName], $paramInfo); |
| 132 | + } |
130 | 133 | } |
131 | 134 | } |
132 | 135 | |
— | — | @@ -162,6 +165,48 @@ |
163 | 166 | } |
164 | 167 | |
165 | 168 | /** |
| 169 | + * |
| 170 | + * @param $name |
| 171 | + * @param $value |
| 172 | + * |
| 173 | + * @return unknown_type |
| 174 | + */ |
| 175 | + private function cleanParameter( $name, &$value ) { |
| 176 | + if (array_key_exists('default', $this->parameterInfo[$name])) { |
| 177 | + $this->setFinalType($this->parameterInfo[$name]['default'], $this->parameterInfo[$name]); |
| 178 | + } |
| 179 | + |
| 180 | + // Ensure there is a criteria array. |
| 181 | + if (! array_key_exists('criteria', $this->parameterInfo[$name] )) { |
| 182 | + $this->parameterInfo[$name]['criteria'] = array(); |
| 183 | + } |
| 184 | + |
| 185 | + if ( array_key_exists( 'type', $this->parameterInfo[$name] ) ) { |
| 186 | + // Add type specific criteria. |
| 187 | + switch(strtolower($this->parameterInfo[$name]['type'])) { |
| 188 | + case 'integer': |
| 189 | + $this->parameterInfo[$name]['criteria']['is_integer'] = array(); |
| 190 | + break; |
| 191 | + case 'list' : case 'list-string' : |
| 192 | + if (! array_key_exists('delimiter', $this->parameterInfo[$name])) $this->parameterInfo[$name]['delimiter'] = ','; |
| 193 | + } |
| 194 | + |
| 195 | + // Remove redundant spaces. |
| 196 | + switch(strtolower($this->parameterInfo[$name]['type'])) { |
| 197 | + case 'list' : case 'list-string': |
| 198 | + // TODO: make sure the delimiter doesn't mess up the regex when it's a special char. |
| 199 | + $value = preg_replace('/((\s)*' . |
| 200 | + $this->parameterInfo[$name]['delimiter'] . |
| 201 | + '(\s)*)/', $this->parameterInfo[$name]['delimiter'], $value); |
| 202 | + break; |
| 203 | + default : |
| 204 | + $value = trim ($value); |
| 205 | + break; |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + /** |
166 | 211 | * Valides the provided parameter by matching the value against the criteria for the name. |
167 | 212 | * |
168 | 213 | * @param string $name |
— | — | @@ -171,37 +216,68 @@ |
172 | 217 | */ |
173 | 218 | private function validateParameter( $name, $value ) { |
174 | 219 | $errors = array(); |
175 | | - |
176 | | - if ( array_key_exists( 'criteria', $this->parameterInfo[$name] ) ) { |
177 | | - foreach ( $this->parameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) { |
| 220 | + |
| 221 | + // Split list types into arrays. |
| 222 | + switch(strtolower($this->parameterInfo[$name]['type'])) { |
| 223 | + case 'list' : case 'list-string' : |
| 224 | + $value = explode($this->parameterInfo[$name]['delimiter'], $value); |
| 225 | + break; |
| 226 | + } |
| 227 | + |
| 228 | + // Go through all criteria. |
| 229 | + foreach ( $this->parameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) { |
| 230 | + // Get the validation function. If there is no matching function, throw an exception. |
| 231 | + if (array_key_exists($criteriaName, self::$validationFunctions)) { |
178 | 232 | $validationFunction = self::$validationFunctions[$criteriaName]; |
179 | | - $arguments = array( $value ); |
180 | | - if ( count( $criteriaArgs ) > 0 ) $arguments[] = $criteriaArgs; |
181 | | - $isValid = call_user_func_array( $validationFunction, $arguments ); |
| 233 | + } |
| 234 | + else { |
| 235 | + throw new Exception( 'There is no validation function for criteria type ' . $criteriaName ); |
| 236 | + } |
| 237 | + |
| 238 | + // Build up the array of parameters to be passed to call_user_func_array. |
| 239 | + $arguments = array( $value ); |
| 240 | + if ( count( $criteriaArgs ) > 0 ) $arguments[] = $criteriaArgs; |
| 241 | + |
| 242 | + // Call the validation function and store the result. |
| 243 | + $isValid = call_user_func_array( $validationFunction, $arguments ); |
182 | 244 | |
183 | | - if ( ! $isValid ) { |
184 | | - $errors[] = array( $criteriaName, $criteriaArgs, $value ); |
185 | | - if ( ! self::$accumulateParameterErrors ) break; |
186 | | - } |
| 245 | + // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
| 246 | + if ( ! $isValid ) { |
| 247 | + $errors[] = array( $criteriaName, $criteriaArgs, $value ); |
| 248 | + if ( ! self::$accumulateParameterErrors ) break; |
187 | 249 | } |
188 | 250 | } |
189 | 251 | |
190 | 252 | return $errors; |
191 | 253 | } |
| 254 | + |
| 255 | + /** |
| 256 | + * Ensures the type of the value is correct. |
| 257 | + * |
| 258 | + * @param $value |
| 259 | + * @param array $info |
| 260 | + */ |
| 261 | + private function setFinalType(&$value, array $info) { |
| 262 | + if (array_key_exists('type', $info)) { |
| 263 | + switch(strtolower($info['type'])) { |
| 264 | + case 'list-string' : |
| 265 | + if (is_array($value)) { |
| 266 | + $delimiter = array_key_exists('delimiter', $info) ? $info['delimiter'] : ','; |
| 267 | + $value = implode($delimiter, $value); |
| 268 | + } |
| 269 | + break; |
| 270 | + } |
| 271 | + } |
| 272 | + } |
192 | 273 | |
193 | 274 | /** |
194 | 275 | * Changes the invalid parameters to their default values, and changes their state to valid. |
195 | 276 | */ |
196 | 277 | public function correctInvalidParams() { |
197 | 278 | foreach ( $this->invalid as $paramName => $paramValue ) { |
198 | | - |
199 | | - if ( array_key_exists( 'default', $this->parameterInfo[$paramName] ) ) { |
200 | | - unset( $this->invalid[$paramName] ); |
201 | | - $this->valid[$paramName] = $this->parameterInfo[$paramName]['default']; |
202 | | - } |
203 | | - else { |
204 | | - throw new Exception( 'The default value for parameter ' . $paramName . ' is not set.' ); |
205 | | - } |
| 279 | + unset( $this->invalid[$paramName] ); |
| 280 | + $this->valid[$paramName] = array_key_exists( 'default', $this->parameterInfo[$paramName] ) ? $this->parameterInfo[$paramName]['default'] : ''; |
| 281 | + $this->setFinalType($this->valid[$paramName], $this->parameterInfo[$paramName]); |
206 | 282 | } |
207 | 283 | } |
208 | 284 | |
Index: trunk/extensions/Validator/Validator_Functions.php |
— | — | @@ -14,6 +14,7 @@ |
15 | 15 | |
16 | 16 | /** |
17 | 17 | * Class holding variouse static methods for the validation of parameters that have to comply to cetrain criteria. |
| 18 | + * Functions are called by Validator with the parameters $value, $arguments, where $arguments is optional. |
18 | 19 | * |
19 | 20 | * @ingroup Validator |
20 | 21 | * |
— | — | @@ -22,9 +23,9 @@ |
23 | 24 | final class ValidatorFunctions { |
24 | 25 | |
25 | 26 | /** |
26 | | - * Returns whether the provided value, which must be a number, is within a certain range. |
| 27 | + * Returns whether the provided value, which must be a number, is within a certain range. Upper bound not included. |
27 | 28 | * |
28 | | - * @param string $value |
| 29 | + * @param $value |
29 | 30 | * @param array $limits |
30 | 31 | * |
31 | 32 | * @return boolean |
— | — | @@ -32,19 +33,30 @@ |
33 | 34 | public static function in_range( $value, array $limits ) { |
34 | 35 | if ( ! is_numeric( $value ) ) return false; |
35 | 36 | $value = (int)$value; |
36 | | - return ( $value >= $limits[0] && $value <= $limits[1] ) || ( $value <= $limits[0] And $value >= $limits[1] ); |
| 37 | + return ( $value >= $limits[0] && $value < $limits[1] ) || ( $value < $limits[0] And $value >= $limits[1] ); |
37 | 38 | } |
38 | 39 | |
39 | 40 | /** |
40 | 41 | * Returns whether the string value is not empty. Not empty is defined as having at least one character after trimming. |
41 | 42 | * |
42 | | - * @param string $value |
| 43 | + * @param $value |
43 | 44 | * |
44 | 45 | * @return boolean |
45 | 46 | */ |
46 | 47 | public static function not_empty( $value ) { |
47 | 48 | return strlen( trim( $value ) ) > 0; |
48 | 49 | } |
| 50 | + |
| 51 | + /** |
| 52 | + * Returns whether a variable is an integer or an integer string. Uses the native PHP function. |
| 53 | + * |
| 54 | + * @param $value |
| 55 | + * |
| 56 | + * @return boolean |
| 57 | + */ |
| 58 | + public static function is_integer( $value ) { |
| 59 | + return is_numeric($value); // TODO: int check |
| 60 | + } |
49 | 61 | |
50 | 62 | /** |
51 | 63 | * Returns if all items of the first array are present in the second one. |
— | — | @@ -57,7 +69,7 @@ |
58 | 70 | public static function all_in_array( array $needles, array $haystack ) { |
59 | 71 | $true = true; |
60 | 72 | foreach ( $needles as $needle ) { |
61 | | - if ( ! in_array( trim( $needle ), $haystack ) ) { |
| 73 | + if ( ! in_array( $needle , $haystack ) ) { |
62 | 74 | $true = false; |
63 | 75 | break; |
64 | 76 | } |
— | — | @@ -76,39 +88,11 @@ |
77 | 89 | public static function any_in_array( array $needles, array $haystack ) { |
78 | 90 | $true = false; |
79 | 91 | foreach ( $needles as $needle ) { |
80 | | - if ( in_array( trim( $needle ), $haystack ) ) { |
| 92 | + if ( in_array( $needle , $haystack ) ) { |
81 | 93 | $true = true; |
82 | 94 | break; |
83 | 95 | } |
84 | 96 | } |
85 | 97 | return $true; |
86 | 98 | } |
87 | | - |
88 | | - /** |
89 | | - * Returns if all items in the string are present in the array. |
90 | | - * The first element of the $args array should be the delimieter, |
91 | | - * the second one an array holding the haystack. |
92 | | - * |
93 | | - * @param string $needles |
94 | | - * @param array $args |
95 | | - * |
96 | | - * @return boolean |
97 | | - */ |
98 | | - public static function all_str_in_array( $needles, array $args ) { |
99 | | - return self::all_in_array( explode( $args[0], $needles ), $args[1] ); |
100 | | - } |
101 | | - |
102 | | - /** |
103 | | - * Returns if any items in the string are present in the array. |
104 | | - * The first element of the $args array should be the delimieter, |
105 | | - * the second one an array holding the haystack. |
106 | | - * |
107 | | - * @param string $needles |
108 | | - * @param array $args |
109 | | - * |
110 | | - * @return boolean |
111 | | - */ |
112 | | - public static function any_str_in_array( $needles, array $args ) { |
113 | | - return self::any_in_array( explode( $args[0], $needles ), $args[1] ); |
114 | | - } |
115 | 99 | } |
Index: trunk/extensions/Validator/Validator_Manager.php |
— | — | @@ -25,12 +25,12 @@ |
26 | 26 | private $errors = array(); |
27 | 27 | |
28 | 28 | /** |
29 | | - * Validates the provided parameters, and corrects them depedning on the error level. |
| 29 | + * Validates the provided parameters, and corrects them depending on the error level. |
30 | 30 | * |
31 | | - * @param array $rawParameters |
32 | | - * @param array $parameterInfo |
| 31 | + * @param array $rawParameters The raw parameters, as provided by the user. |
| 32 | + * @param array $parameterInfo Array containing the parameter definitions, which are needed for validation and defaulting. |
33 | 33 | * |
34 | | - * @return boolean Indicates whether the regular output should be shown or not. |
| 34 | + * @return array or false The valid parameters or false when the output should not be shown. |
35 | 35 | */ |
36 | 36 | public function manageMapparameters( array $rawParameters, array $parameterInfo ) { |
37 | 37 | global $egValidatorErrorLevel; |
— | — | @@ -85,6 +85,9 @@ |
86 | 86 | case 'is_numeric' : |
87 | 87 | $errors[] = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $error['name'] ); |
88 | 88 | break; |
| 89 | + case 'is_integer' : |
| 90 | + $errors[] = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] ); |
| 91 | + break; |
89 | 92 | case 'in_array' : case 'all_in_array' : case 'all_str_in_array' : |
90 | 93 | $items = $error['error'][0] == 'all_str_in_array' ? $error['error'][1][1] : $error['error'][1]; |
91 | 94 | $itemsText = $wgLang->listToText( $items ); |
Index: trunk/extensions/Validator/Validator.i18n.php |
— | — | @@ -26,6 +26,7 @@ |
27 | 27 | 'validator_error_required_missing' => 'The required parameter $1 is not provided.', |
28 | 28 | |
29 | 29 | 'validator_error_must_be_number' => 'Parameter $1 can only be a number.', |
| 30 | + 'validator_error_must_be_integer' => 'Parameter $1 can only be an intger.', |
30 | 31 | 'validator_error_invalid_range' => 'Parameter $1 must be between $2 and $3.', |
31 | 32 | 'maps_error_accepts_only' => 'Parameter $1 only accepts {{PLURAL:$3|this value|these values}}: $2.', |
32 | 33 | ); |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | die( 'Not an entry point.' ); |
26 | 26 | } |
27 | 27 | |
28 | | -define( 'Validator_VERSION', '0.1 a2' ); |
| 28 | +define( 'Validator_VERSION', '0.1 a3' ); |
29 | 29 | |
30 | 30 | // Constants indicating the strictness of the parameter validation. |
31 | 31 | define( 'Validator_ERRORS_NONE', 0 ); |