Index: trunk/extensions/Validator/includes/ListParameter.php |
— | — | @@ -13,6 +13,16 @@ |
14 | 14 | class ListParameter extends Parameter { |
15 | 15 | |
16 | 16 | /** |
| 17 | + * Indicates if errors in list items should cause the item to be omitted, |
| 18 | + * versus having the whole list be set to it's default. |
| 19 | + * |
| 20 | + * @since 0.4 |
| 21 | + * |
| 22 | + * @var boolean |
| 23 | + */ |
| 24 | + public static $perItemValidation = true; |
| 25 | + |
| 26 | + /** |
17 | 27 | * The default delimiter for lists, used when the parameter definition does not specify one. |
18 | 28 | * |
19 | 29 | * @since 0.4 |
Index: trunk/extensions/Validator/includes/ParserHook.php |
— | — | @@ -151,21 +151,19 @@ |
152 | 152 | $this->validator->setParameters( $arguments, $this->getParameterInfo() ); |
153 | 153 | } |
154 | 154 | else { |
155 | | - $this->validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() ); |
| 155 | + $this->validator->setFunctionParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() ); |
156 | 156 | } |
157 | 157 | |
158 | | - $this->validator->validateAndFormatParameters(); |
| 158 | + $this->validator->validateParameters(); |
159 | 159 | |
160 | | - if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) { |
161 | | - $this->validator->correctInvalidParams(); |
162 | | - } |
163 | | - |
164 | 160 | if ( $this->validator->hasFatalError() ) { |
165 | 161 | // TODO |
166 | 162 | $output = 'Demo: fatal error'; |
167 | 163 | } |
168 | 164 | else { |
169 | | - $output = $this->render( $this->validator->getValidParams( false ) ); |
| 165 | + $this->validator->formatParameters(); |
| 166 | + |
| 167 | + $output = $this->render( $this->validator->getParameterValues() ); |
170 | 168 | } |
171 | 169 | |
172 | 170 | return $output; |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -20,6 +20,26 @@ |
21 | 21 | const TYPE_CHAR = 'char'; |
22 | 22 | |
23 | 23 | /** |
| 24 | + * Indicates whether parameters that are provided more then once should be accepted, |
| 25 | + * and use the first provided value, or not, and generate an error. |
| 26 | + * |
| 27 | + * @since 0.4 |
| 28 | + * |
| 29 | + * @var boolean |
| 30 | + */ |
| 31 | + public static $acceptOverriding = false; |
| 32 | + |
| 33 | + /** |
| 34 | + * Indicates whether parameters not found in the criteria list |
| 35 | + * should be stored in case they are not accepted. The default is false. |
| 36 | + * |
| 37 | + * @since 0.4 |
| 38 | + * |
| 39 | + * @var boolean |
| 40 | + */ |
| 41 | + public static $accumulateParameterErrors = false; |
| 42 | + |
| 43 | + /** |
24 | 44 | * Indicates if the parameter value should be lowercased. |
25 | 45 | * |
26 | 46 | * @since 0.4 |
— | — | @@ -112,6 +132,15 @@ |
113 | 133 | protected $originalValue; |
114 | 134 | |
115 | 135 | /** |
| 136 | + * The value of the parameter. |
| 137 | + * |
| 138 | + * @since 0.4 |
| 139 | + * |
| 140 | + * @var mixed |
| 141 | + */ |
| 142 | + protected $value; |
| 143 | + |
| 144 | + /** |
116 | 145 | * Keeps track of how many times the parameter has been set by the user. |
117 | 146 | * This is used to detect overrides and for figuring out a parameter is missing. |
118 | 147 | * |
— | — | @@ -253,40 +282,83 @@ |
254 | 283 | } |
255 | 284 | |
256 | 285 | /** |
| 286 | + * |
| 287 | + * |
257 | 288 | * @since 0.4 |
258 | 289 | * |
259 | 290 | * @param string $paramName |
260 | 291 | * @param string $paramValue |
| 292 | + * |
| 293 | + * @return boolean |
261 | 294 | */ |
262 | 295 | public function setUserValue( $paramName, $paramValue ) { |
263 | | - if ( $this->setCount > 0 && true /* TODO: accept overridng? */ ) { |
| 296 | + if ( $this->setCount > 0 && !self::$acceptOverriding ) { |
264 | 297 | // TODO: fatal error |
| 298 | + /* |
| 299 | + $this->registerError( |
| 300 | + wfMsgExt( |
| 301 | + 'validator-error-override-argument', |
| 302 | + 'parsemag', |
| 303 | + $paramName, |
| 304 | + $this->mParameters[$mainName]['original-value'], |
| 305 | + is_array( $paramData ) ? $paramData['original-value'] : $paramData |
| 306 | + ), |
| 307 | + 'override' |
| 308 | + ); |
| 309 | + */ |
| 310 | + |
| 311 | + return false; |
265 | 312 | } |
266 | 313 | else { |
267 | 314 | $this->originalName = $paramName; |
268 | | - $this->paramValue = $paramValue; |
| 315 | + $this->originalValue = $paramValue; |
269 | 316 | |
270 | | - $this->setCount++; |
| 317 | + $this->cleanValue(); |
| 318 | + |
| 319 | + $this->setCount++; |
| 320 | + |
| 321 | + return true; |
271 | 322 | } |
272 | 323 | } |
273 | 324 | |
274 | 325 | /** |
| 326 | + * Sets the $value to a cleaned value of $originalValue. |
| 327 | + * |
| 328 | + * @since 0.4 |
| 329 | + */ |
| 330 | + protected function cleanValue() { |
| 331 | + $this->value = $this->originalValue; |
| 332 | + |
| 333 | + if ( $this->lowerCaseValue ) { |
| 334 | + $this->value = strtolower( $this->value ); |
| 335 | + } |
| 336 | + } |
| 337 | + |
| 338 | + /** |
275 | 339 | * Validates the parameter value against it's criteria. |
| 340 | + * If the parameter is invalid or not provided, it'll be set to it's default, |
| 341 | + * or when it's required, a fatal error will be stored. |
276 | 342 | * |
277 | 343 | * @since 0.4 |
| 344 | + * |
| 345 | + * @return boolean If there where no fatal errors |
278 | 346 | */ |
279 | 347 | public function validate() { |
280 | 348 | if ( $this->setCount == 0 ) { |
281 | 349 | if ( $this->isRequired() ) { |
282 | 350 | // TODO: fatal error |
| 351 | + $success = false; |
283 | 352 | } |
284 | 353 | else { |
285 | | - $this->validateCriteria( $this->default ); |
| 354 | + $success = true; |
| 355 | + $this->value = $this->default; |
286 | 356 | } |
287 | 357 | } |
288 | 358 | else { |
289 | | - $this->validateCriteria( $this->originalValue ); |
| 359 | + $success = $this->validateCriteria( $this->originalValue ); |
290 | 360 | } |
| 361 | + |
| 362 | + return $success; |
291 | 363 | } |
292 | 364 | |
293 | 365 | /** |
— | — | @@ -295,13 +367,29 @@ |
296 | 368 | * @since 0.4 |
297 | 369 | * |
298 | 370 | * @param string $value |
| 371 | + * |
| 372 | + * @return boolean If there where no fatal errors |
299 | 373 | */ |
300 | 374 | protected function validateCriteria( $value ) { |
| 375 | + $success = true; |
| 376 | + $hasError = false; |
| 377 | + |
301 | 378 | foreach ( $this->getCriteria() as $criterion ) { |
302 | 379 | if ( !$criterion->validate( $value ) ) { |
| 380 | + $hasError = true; |
303 | 381 | |
| 382 | + if ( !self::$accumulateParameterErrors ) { |
| 383 | + break; |
| 384 | + } |
304 | 385 | } |
305 | 386 | } |
| 387 | + |
| 388 | + // TODO: move this to a nicer place |
| 389 | + if ( $hasError ) { |
| 390 | + $this->value = $this->default; |
| 391 | + } |
| 392 | + |
| 393 | + return $success; |
306 | 394 | } |
307 | 395 | |
308 | 396 | /** |
— | — | @@ -316,6 +404,17 @@ |
317 | 405 | } |
318 | 406 | |
319 | 407 | /** |
| 408 | + * Returns the parameters value. |
| 409 | + * |
| 410 | + * @since 0.4 |
| 411 | + * |
| 412 | + * @return string |
| 413 | + */ |
| 414 | + public function getValue() { |
| 415 | + return $this->value; |
| 416 | + } |
| 417 | + |
| 418 | + /** |
320 | 419 | * Returns if the parameter is a required one or not. |
321 | 420 | * |
322 | 421 | * @since 0.4 |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -18,30 +18,8 @@ |
19 | 19 | class Validator { |
20 | 20 | |
21 | 21 | /** |
22 | | - * @var boolean Indicates whether parameters not found in the criteria list |
23 | | - * should be stored in case they are not accepted. The default is false. |
24 | | - */ |
25 | | - public static $storeUnknownParameters = false; |
26 | | - |
27 | | - /** |
28 | | - * @var boolean Indicates whether parameters not found in the criteria list |
29 | | - * should be stored in case they are not accepted. The default is false. |
30 | | - */ |
31 | | - public static $accumulateParameterErrors = false; |
32 | | - |
33 | | - /** |
34 | | - * @var boolean Indicates whether parameters that are provided more then once |
35 | | - * should be accepted, and use the first provided value, or not, and generate an error. |
36 | | - */ |
37 | | - public static $acceptOverriding = false; |
38 | | - |
39 | | - /** |
40 | | - * @var boolean Indicates if errors in list items should cause the item to be omitted, |
41 | | - * versus having the whole list be set to it's default. |
42 | | - */ |
43 | | - public static $perItemValidation = true; |
44 | | - |
45 | | - /** |
| 22 | + * @deprecated TODO: remove |
| 23 | + * |
46 | 24 | * @var array Holder for the formatting functions. |
47 | 25 | */ |
48 | 26 | protected static $mOutputFormats = array( |
— | — | @@ -55,37 +33,15 @@ |
56 | 34 | ); |
57 | 35 | |
58 | 36 | /** |
59 | | - * Array containing parameter definitions. |
| 37 | + * Array containing the parameters. |
60 | 38 | * |
61 | 39 | * @since 0.4 |
62 | 40 | * |
63 | 41 | * @var array of Parameter |
64 | 42 | */ |
65 | | - protected $parameterInfo; |
| 43 | + protected $parameters; |
66 | 44 | |
67 | 45 | /** |
68 | | - * An array initially containing the user provided values. Adittional data about |
69 | | - * the validation and formatting processes gets added later on, and so stays |
70 | | - * available for validation and formatting of other parameters. |
71 | | - * |
72 | | - * original-value |
73 | | - * default |
74 | | - * position |
75 | | - * original-name |
76 | | - * formatted-value |
77 | | - * |
78 | | - * @var associative array |
79 | | - */ |
80 | | - protected $mParameters = array(); |
81 | | - |
82 | | - /** |
83 | | - * Arrays for holding the (main) names of valid, invalid and unknown parameters. |
84 | | - */ |
85 | | - protected $mValidParams = array(); |
86 | | - protected $mInvalidParams = array(); |
87 | | - protected $mUnknownParams = array(); |
88 | | - |
89 | | - /** |
90 | 46 | * List of ValidatorError. |
91 | 47 | * |
92 | 48 | * @since 0.4 |
— | — | @@ -95,8 +51,8 @@ |
96 | 52 | protected $errors = array(); |
97 | 53 | |
98 | 54 | /** |
| 55 | + * Name of the element that's being validated. |
99 | 56 | * |
100 | | - * |
101 | 57 | * @since 0.4 |
102 | 58 | * |
103 | 59 | * @var string |
— | — | @@ -115,6 +71,8 @@ |
116 | 72 | } |
117 | 73 | |
118 | 74 | /** |
| 75 | + * @deprecated TODO: remove |
| 76 | + * |
119 | 77 | * Adds a new output format and the formatting function that should validate values of this type. |
120 | 78 | * You can use this function to override existing criteria type handlers. |
121 | 79 | * |
— | — | @@ -127,47 +85,17 @@ |
128 | 86 | } |
129 | 87 | |
130 | 88 | /** |
131 | | - * Registers an error. |
132 | | - * |
133 | | - * @param string $message |
134 | | - * @param mixed $tags string or array |
135 | | - * @param integer $severity |
136 | | - */ |
137 | | - protected function registerError( $message, $tags = array(), $severity = ValidatorError::SEVERITY_NORMAL ) { |
138 | | - $error = new ValidatorError( |
139 | | - $message, |
140 | | - $severity, |
141 | | - $this->element, |
142 | | - (array)$tags |
143 | | - ); |
144 | | - |
145 | | - $this->errors[] = $error; |
146 | | - ValidatorErrorHandler::addError( $error ); |
147 | | - } |
148 | | - |
149 | | - /** |
150 | | - * Ensures all elements of the array are Parameter objects. |
151 | | - * |
152 | | - * @since 0.4 |
153 | | - * |
154 | | - * @param array $paramInfo |
155 | | - */ |
156 | | - protected function cleanParameterInfo( array &$paramInfo ) { |
157 | | - foreach ( $paramInfo as $key => &$parameter ) { |
158 | | - $parameter = $parameter instanceof Parameter ? $parameter : Parameter::newFromArray( $key, $parameter ); |
159 | | - } |
160 | | - } |
161 | | - |
162 | | - /** |
163 | 89 | * Determines the names and values of all parameters. Also takes care of default parameters. |
164 | 90 | * After that the resulting parameter list is passed to Validator::setParameters |
165 | 91 | * |
| 92 | + * @since 0.4 |
| 93 | + * |
166 | 94 | * @param array $rawParams |
167 | 95 | * @param array $parameterInfo |
168 | 96 | * @param array $defaultParams |
169 | 97 | * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true. |
170 | 98 | */ |
171 | | - public function parseAndSetParams( array $rawParams, array $parameterInfo, array $defaultParams = array(), $toLower = true ) { |
| 99 | + public function setFunctionParams( array $rawParams, array $parameterInfo, array $defaultParams = array(), $toLower = true ) { |
172 | 100 | $this->cleanParameterInfo( $parameterInfo ); |
173 | 101 | |
174 | 102 | $parameters = array(); |
— | — | @@ -187,8 +115,6 @@ |
188 | 116 | if ( count( $defaultParams ) > 0 ) { |
189 | 117 | $defaultParam = strtolower( array_shift( $defaultParams ) ); |
190 | 118 | |
191 | | - $this->lowerCaseIfNeeded( $parts[0], $defaultParam, $parameterInfo, $toLower ); |
192 | | - |
193 | 119 | $parameters[$defaultParam] = array( |
194 | 120 | 'original-value' => trim( $parts[0] ), |
195 | 121 | 'default' => $defaultNr, |
— | — | @@ -202,8 +128,6 @@ |
203 | 129 | } else { |
204 | 130 | $paramName = trim( strtolower( $parts[0] ) ); |
205 | 131 | |
206 | | - $this->lowerCaseIfNeeded( $parts[1], $paramName, $parameterInfo, $toLower ); |
207 | | - |
208 | 132 | $parameters[$paramName] = array( |
209 | 133 | 'original-value' => trim( $parts[1] ), |
210 | 134 | 'default' => false, |
— | — | @@ -238,8 +162,8 @@ |
239 | 163 | public function setParameters( array $parameters, array $parameterInfo, $toLower = true ) { |
240 | 164 | $this->cleanParameterInfo( $parameterInfo ); |
241 | 165 | |
242 | | - $this->parameterInfo = $parameterInfo; |
243 | | - |
| 166 | + $this->parameters = $parameterInfo; |
| 167 | + |
244 | 168 | // Loop through all the user provided parameters, and destinguise between those that are allowed and those that are not. |
245 | 169 | foreach ( $parameters as $paramName => $paramData ) { |
246 | 170 | $paramName = trim( strtolower( $paramName ) ); |
— | — | @@ -249,42 +173,21 @@ |
250 | 174 | |
251 | 175 | // If the parameter is found in the list of allowed ones, add it to the $mParameters array. |
252 | 176 | if ( $mainName ) { |
253 | | - // Check for parameter overriding. In most cases, this has already largely been taken care off, |
254 | | - // in the form of later parameters overriding earlier ones. This is not true for different aliases though. |
255 | | - if ( !array_key_exists( $mainName, $this->mParameters ) || self::$acceptOverriding ) { |
256 | | - // If the valueis an array, this means it has been procesed in parseAndSetParams already. |
257 | | - // If it is not, setParameters was called directly with an array of string parameter values. |
258 | | - if ( is_array( $paramData ) && array_key_exists( 'original-value', $paramData ) ) { |
259 | | - $paramData['original-name'] = $paramName; |
260 | | - $this->mParameters[$mainName] = $paramData; |
261 | | - } |
262 | | - else { |
263 | | - if ( is_string( $paramData ) ) { |
264 | | - $paramData = trim( $paramData ); |
265 | | - $this->lowerCaseIfNeeded( $paramData, $mainName, $this->parameterInfo, $toLower ); |
266 | | - } |
267 | | - |
268 | | - $this->mParameters[$mainName] = array( |
269 | | - 'original-value' => $paramData, |
270 | | - 'original-name' => $paramName, |
271 | | - ); |
272 | | - } |
| 177 | + // If the valueis an array, this means it has been procesed in parseAndSetParams already. |
| 178 | + // If it is not, setParameters was called directly with an array of string parameter values. |
| 179 | + if ( is_array( $paramData ) ) { |
| 180 | + $this->parameters[$mainName]->setUserValue( $paramName, $paramData['original-value'] ); |
273 | 181 | } |
274 | 182 | else { |
275 | | - $this->registerError( |
276 | | - wfMsgExt( |
277 | | - 'validator-error-override-argument', |
278 | | - 'parsemag', |
279 | | - $paramName, |
280 | | - $this->mParameters[$mainName]['original-value'], |
281 | | - is_array( $paramData ) ? $paramData['original-value'] : $paramData |
282 | | - ), |
283 | | - 'override' |
284 | | - ); |
| 183 | + if ( is_string( $paramData ) ) { |
| 184 | + $paramData = trim( $paramData ); |
| 185 | + } |
| 186 | + |
| 187 | + $this->parameters[$mainName]->setUserValue( $paramName, $paramData ); |
285 | 188 | } |
| 189 | + |
286 | 190 | } |
287 | 191 | else { // If the parameter is not found in the list of allowed ones, add an item to the $this->mErrors array. |
288 | | - if ( self::$storeUnknownParameters ) $this->mUnknownParams[] = $paramName; |
289 | 192 | $this->registerError( |
290 | 193 | wfMsgExt( |
291 | 194 | 'validator_error_unknown_argument', |
— | — | @@ -298,18 +201,37 @@ |
299 | 202 | } |
300 | 203 | |
301 | 204 | /** |
302 | | - * Lowercases the provided $paramValue if needed. |
| 205 | + * Registers an error. |
303 | 206 | * |
304 | | - * @since 0.3.6 |
| 207 | + * @since 0.4 |
305 | 208 | * |
306 | | - * @param $paramValue String |
307 | | - * @param $paramName String |
308 | | - * @param $parameterInfo Array |
309 | | - * @param $globalDefault Boolean |
| 209 | + * @param string $message |
| 210 | + * @param mixed $tags string or array |
| 211 | + * @param integer $severity |
310 | 212 | */ |
311 | | - protected function lowerCaseIfNeeded( &$paramValue, $paramName, array $parameterInfo, $globalDefault ) { |
312 | | - $lowerCase = array_key_exists( $paramName, $parameterInfo ) ? $parameterInfo[$paramName]->lowerCaseValue : $globalDefault; |
313 | | - if ( $lowerCase ) $paramValue = strtolower( $paramValue ); |
| 213 | + protected function registerError( $message, $tags = array(), $severity = ValidatorError::SEVERITY_NORMAL ) { |
| 214 | + $error = new ValidatorError( |
| 215 | + $message, |
| 216 | + $severity, |
| 217 | + $this->element, |
| 218 | + (array)$tags |
| 219 | + ); |
| 220 | + |
| 221 | + $this->errors[] = $error; |
| 222 | + ValidatorErrorHandler::addError( $error ); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Ensures all elements of the array are Parameter objects. |
| 227 | + * |
| 228 | + * @since 0.4 |
| 229 | + * |
| 230 | + * @param array $paramInfo |
| 231 | + */ |
| 232 | + protected function cleanParameterInfo( array &$paramInfo ) { |
| 233 | + foreach ( $paramInfo as $key => &$parameter ) { |
| 234 | + $parameter = $parameter instanceof Parameter ? $parameter : Parameter::newFromArray( $key, $parameter ); |
| 235 | + } |
314 | 236 | } |
315 | 237 | |
316 | 238 | /** |
— | — | @@ -323,11 +245,11 @@ |
324 | 246 | protected function getMainParamName( $paramName ) { |
325 | 247 | $result = false; |
326 | 248 | |
327 | | - if ( array_key_exists( $paramName, $this->parameterInfo ) ) { |
| 249 | + if ( array_key_exists( $paramName, $this->parameters ) ) { |
328 | 250 | $result = $paramName; |
329 | 251 | } |
330 | 252 | else { |
331 | | - foreach ( $this->parameterInfo as $name => $parameter ) { |
| 253 | + foreach ( $this->parameters as $name => $parameter ) { |
332 | 254 | if ( $parameter->hasAlias( $paramName ) ) { |
333 | 255 | $result = $name; |
334 | 256 | break; |
— | — | @@ -336,19 +258,17 @@ |
337 | 259 | } |
338 | 260 | |
339 | 261 | return $result; |
340 | | - } |
| 262 | + } |
341 | 263 | |
342 | 264 | /** |
343 | | - * First determines the order of parameter handling based on the dependency definitons, |
344 | | - * and then goes through the parameters one by one, first validating and then formatting, |
345 | | - * storing any encountered errors along the way. |
| 265 | + * Validates all the parameters (but aborts when a fatal error occurs). |
346 | 266 | * |
347 | | - * The 'value' element is set here, either by the cleaned 'original-value' or default. |
| 267 | + * @since 0.4 |
348 | 268 | */ |
349 | | - public function validateAndFormatParameters() { |
| 269 | + public function validateParameters() { |
350 | 270 | $dependencyList = array(); |
351 | 271 | |
352 | | - foreach ( $this->parameterInfo as $paramName => $parameter ) { |
| 272 | + foreach ( $this->parameters as $paramName => $parameter ) { |
353 | 273 | $dependencyList[$paramName] = $parameter->dependencies; |
354 | 274 | } |
355 | 275 | |
— | — | @@ -356,275 +276,34 @@ |
357 | 277 | $orderedParameters = $sorter->doSort(); |
358 | 278 | |
359 | 279 | foreach ( $orderedParameters as $paramName ) { |
360 | | - $parameter = $this->parameterInfo[$paramName]; |
| 280 | + $parameter = $this->parameters[$paramName]; |
361 | 281 | |
362 | | - // If the user provided a value for this parameter, validate and handle it. |
363 | | - if ( array_key_exists( $paramName, $this->mParameters ) ) { |
364 | | - |
365 | | - $this->cleanParameter( $paramName ); |
366 | | - |
367 | | - if ( $this->validateParameter( $paramName ) ) { |
368 | | - // If the validation succeeded, add the parameter to the list of valid ones. |
369 | | - $this->mValidParams[] = $paramName; |
370 | | - $this->setOutputTypes( $paramName ); |
371 | | - } |
372 | | - else { |
373 | | - // If the validation failed, add the parameter to the list of invalid ones. |
374 | | - $this->mInvalidParams[] = $paramName; |
375 | | - } |
376 | | - } |
377 | | - else { |
378 | | - // If the parameter is required, add a new error of type 'missing'. |
379 | | - // TODO: break when has dependencies |
380 | | - if ( $parameter->isRequired() ) { |
381 | | - $this->registerError( |
382 | | - wfMsgExt( |
383 | | - 'validator_error_required_missing', |
384 | | - 'parsemag', |
385 | | - $paramName |
386 | | - ), |
387 | | - 'missing' |
388 | | - ); |
389 | | - } |
390 | | - else { |
391 | | - // Set the default value. |
392 | | - $this->mParameters[$paramName]['value'] = $parameter->default; |
393 | | - $this->mValidParams[] = $paramName; |
394 | | - $this->setOutputTypes( $paramName ); |
395 | | - } |
396 | | - } |
397 | | - } |
398 | | - } |
399 | | - |
400 | | - /** |
401 | | - * Ensures the parameter info is valid and parses list types. |
402 | | - * |
403 | | - * @param string $name |
404 | | - */ |
405 | | - private function cleanParameter( $name ) { |
406 | | - // If the original-value element is set, clean it, and store as value. |
407 | | - if ( array_key_exists( 'original-value', $this->mParameters[$name] ) ) { |
408 | | - $value = $this->mParameters[$name]['original-value']; |
| 282 | + // Do the validation. |
| 283 | + $success = $parameter->validate(); |
409 | 284 | |
410 | | - if ( $this->parameterInfo[$name]->isList() ) { |
411 | | - // Trimming and splitting of list values. |
412 | | - $delimiter = $this->parameterInfo[$name]->getListDelimeter(); |
413 | | - $value = preg_replace( '/((\s)*' . $delimiter . '(\s)*)/', $delimiter, $value ); |
414 | | - $value = explode( $delimiter, $value ); |
| 285 | + // Break on fatal errors. |
| 286 | + if ( !$success ) { |
| 287 | + break; |
415 | 288 | } |
416 | | - |
417 | | - $this->mParameters[$name]['value'] = $value; |
418 | 289 | } |
419 | 290 | } |
420 | 291 | |
421 | 292 | /** |
422 | | - * Valides the provided parameter. |
| 293 | + * Applies the output formats to all parameters. |
423 | 294 | * |
424 | | - * This method itself validates the list criteria, if any. After this the regular criteria |
425 | | - * are validated by calling the doItemValidation method. |
426 | | - * |
427 | 295 | * @param string $name |
428 | | - * |
429 | | - * @return boolean Indicates whether there the parameter value(s) is/are valid. |
430 | 296 | */ |
431 | | - protected function validateParameter( $name ) { |
432 | | - $hasNoErrors = $this->doListValidation( $name ); |
433 | | - |
434 | | - if ( $hasNoErrors || self::$accumulateParameterErrors ) { |
435 | | - $hasNoErrors = $hasNoErrors && $this->doItemValidation( $name ); |
| 297 | + public function formatParameters() { |
| 298 | + foreach ( $this->parameters as $parameter ) { |
| 299 | + foreach ( $parameter->outputTypes as $outputType ) { |
| 300 | + $this->setOutputType( $parameter->getName(), $outputType ); |
| 301 | + } |
436 | 302 | } |
437 | | - |
438 | | - return $hasNoErrors; |
439 | 303 | } |
440 | 304 | |
441 | 305 | /** |
442 | | - * Validates the list criteria for a parameter, if there are any. |
| 306 | + * @deprecated TODO: remove |
443 | 307 | * |
444 | | - * @param string $name |
445 | | - */ |
446 | | - protected function doListValidation( $name ) { |
447 | | - $hasNoErrors = true; |
448 | | - |
449 | | - /* TODO |
450 | | - foreach ( $this->parameterInfo[$name]->getListCriteria() as $criteriaName => $criteriaArgs ) { |
451 | | - // Get the validation function. If there is no matching function, throw an exception. |
452 | | - if ( array_key_exists( $criteriaName, self::$mListValidationFunctions ) ) { |
453 | | - $validationFunction = self::$mListValidationFunctions[$criteriaName]; |
454 | | - $isValid = $this->doCriteriaValidation( $validationFunction, $this->mParameters['value'], $name, $criteriaArgs ); |
455 | | - |
456 | | - // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
457 | | - if ( ! $isValid ) { |
458 | | - $hasNoErrors = false; |
459 | | - |
460 | | - $this->registerError( |
461 | | - $this->getCriteriaErrorMessage( |
462 | | - $criteriaName, |
463 | | - $this->mParameters[$name]['original-name'], |
464 | | - $this->mParameters[$name]['original-value'], |
465 | | - $criteriaArgs, |
466 | | - true |
467 | | - ), |
468 | | - $criteriaName |
469 | | - ); |
470 | | - |
471 | | - if ( !self::$accumulateParameterErrors ) { |
472 | | - break; |
473 | | - } |
474 | | - } |
475 | | - } |
476 | | - else { |
477 | | - $hasNoErrors = false; |
478 | | - throw new Exception( 'There is no validation function for list criteria type ' . $criteriaName ); |
479 | | - } |
480 | | - } |
481 | | - */ |
482 | | - |
483 | | - return $hasNoErrors; |
484 | | - } |
485 | | - |
486 | | - /** |
487 | | - * Valides the provided parameter by matching the value against the item criteria for the name. |
488 | | - * |
489 | | - * @param string $name |
490 | | - * |
491 | | - * @return boolean Indicates whether there the parameter value(s) is/are valid. |
492 | | - */ |
493 | | - protected function doItemValidation( $name ) { |
494 | | - $hasNoErrors = true; |
495 | | - |
496 | | - $value = &$this->mParameters[$name]['value']; |
497 | | - |
498 | | - /* TODO |
499 | | - // Go through all item criteria. |
500 | | - foreach ( $this->parameterInfo[$name]->getCriteria() as $criteriaName => $criteriaArgs ) { |
501 | | - // Get the validation function. If there is no matching function, throw an exception. |
502 | | - if ( array_key_exists( $criteriaName, self::$mValidationFunctions ) ) { |
503 | | - $validationFunction = self::$mValidationFunctions[$criteriaName]; |
504 | | - |
505 | | - if ( is_array( $value ) ) { |
506 | | - // Handling of list parameters |
507 | | - $invalidItems = array(); |
508 | | - $validItems = array(); |
509 | | - |
510 | | - // Loop through all the items in the parameter value, and validate them. |
511 | | - foreach ( $value as $item ) { |
512 | | - $isValid = $this->doCriteriaValidation( $validationFunction, $item, $name, $criteriaArgs ); |
513 | | - if ( $isValid ) { |
514 | | - // If per item validation is on, store the valid items, so only these can be returned by Validator. |
515 | | - if ( self::$perItemValidation ) $validItems[] = $item; |
516 | | - } |
517 | | - else { |
518 | | - // If per item validation is on, store the invalid items, so a fitting error message can be created. |
519 | | - if ( self::$perItemValidation ) { |
520 | | - $invalidItems[] = $item; |
521 | | - } |
522 | | - else { |
523 | | - // If per item validation is not on, an error to one item means the complete value is invalid. |
524 | | - // Therefore it's not required to validate the remaining items. |
525 | | - break; |
526 | | - } |
527 | | - } |
528 | | - } |
529 | | - |
530 | | - if ( self::$perItemValidation ) { |
531 | | - // If per item validation is on, the parameter value is valid as long as there is at least one valid item. |
532 | | - $isValid = count( $validItems ) > 0; |
533 | | - |
534 | | - // If the value is valid, but there are invalid items, add an error with a list of these items. |
535 | | - if ( $isValid && count( $invalidItems ) > 0 ) { |
536 | | - $value = $validItems; |
537 | | - |
538 | | - $this->registerError( |
539 | | - $this->getCriteriaErrorMessage( |
540 | | - $criteriaName, |
541 | | - $this->mParameters[$name]['original-name'], |
542 | | - $this->mParameters[$name]['original-value'], |
543 | | - $criteriaArgs, |
544 | | - true, |
545 | | - $invalidItems |
546 | | - ), |
547 | | - $criteriaName |
548 | | - ); |
549 | | - } |
550 | | - } |
551 | | - } |
552 | | - else { |
553 | | - // Determine if the value is valid for single valued parameters. |
554 | | - $isValid = $this->doCriteriaValidation( $validationFunction, $value, $name, $criteriaArgs ); |
555 | | - } |
556 | | - |
557 | | - // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated. |
558 | | - if ( !$isValid ) { |
559 | | - $this->registerError( |
560 | | - $this->getCriteriaErrorMessage( |
561 | | - $criteriaName, |
562 | | - $this->mParameters[$name]['original-name'], |
563 | | - $this->mParameters[$name]['original-value'], |
564 | | - $criteriaArgs, |
565 | | - is_array( $value ) |
566 | | - ), |
567 | | - $criteriaName |
568 | | - ); |
569 | | - |
570 | | - $hasNoErrors = false; |
571 | | - if ( !self::$accumulateParameterErrors ) break; |
572 | | - } |
573 | | - } |
574 | | - else { |
575 | | - $hasNoErrors = false; |
576 | | - throw new Exception( 'There is no validation function for criteria type ' . $criteriaName ); |
577 | | - } |
578 | | - } |
579 | | - */ |
580 | | - |
581 | | - return $hasNoErrors; |
582 | | - } |
583 | | - |
584 | | - /** |
585 | | - * Calls the validation function for the provided list or single value and returns it's result. |
586 | | - * The call is made with these parameters: |
587 | | - * - value: The value that is the complete list, or a single item. |
588 | | - * - parameter name: For lookups in the param info array. |
589 | | - * - parameter array: All data about the parameters gathered so far (this includes dependencies!). |
590 | | - * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters. |
591 | | - * |
592 | | - * @param $validationFunction |
593 | | - * @param mixed $value |
594 | | - * @param string $name |
595 | | - * @param array $criteriaArgs |
596 | | - * |
597 | | - * @return boolean |
598 | | - */ |
599 | | - private function doCriteriaValidation( $validationFunction, $value, $name, array $criteriaArgs ) { |
600 | | - // Call the validation function and store the result. |
601 | | - $parameters = array( &$value, $name, $this->mParameters ); |
602 | | - $parameters = array_merge( $parameters, $criteriaArgs ); |
603 | | - return call_user_func_array( $validationFunction, $parameters ); |
604 | | - } |
605 | | - |
606 | | - /** |
607 | | - * Changes the invalid parameters to their default values, and changes their state to valid. |
608 | | - */ |
609 | | - public function correctInvalidParams() { |
610 | | - while ( $paramName = array_shift( $this->mInvalidParams ) ) { |
611 | | - $this->mParameters[$paramName]['value'] = $this->parameterInfo[$paramName]->default; |
612 | | - $this->setOutputTypes( $paramName ); |
613 | | - $this->mValidParams[] = $paramName; |
614 | | - } |
615 | | - } |
616 | | - |
617 | | - /** |
618 | | - * Ensures the output type values are arrays, and then calls setOutputType. |
619 | | - * |
620 | | - * @param string $name |
621 | | - */ |
622 | | - protected function setOutputTypes( $name ) { |
623 | | - foreach ( $this->parameterInfo[$name]->outputTypes as $outputType ) { |
624 | | - $this->setOutputType( $name, $outputType ); |
625 | | - } |
626 | | - } |
627 | | - |
628 | | - /** |
629 | 308 | * Calls the formatting function for the provided output format with these parameters: |
630 | 309 | * - parameter value: ByRef for easy manipulation. |
631 | 310 | * - parameter name: For lookups in the param info array. |
— | — | @@ -639,12 +318,12 @@ |
640 | 319 | // The remaining ones will be any extra arguments. |
641 | 320 | $outputType = strtolower( array_shift( $typeInfo ) ); |
642 | 321 | |
643 | | - if ( !array_key_exists( 'formatted-value', $this->mParameters[$name] ) ) { |
644 | | - $this->mParameters[$name]['formatted-value'] = $this->mParameters[$name]['value']; |
| 322 | + if ( !array_key_exists( 'formatted-value', $this->parameters[$name] ) ) { |
| 323 | + $this->parameters[$name]['formatted-value'] = $this->parameters[$name]['value']; |
645 | 324 | } |
646 | 325 | |
647 | 326 | if ( array_key_exists( $outputType, self::$mOutputFormats ) ) { |
648 | | - $parameters = array( &$this->mParameters[$name]['formatted-value'], $name, $this->mParameters ); |
| 327 | + $parameters = array( &$this->parameters[$name]['formatted-value'], $name, $this->parameters ); |
649 | 328 | $parameters = array_merge( $parameters, $typeInfo ); |
650 | 329 | call_user_func_array( self::$mOutputFormats[$outputType], $parameters ); |
651 | 330 | } |
— | — | @@ -654,43 +333,34 @@ |
655 | 334 | } |
656 | 335 | |
657 | 336 | /** |
658 | | - * Returns the valid parameters. |
659 | | - * |
660 | | - * @param boolean $includeMetaData |
661 | | - * |
| 337 | + * Returns the parameters. |
| 338 | + * |
| 339 | + * @since 0.4 |
| 340 | + * |
662 | 341 | * @return array |
663 | 342 | */ |
664 | | - public function getValidParams( $includeMetaData ) { |
665 | | - if ( $includeMetaData ) { |
666 | | - return $this->mValidParams; |
667 | | - } |
668 | | - else { |
669 | | - $validParams = array(); |
670 | | - |
671 | | - foreach( $this->mValidParams as $name ) { |
672 | | - $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value'; |
673 | | - $validParams[$name] = $this->mParameters[$name][$key]; |
674 | | - } |
675 | | - |
676 | | - return $validParams; |
677 | | - } |
| 343 | + public function getParameters() { |
| 344 | + return $this->parameters; |
678 | 345 | } |
679 | | - |
| 346 | + |
680 | 347 | /** |
681 | | - * Returns the unknown parameters. |
682 | | - * |
| 348 | + * Returns an associative array with the parameter names as key and their |
| 349 | + * correspinding values as value. |
| 350 | + * |
| 351 | + * @since 0.4 |
| 352 | + * |
683 | 353 | * @return array |
684 | 354 | */ |
685 | | - public static function getUnknownParams() { |
686 | | - $unknownParams = array(); |
| 355 | + public function getParameterValues() { |
| 356 | + $parameters = array(); |
687 | 357 | |
688 | | - foreach( $this->mUnknownParams as $name ) { |
689 | | - $unknownParams[$name] = $this->mParameters[$name]; |
690 | | - } |
| 358 | + foreach ( $this->parameters as $parameter ) { |
| 359 | + $parameters[$parameter->getName()] = $parameter->getValue(); |
| 360 | + } |
691 | 361 | |
692 | | - return $unknownParams; |
| 362 | + return $parameters; |
693 | 363 | } |
694 | | - |
| 364 | + |
695 | 365 | /** |
696 | 366 | * Returns the errors. |
697 | 367 | * |
— | — | @@ -731,7 +401,7 @@ |
732 | 402 | /** |
733 | 403 | * Returns an error message for a criteria validation that failed. |
734 | 404 | * |
735 | | - * TODO: integrate this further with the hook mechanisms |
| 405 | + * TODO: move individual messgaes over to their corresponding criterion class |
736 | 406 | * TODO: proper escaping |
737 | 407 | * |
738 | 408 | * @since 0.4 |