Index: trunk/extensions/Validator/Validator.class.php |
— | — | @@ -19,6 +19,8 @@ |
20 | 20 | * @ingroup Validator |
21 | 21 | * |
22 | 22 | * @author Jeroen De Dauw |
| 23 | + * |
| 24 | + * TODO: add native suport for default parameters as present in Maps 0.6. |
23 | 25 | */ |
24 | 26 | final class Validator { |
25 | 27 | |
— | — | @@ -49,7 +51,7 @@ |
50 | 52 | /** |
51 | 53 | * @var array Holder for the validation functions. |
52 | 54 | */ |
53 | | - private static $validationFunctions = array( |
| 55 | + private static $mValidationFunctions = array( |
54 | 56 | 'in_array' => array( 'ValidatorFunctions', 'in_array' ), |
55 | 57 | 'in_range' => array( 'ValidatorFunctions', 'in_range' ), |
56 | 58 | 'is_numeric' => 'is_numeric', |
— | — | @@ -62,7 +64,7 @@ |
63 | 65 | /** |
64 | 66 | * @var array Holder for the list validation functions. |
65 | 67 | */ |
66 | | - private static $listValidationFunctions = array( |
| 68 | + private static $mListValidationFunctions = array( |
67 | 69 | 'item_count' => array( 'ValidatorFunctions', 'has_item_count' ), |
68 | 70 | 'unique_items' => array( 'ValidatorFunctions', 'has_unique_items' ), |
69 | 71 | ); |
— | — | @@ -70,7 +72,7 @@ |
71 | 73 | /** |
72 | 74 | * @var array Holder for the formatting functions. |
73 | 75 | */ |
74 | | - private static $outputFormats = array( |
| 76 | + private static $mOutputFormats = array( |
75 | 77 | 'array' => array( 'ValidatorFormats', 'format_array' ), |
76 | 78 | 'list' => array( 'ValidatorFormats', 'format_list' ), |
77 | 79 | 'boolean' => array( 'ValidatorFormats', 'format_boolean' ), |
— | — | @@ -80,23 +82,25 @@ |
81 | 83 | 'filtered_array' => array( 'ValidatorFormats', 'format_filtered_array' ), |
82 | 84 | ); |
83 | 85 | |
84 | | - private $parameterInfo; |
85 | | - private $rawParameters = array(); |
| 86 | + private $mParameterInfo; |
| 87 | + |
| 88 | + private $mRawParameters = array(); |
86 | 89 | |
87 | | - private $parameters = array(); |
88 | | - private $valid = array(); |
89 | | - private $invalid = array(); |
90 | | - private $unknown = array(); |
| 90 | + private $mParameters = array(); |
| 91 | + private $mValidParams = array(); |
| 92 | + private $mInvalidParams = array(); |
| 93 | + private $mUnknownParams = array(); |
91 | 94 | |
92 | | - private $errors = array(); |
| 95 | + private $mErrors = array(); |
93 | 96 | |
94 | 97 | /** |
95 | 98 | * Sets the parameter criteria, used to valiate the parameters. |
96 | 99 | * |
97 | 100 | * @param array $parameterInfo |
| 101 | + * @param array $defaultParams |
98 | 102 | */ |
99 | 103 | public function setParameterInfo( array $parameterInfo ) { |
100 | | - $this->parameterInfo = $parameterInfo; |
| 104 | + $this->mParameterInfo = $parameterInfo; |
101 | 105 | } |
102 | 106 | |
103 | 107 | /** |
— | — | @@ -105,53 +109,79 @@ |
106 | 110 | * @param array $parameters |
107 | 111 | */ |
108 | 112 | public function setParameters( array $parameters ) { |
109 | | - $this->rawParameters = $parameters; |
| 113 | + $this->mRawParameters = $parameters; |
110 | 114 | } |
111 | 115 | |
112 | 116 | /** |
| 117 | + * Determine all parameter names and value, and take care of default (nameless) |
| 118 | + * parameters, by turning them into named ones. |
| 119 | + * |
| 120 | + * @param array $rawParams |
| 121 | + * @param array $defaultParams |
| 122 | + */ |
| 123 | + public function parseAndSetParams( array $rawParams, array $defaultParams = array() ) { |
| 124 | + $parameters = array(); |
| 125 | + |
| 126 | + foreach( $rawParams as $arg ) { |
| 127 | + $parts = explode( '=', $arg ); |
| 128 | + if ( count( $parts ) == 1 ) { |
| 129 | + if ( count( $defaultParams ) > 0 ) { |
| 130 | + $defaultParam = array_shift( $defaultParams ); |
| 131 | + $parameters[$defaultParam] = trim( $parts[0] ); |
| 132 | + } |
| 133 | + } else { |
| 134 | + $name = strtolower( trim( array_shift( $parts ) ) ); |
| 135 | + $parameters[$name] = trim( implode( $parts ) ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + $this->setParameters( $parameters ); |
| 140 | + } |
| 141 | + |
| 142 | + /** |
113 | 143 | * Valides the raw parameters, and allocates them as valid, invalid or unknown. |
114 | 144 | * Errors are collected, and can be retrieved via getErrors. |
115 | 145 | * |
116 | | - * @return boolean Indicates whether there where no errors. |
| 146 | + * @return boolean Indicates whether there where NO errors. |
117 | 147 | */ |
118 | 148 | public function validateParameters() { |
119 | 149 | // Loop through all the user provided parameters, and destinguise between those that are allowed and those that are not. |
120 | | - foreach ( $this->rawParameters as $paramName => $paramValue ) { |
| 150 | + foreach ( $this->mRawParameters as $paramName => $paramValue ) { |
121 | 151 | // Attempt to get the main parameter name (takes care of aliases). |
122 | | - $mainName = self::getMainParamName( $paramName, $this->parameterInfo ); |
123 | | - // If the parameter is found in the list of allowed ones, add it to the $parameters array. |
| 152 | + $mainName = self::getMainParamName( $paramName ); |
| 153 | + // If the parameter is found in the list of allowed ones, add it to the $mParameters array. |
124 | 154 | if ( $mainName ) { |
125 | 155 | // Check for parameter overriding. In most cases, this has already largely been taken care off, |
126 | 156 | // in the form of later parameters overriding earlier ones. This is not true for different aliases though. |
127 | | - if ( ! array_key_exists( $mainName, $this->parameters ) || self::$acceptOverriding ) { |
128 | | - $this->parameters[$mainName] = $paramValue; |
| 157 | + if ( !array_key_exists( $mainName, $this->mParameters ) || self::$acceptOverriding ) { |
| 158 | + $this->mParameters[$mainName] = $paramValue; |
129 | 159 | } |
130 | 160 | else { |
131 | | - $this->errors[] = array( 'type' => 'unknown', 'name' => $mainName ); |
| 161 | + $this->errors[] = array( 'type' => 'override', 'name' => $mainName ); |
132 | 162 | } |
133 | 163 | } |
134 | | - else { // If the parameter is not found in the list of allowed ones, add an item to the $this->errors array. |
135 | | - if ( self::$storeUnknownParameters ) $this->unknown[$paramName] = $paramValue; |
136 | | - $this->errors[] = array( 'type' => 'unknown', 'name' => $paramName ); |
| 164 | + else { // If the parameter is not found in the list of allowed ones, add an item to the $this->mErrors array. |
| 165 | + if ( self::$storeUnknownParameters ) $this->mUnknownParams[$paramName] = $paramValue; |
| 166 | + $this->mErrors[] = array( 'type' => 'unknown', 'name' => $paramName ); |
137 | 167 | } |
138 | 168 | } |
139 | 169 | |
140 | 170 | // Loop through the list of allowed parameters. |
141 | | - foreach ( $this->parameterInfo as $paramName => $paramInfo ) { |
| 171 | + foreach ( $this->mParameterInfo as $paramName => $paramInfo ) { |
142 | 172 | // If the user provided a value for this parameter, validate and handle it. |
143 | | - if ( array_key_exists( $paramName, $this->parameters ) ) { |
| 173 | + if ( array_key_exists( $paramName, $this->mParameters ) ) { |
144 | 174 | |
145 | | - $paramValue = $this->parameters[$paramName]; |
| 175 | + $paramValue = $this->mParameters[$paramName]; |
146 | 176 | $this->cleanParameter( $paramName, $paramValue ); |
147 | 177 | |
148 | 178 | if ( $this->validateParameter( $paramName, $paramValue ) ) { |
149 | 179 | // If the validation succeeded, add the parameter to the list of valid ones. |
150 | | - $this->valid[$paramName] = $paramValue; |
151 | | - $this->setOutputTypes( $this->valid[$paramName], $paramInfo ); |
| 180 | + $this->mValidParams[$paramName] = $paramValue; |
| 181 | + $this->setOutputTypes( $this->mValidParams[$paramName], $paramInfo ); |
152 | 182 | } |
153 | 183 | else { |
154 | 184 | // If the validation failed, add the parameter to the list of invalid ones. |
155 | | - $this->invalid[$paramName] = $paramValue; |
| 185 | + $this->mInvalidParams[$paramName] = $paramValue; |
156 | 186 | } |
157 | 187 | } |
158 | 188 | else { |
— | — | @@ -161,13 +191,13 @@ |
162 | 192 | } |
163 | 193 | else { |
164 | 194 | // Set the default value (or default 'default value' if none is provided), and ensure the type is correct. |
165 | | - $this->valid[$paramName] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : ''; |
166 | | - $this->setOutputTypes( $this->valid[$paramName], $paramInfo ); |
| 195 | + $this->mValidParams[$paramName] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : ''; |
| 196 | + $this->setOutputTypes( $this->mValidParams[$paramName], $paramInfo ); |
167 | 197 | } |
168 | 198 | } |
169 | 199 | } |
170 | 200 | |
171 | | - return count( $this->errors ) == 0; |
| 201 | + return count( $this->mErrors ) == 0; |
172 | 202 | } |
173 | 203 | |
174 | 204 | /** |
— | — | @@ -175,23 +205,20 @@ |
176 | 206 | * when it is not recognized as main parameter or alias. |
177 | 207 | * |
178 | 208 | * @param string $paramName |
179 | | - * @param array $allowedParms |
180 | 209 | * |
181 | 210 | * @return string |
182 | 211 | */ |
183 | | - private function getMainParamName( $paramName, array $allowedParms ) { |
| 212 | + private function getMainParamName( $paramName ) { |
184 | 213 | $result = false; |
185 | 214 | |
186 | | - if ( array_key_exists( $paramName, $allowedParms ) ) { |
| 215 | + if ( array_key_exists( $paramName, $this->mParameterInfo ) ) { |
187 | 216 | $result = $paramName; |
188 | 217 | } |
189 | 218 | else { |
190 | | - foreach ( $allowedParms as $name => $data ) { |
191 | | - if ( array_key_exists( 'aliases', $data ) ) { |
192 | | - if ( in_array( $paramName, $data['aliases'] ) ) { |
193 | | - $result = $name; |
194 | | - break; |
195 | | - } |
| 219 | + foreach ( $this->mParameterInfo as $name => $data ) { |
| 220 | + if ( array_key_exists( 'aliases', $data ) && in_array( $paramName, $data['aliases'] ) ) { |
| 221 | + $result = $name; |
| 222 | + break; |
196 | 223 | } |
197 | 224 | } |
198 | 225 | } |
— | — | @@ -207,29 +234,34 @@ |
208 | 235 | */ |
209 | 236 | private function cleanParameter( $name, &$value ) { |
210 | 237 | // Ensure there is a criteria array. |
211 | | - if ( ! array_key_exists( 'criteria', $this->parameterInfo[$name] ) ) { |
212 | | - $this->parameterInfo[$name]['criteria'] = array(); |
| 238 | + if ( ! array_key_exists( 'criteria', $this->mParameterInfo[$name] ) ) { |
| 239 | + $this->mParameterInfo[$name]['criteria'] = array(); |
213 | 240 | } |
214 | 241 | |
215 | 242 | // Ensure the type is set in array form. |
216 | | - if ( ! array_key_exists( 'type', $this->parameterInfo[$name] ) ) { |
217 | | - $this->parameterInfo[$name]['type'] = array( 'string' ); |
| 243 | + if ( ! array_key_exists( 'type', $this->mParameterInfo[$name] ) ) { |
| 244 | + $this->mParameterInfo[$name]['type'] = array( 'string' ); |
218 | 245 | } |
219 | | - elseif ( ! is_array( $this->parameterInfo[$name]['type'] ) ) { |
220 | | - $this->parameterInfo[$name]['type'] = array( $this->parameterInfo[$name]['type'] ); |
| 246 | + elseif ( ! is_array( $this->mParameterInfo[$name]['type'] ) ) { |
| 247 | + $this->mParameterInfo[$name]['type'] = array( $this->mParameterInfo[$name]['type'] ); |
221 | 248 | } |
222 | 249 | |
223 | | - if ( array_key_exists( 'type', $this->parameterInfo[$name] ) ) { |
| 250 | + if ( array_key_exists( 'type', $this->mParameterInfo[$name] ) ) { |
224 | 251 | // Add type specific criteria. |
225 | | - switch( strtolower( $this->parameterInfo[$name]['type'][0] ) ) { |
| 252 | + switch( strtolower( $this->mParameterInfo[$name]['type'][0] ) ) { |
226 | 253 | case 'integer': |
227 | 254 | $this->addTypeCriteria( $name, 'is_integer' ); |
228 | 255 | break; |
229 | | - case 'number': |
| 256 | + case 'float': |
| 257 | + // FIXME: only accept floats, not the weird crap is_numeric also accepts |
230 | 258 | $this->addTypeCriteria( $name, 'is_numeric' ); |
231 | | - break; |
| 259 | + break; |
| 260 | + case 'number': // Note: This accepts non-decimal notations! |
| 261 | + $this->addTypeCriteria( $name, 'is_numeric' ); |
| 262 | + break; |
232 | 263 | case 'boolean': |
233 | 264 | // TODO: work with list of true and false values. |
| 265 | + // TODO: i18n |
234 | 266 | $this->addTypeCriteria( $name, 'in_array', array( 'yes', 'no', 'on', 'off' ) ); |
235 | 267 | break; |
236 | 268 | case 'char': |
— | — | @@ -238,13 +270,13 @@ |
239 | 271 | } |
240 | 272 | } |
241 | 273 | |
242 | | - if ( count( $this->parameterInfo[$name]['type'] ) > 1 && $this->parameterInfo[$name]['type'][1] == 'list' ) { |
| 274 | + if ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'list' ) { |
243 | 275 | // Trimming and splitting of list values. |
244 | | - $delimiter = count( $this->parameterInfo[$name]['type'] ) > 2 ? $this->parameterInfo[$name]['type'][2] : ','; |
| 276 | + $delimiter = count( $this->mParameterInfo[$name]['type'] ) > 2 ? $this->mParameterInfo[$name]['type'][2] : ','; |
245 | 277 | $value = preg_replace( '/((\s)*' . $delimiter . '(\s)*)/', $delimiter, $value ); |
246 | 278 | $value = explode( $delimiter, $value ); |
247 | 279 | } |
248 | | - elseif ( count( $this->parameterInfo[$name]['type'] ) > 1 && $this->parameterInfo[$name]['type'][1] == 'array' && is_array( $value ) ) { |
| 280 | + elseif ( count( $this->mParameterInfo[$name]['type'] ) > 1 && $this->mParameterInfo[$name]['type'][1] == 'array' && is_array( $value ) ) { |
249 | 281 | // Trimming of array values. |
250 | 282 | for ( $i = count( $value ); $i > 0; $i-- ) $value[$i] = trim ( $value[$i] ); |
251 | 283 | } |
— | — | @@ -255,7 +287,7 @@ |
256 | 288 | } |
257 | 289 | |
258 | 290 | private function addTypeCriteria( $paramName, $criteriaName, $criteriaArgs = array() ) { |
259 | | - $this->parameterInfo[$paramName]['criteria'] = array_merge( array( $criteriaName => $criteriaArgs ), $this->parameterInfo[$paramName]['criteria'] ); |
| 291 | + $this->mParameterInfo[$paramName]['criteria'] = array_merge( array( $criteriaName => $criteriaArgs ), $this->mParameterInfo[$paramName]['criteria'] ); |
260 | 292 | } |
261 | 293 | |
262 | 294 | /** |
— | — | @@ -270,11 +302,11 @@ |
271 | 303 | $hasNoErrors = true; |
272 | 304 | $checkItemCriteria = true; |
273 | 305 | |
274 | | - if ( array_key_exists( 'list-criteria', $this->parameterInfo[$name] ) ) { |
275 | | - foreach ( $this->parameterInfo[$name]['list-criteria'] as $criteriaName => $criteriaArgs ) { |
| 306 | + if ( array_key_exists( 'list-criteria', $this->mParameterInfo[$name] ) ) { |
| 307 | + foreach ( $this->mParameterInfo[$name]['list-criteria'] as $criteriaName => $criteriaArgs ) { |
276 | 308 | // Get the validation function. If there is no matching function, throw an exception. |
277 | | - if ( array_key_exists( $criteriaName, self::$listValidationFunctions ) ) { |
278 | | - $validationFunction = self::$listValidationFunctions[$criteriaName]; |
| 309 | + if ( array_key_exists( $criteriaName, self::$mListValidationFunctions ) ) { |
| 310 | + $validationFunction = self::$mListValidationFunctions[$criteriaName]; |
279 | 311 | $isValid = $this->doCriteriaValidation( $validationFunction, $value, $criteriaArgs ); |
280 | 312 | |
281 | 313 | // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
— | — | @@ -313,10 +345,10 @@ |
314 | 346 | $hasNoErrors = true; |
315 | 347 | |
316 | 348 | // Go through all item criteria. |
317 | | - foreach ( $this->parameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) { |
| 349 | + foreach ( $this->mParameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) { |
318 | 350 | // Get the validation function. If there is no matching function, throw an exception. |
319 | | - if ( array_key_exists( $criteriaName, self::$validationFunctions ) ) { |
320 | | - $validationFunction = self::$validationFunctions[$criteriaName]; |
| 351 | + if ( array_key_exists( $criteriaName, self::$mValidationFunctions ) ) { |
| 352 | + $validationFunction = self::$mValidationFunctions[$criteriaName]; |
321 | 353 | |
322 | 354 | if ( is_array( $value ) ) { |
323 | 355 | // Handling of list parameters |
— | — | @@ -362,8 +394,8 @@ |
363 | 395 | // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
364 | 396 | if ( ! $isValid ) { |
365 | 397 | $isList = is_array( $value ); |
366 | | - if ( $isList ) $value = $this->rawParameters[$name]; |
367 | | - $this->errors[] = array( 'type' => $criteriaName, 'args' => $criteriaArgs, 'name' => $name, 'list' => $isList, 'value' => $value ); |
| 398 | + if ( $isList ) $value = $this->mRawParameters[$name]; |
| 399 | + $this->mErrors[] = array( 'type' => $criteriaName, 'args' => $criteriaArgs, 'name' => $name, 'list' => $isList, 'value' => $value ); |
368 | 400 | $hasNoErrors = false; |
369 | 401 | if ( ! self::$accumulateParameterErrors ) break; |
370 | 402 | } |
— | — | @@ -395,19 +427,18 @@ |
396 | 428 | * Changes the invalid parameters to their default values, and changes their state to valid. |
397 | 429 | */ |
398 | 430 | public function correctInvalidParams() { |
399 | | - foreach ( $this->invalid as $paramName => $paramValue ) { |
400 | | - unset( $this->invalid[$paramName] ); |
401 | | - $this->valid[$paramName] = array_key_exists( 'default', $this->parameterInfo[$paramName] ) ? $this->parameterInfo[$paramName]['default'] : ''; |
402 | | - $this->setOutputTypes( $this->valid[$paramName], $this->parameterInfo[$paramName] ); |
| 431 | + foreach ( $this->mInvalidParams as $paramName => $paramValue ) { |
| 432 | + unset( $this->mInvalidParams[$paramName] ); |
| 433 | + $this->mValidParams[$paramName] = array_key_exists( 'default', $this->mParameterInfo[$paramName] ) ? $this->mParameterInfo[$paramName]['default'] : ''; |
| 434 | + $this->setOutputTypes( $this->mValidParams[$paramName], $this->mParameterInfo[$paramName] ); |
403 | 435 | } |
404 | 436 | } |
405 | 437 | |
406 | 438 | /** |
407 | 439 | * Ensures the output type values are arrays, and then calls setOutputType. |
408 | 440 | * |
409 | | - * @param array $value |
410 | | - * @param $info |
411 | | - * @return unknown_type |
| 441 | + * @param $value |
| 442 | + * @param array $info |
412 | 443 | */ |
413 | 444 | private function setOutputTypes( &$value, array $info ) { |
414 | 445 | if ( array_key_exists( 'output-types', $info ) ) { |
— | — | @@ -434,9 +465,9 @@ |
435 | 466 | // The remaining ones will be any extra arguments. |
436 | 467 | $outputType = strtolower( array_shift( $typeInfo ) ); |
437 | 468 | |
438 | | - if ( array_key_exists( $outputType, self::$outputFormats ) ) { |
| 469 | + if ( array_key_exists( $outputType, self::$mOutputFormats ) ) { |
439 | 470 | // Call the formatting function with as first parameter the value, followed by the extra arguments. |
440 | | - call_user_func_array( self::$outputFormats[$outputType], array_merge( array( &$value ), $typeInfo ) ); |
| 471 | + call_user_func_array( self::$mOutputFormats[$outputType], array_merge( array( &$value ), $typeInfo ) ); |
441 | 472 | } |
442 | 473 | else { |
443 | 474 | throw new Exception( 'There is no formatting function for output format ' . $outputType ); |
— | — | @@ -449,7 +480,7 @@ |
450 | 481 | * @return array |
451 | 482 | */ |
452 | 483 | public function getValidParams() { |
453 | | - return $this->valid; |
| 484 | + return $this->mValidParams; |
454 | 485 | } |
455 | 486 | |
456 | 487 | /** |
— | — | @@ -458,7 +489,7 @@ |
459 | 490 | * @return array |
460 | 491 | */ |
461 | 492 | public static function getUnknownParams() { |
462 | | - return $this->unknown; |
| 493 | + return $this->mUnknownParams; |
463 | 494 | } |
464 | 495 | |
465 | 496 | /** |
— | — | @@ -467,7 +498,7 @@ |
468 | 499 | * @return array |
469 | 500 | */ |
470 | 501 | public function getErrors() { |
471 | | - return $this->errors; |
| 502 | + return $this->mErrors; |
472 | 503 | } |
473 | 504 | |
474 | 505 | /** |
— | — | @@ -479,7 +510,7 @@ |
480 | 511 | * if it's in a class, first the class name, then the method name. |
481 | 512 | */ |
482 | 513 | public static function addValidationFunction( $criteriaName, array $functionName ) { |
483 | | - self::$validationFunctions[$criteriaName] = $functionName; |
| 514 | + self::$mValidationFunctions[$criteriaName] = $functionName; |
484 | 515 | } |
485 | 516 | |
486 | 517 | /** |
— | — | @@ -491,7 +522,7 @@ |
492 | 523 | * if it's in a class, first the class name, then the method name. |
493 | 524 | */ |
494 | 525 | public static function addListValidationFunction( $criteriaName, array $functionName ) { |
495 | | - self::$listValidationFunctions[$criteriaName] = $functionName; |
| 526 | + self::$mListValidationFunctions[$criteriaName] = $functionName; |
496 | 527 | } |
497 | 528 | |
498 | 529 | /** |
— | — | @@ -503,6 +534,6 @@ |
504 | 535 | * if it's in a class, first the class name, then the method name. |
505 | 536 | */ |
506 | 537 | public static function addOutputFormat( $formatName, array $functionName ) { |
507 | | - self::$outputFormats[$formatName] = $functionName; |
| 538 | + self::$mOutputFormats[$formatName] = $functionName; |
508 | 539 | } |
509 | 540 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/Validator_Manager.php |
— | — | @@ -19,6 +19,10 @@ |
20 | 20 | * @ingroup Validator |
21 | 21 | * |
22 | 22 | * @author Jeroen De Dauw |
| 23 | + * |
| 24 | + * FIXME: missing params should result in a no-go, no matter of the error level, as they can/are not defaulted. |
| 25 | + * |
| 26 | + * TODO: make a distinction between fatal errors and regular errors by using 2 seperate error levels. |
23 | 27 | */ |
24 | 28 | final class ValidatorManager { |
25 | 29 | |
— | — | @@ -29,44 +33,64 @@ |
30 | 34 | * |
31 | 35 | * @param array $rawParameters The raw parameters, as provided by the user. |
32 | 36 | * @param array $parameterInfo Array containing the parameter definitions, which are needed for validation and defaulting. |
33 | | - * |
| 37 | + * @param array $defaultParams |
34 | 38 | * @return array or false The valid parameters or false when the output should not be shown. |
35 | 39 | */ |
36 | | - public function manageMapparameters( array $rawParameters, array $parameterInfo ) { |
| 40 | + public function manageMapparameters( array $rawParameters, array $parameterInfo, array $defaultParams = array() ) { |
37 | 41 | global $egValidatorErrorLevel; |
38 | 42 | |
39 | 43 | $validator = new Validator(); |
40 | 44 | |
41 | 45 | $validator->setParameterInfo( $parameterInfo ); |
42 | | - $validator->setParameters( $rawParameters ); |
| 46 | + $validator->parseAndSetParams( $rawParameters, $defaultParams ); |
43 | 47 | |
44 | | - if ( ! $validator->validateParameters() ) { |
45 | | - if ( $egValidatorErrorLevel != Validator_ERRORS_STRICT ) $validator->correctInvalidParams(); |
| 48 | + $hasNoErrors = $validator->validateParameters(); |
| 49 | + $hasFatalError = $hasNoErrors ? false : $this->hasFatalError(); |
| 50 | + |
| 51 | + if ( !$hasNoErrors ) { |
| 52 | + if ( $egValidatorErrorLevel < Validator_ERRORS_STRICT ) $validator->correctInvalidParams(); |
46 | 53 | if ( $egValidatorErrorLevel >= Validator_ERRORS_WARN ) $this->errors = $validator->getErrors(); |
47 | 54 | } |
| 55 | + |
| 56 | + return !$hasFatalError ? $validator->getValidParams() : false; |
| 57 | + } |
48 | 58 | |
49 | | - $showOutput = ! ( $egValidatorErrorLevel == Validator_ERRORS_STRICT && count( $this->errors ) > 0 ); |
| 59 | + /** |
| 60 | + * Returns wether there are any fatal errors. Fatal errors are either missing or invalid required parameters, |
| 61 | + * or simply any sort of error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT. |
| 62 | + * |
| 63 | + * Note: This function assumes it will only get called when there are errors. It will always return true |
| 64 | + * when $egValidatorErrorLevel is Validator_ERRORS_STRICT or above. |
| 65 | + * |
| 66 | + * @return boolean |
| 67 | + */ |
| 68 | + private function hasFatalError() { |
| 69 | + global $egValidatorErrorLevel; |
| 70 | + $has = $egValidatorErrorLevel >= Validator_ERRORS_STRICT; |
| 71 | + |
| 72 | + if ( !$has ) { |
| 73 | + foreach ( $this->errors as $error ) { |
| 74 | + if ( $error['type'] == 'missing' ) { |
| 75 | + $has = true; |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + } |
50 | 80 | |
51 | | - return $showOutput ? $validator->getValidParams() : false; |
| 81 | + return $has; |
52 | 82 | } |
53 | | - |
| 83 | + |
54 | 84 | /** |
55 | 85 | * Returns a string containing an HTML error list, or an empty string when there are no errors. |
56 | 86 | * |
57 | 87 | * @return string |
58 | 88 | */ |
59 | | - public function getErrorList( $errorLevel = null ) { |
60 | | - global $wgLang; |
61 | | - |
62 | | - $error_count = count( $this->errors ) ; |
| 89 | + public function getErrorList() { |
| 90 | + global $wgLang, $egValidatorErrorLevel; |
| 91 | + $errorCount = count( $this->errors ) ; |
63 | 92 | |
64 | | - if ( is_null( $errorLevel ) ) { |
65 | | - global $egValidatorErrorLevel; |
66 | | - $errorLevel = $egValidatorErrorLevel; |
67 | | - } |
68 | | - |
69 | | - if ( $errorLevel >= Validator_ERRORS_SHOW && $error_count > 0 ) { |
70 | | - $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', $error_count ) . '</b><br /><i>'; |
| 93 | + if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $errorCount > 0 ) { |
| 94 | + $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', $errorCount ) . '</b><br /><i>'; |
71 | 95 | |
72 | 96 | $errors = array(); |
73 | 97 | |
— | — | @@ -138,8 +162,8 @@ |
139 | 163 | |
140 | 164 | return $errorList . implode( $errors, '<br />' ) . '</i><br />'; |
141 | 165 | } |
142 | | - elseif ( $errorLevel == Validator_ERRORS_WARN && $error_count > 0 ) { |
143 | | - return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), $error_count ) . '</b>'; |
| 166 | + elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $errorCount > 0 ) { |
| 167 | + return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), $errorCount ) . '</b>'; |
144 | 168 | } |
145 | 169 | else { |
146 | 170 | return ''; |
Index: trunk/extensions/Validator/INSTALL |
— | — | @@ -1,6 +1,6 @@ |
2 | | -[[Validator 0.2.2]] |
| 2 | +[[Validator 0.3]] |
3 | 3 | |
4 | | -Once you have downloaded the code, place the 'SemanticMaps' directory within |
| 4 | +Once you have downloaded the code, place the 'Validator' directory within |
5 | 5 | your MediaWiki 'extensions' directory. Then add the following code to your |
6 | 6 | LocalSettings.php file BEFORE the inclusion of any extensions using Validator: |
7 | 7 | |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -24,11 +24,10 @@ |
25 | 25 | die( 'Not an entry point.' ); |
26 | 26 | } |
27 | 27 | |
28 | | -define( 'Validator_VERSION', '0.2.3 a1' ); |
| 28 | +define( 'Validator_VERSION', '0.3 a1' ); |
29 | 29 | |
30 | 30 | // Constants indicating the strictness of the parameter validation. |
31 | 31 | define( 'Validator_ERRORS_NONE', 0 ); |
32 | | -define( 'Validator_ERRORS_MINIMAL', 1 ); |
33 | 32 | define( 'Validator_ERRORS_WARN', 2 ); |
34 | 33 | define( 'Validator_ERRORS_SHOW', 3 ); |
35 | 34 | define( 'Validator_ERRORS_STRICT', 4 ); |
Index: trunk/extensions/Validator/Validator_Settings.php |
— | — | @@ -18,9 +18,16 @@ |
19 | 19 | } |
20 | 20 | |
21 | 21 | # Integer. The strictness of the parameter validation and resulting error report when using the ValidatorManager class. |
| 22 | +# This value also affects the error messages native to extensions that integrate Validator correctly. |
22 | 23 | # Validator_ERRORS_NONE : Validator will not show any errors, and make the best of the input it got. |
23 | | -# Validator_ERRORS_MINIMAL : Validator will only show a warning when the output could not be generated. |
24 | 24 | # Validator_ERRORS_WARN : Validator will make the best of the input it got, but will show a warning that there are errors. |
25 | 25 | # Validator_ERRORS_SHOW : Validator will make the best of the input it got, but will show a list of all errors. |
26 | 26 | # Validator_ERRORS_STRICT : Validator will only show regular output when there are no errors, if there are, a list of them will be shown. |
27 | | -$egValidatorErrorLevel = Validator_ERRORS_SHOW; |
\ No newline at end of file |
| 27 | +$egValidatorErrorLevel = Validator_ERRORS_SHOW; |
| 28 | + |
| 29 | +# Integer. The strictness of the parameter validation and resulting error report when using the ValidatorManager class. |
| 30 | +# This value also affects the error messages native to extensions that integrate Validator correctly. |
| 31 | +# Validator_ERRORS_NONE : Validator will not show any errors, and make the best of the input it got, if possible. |
| 32 | +# Validator_ERRORS_WARN : Validator will make the best of the input it got, but will show a warning that there are errors. |
| 33 | +# Validator_ERRORS_SHOW : Validator will make the best of the input it got, but will show a list of all errors. |
| 34 | +$egValidatorFatalLevel = Validator_ERRORS_SHOW; |
\ No newline at end of file |