r72477 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72476‎ | r72477 | r72478 >
Date:11:59, 6 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - work on the new parameter handling model
Modified paths:
  • /trunk/extensions/Validator/includes/ListParameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParserHook.php (modified) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ListParameter.php
@@ -13,6 +13,16 @@
1414 class ListParameter extends Parameter {
1515
1616 /**
 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+ /**
1727 * The default delimiter for lists, used when the parameter definition does not specify one.
1828 *
1929 * @since 0.4
Index: trunk/extensions/Validator/includes/ParserHook.php
@@ -151,21 +151,19 @@
152152 $this->validator->setParameters( $arguments, $this->getParameterInfo() );
153153 }
154154 else {
155 - $this->validator->parseAndSetParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() );
 155+ $this->validator->setFunctionParams( $arguments, $this->getParameterInfo(), $this->getDefaultParameters() );
156156 }
157157
158 - $this->validator->validateAndFormatParameters();
 158+ $this->validator->validateParameters();
159159
160 - if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) {
161 - $this->validator->correctInvalidParams();
162 - }
163 -
164160 if ( $this->validator->hasFatalError() ) {
165161 // TODO
166162 $output = 'Demo: fatal error';
167163 }
168164 else {
169 - $output = $this->render( $this->validator->getValidParams( false ) );
 165+ $this->validator->formatParameters();
 166+
 167+ $output = $this->render( $this->validator->getParameterValues() );
170168 }
171169
172170 return $output;
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -20,6 +20,26 @@
2121 const TYPE_CHAR = 'char';
2222
2323 /**
 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+ /**
2444 * Indicates if the parameter value should be lowercased.
2545 *
2646 * @since 0.4
@@ -112,6 +132,15 @@
113133 protected $originalValue;
114134
115135 /**
 136+ * The value of the parameter.
 137+ *
 138+ * @since 0.4
 139+ *
 140+ * @var mixed
 141+ */
 142+ protected $value;
 143+
 144+ /**
116145 * Keeps track of how many times the parameter has been set by the user.
117146 * This is used to detect overrides and for figuring out a parameter is missing.
118147 *
@@ -253,40 +282,83 @@
254283 }
255284
256285 /**
 286+ *
 287+ *
257288 * @since 0.4
258289 *
259290 * @param string $paramName
260291 * @param string $paramValue
 292+ *
 293+ * @return boolean
261294 */
262295 public function setUserValue( $paramName, $paramValue ) {
263 - if ( $this->setCount > 0 && true /* TODO: accept overridng? */ ) {
 296+ if ( $this->setCount > 0 && !self::$acceptOverriding ) {
264297 // 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;
265312 }
266313 else {
267314 $this->originalName = $paramName;
268 - $this->paramValue = $paramValue;
 315+ $this->originalValue = $paramValue;
269316
270 - $this->setCount++;
 317+ $this->cleanValue();
 318+
 319+ $this->setCount++;
 320+
 321+ return true;
271322 }
272323 }
273324
274325 /**
 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+ /**
275339 * 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.
276342 *
277343 * @since 0.4
 344+ *
 345+ * @return boolean If there where no fatal errors
278346 */
279347 public function validate() {
280348 if ( $this->setCount == 0 ) {
281349 if ( $this->isRequired() ) {
282350 // TODO: fatal error
 351+ $success = false;
283352 }
284353 else {
285 - $this->validateCriteria( $this->default );
 354+ $success = true;
 355+ $this->value = $this->default;
286356 }
287357 }
288358 else {
289 - $this->validateCriteria( $this->originalValue );
 359+ $success = $this->validateCriteria( $this->originalValue );
290360 }
 361+
 362+ return $success;
291363 }
292364
293365 /**
@@ -295,13 +367,29 @@
296368 * @since 0.4
297369 *
298370 * @param string $value
 371+ *
 372+ * @return boolean If there where no fatal errors
299373 */
300374 protected function validateCriteria( $value ) {
 375+ $success = true;
 376+ $hasError = false;
 377+
301378 foreach ( $this->getCriteria() as $criterion ) {
302379 if ( !$criterion->validate( $value ) ) {
 380+ $hasError = true;
303381
 382+ if ( !self::$accumulateParameterErrors ) {
 383+ break;
 384+ }
304385 }
305386 }
 387+
 388+ // TODO: move this to a nicer place
 389+ if ( $hasError ) {
 390+ $this->value = $this->default;
 391+ }
 392+
 393+ return $success;
306394 }
307395
308396 /**
@@ -316,6 +404,17 @@
317405 }
318406
319407 /**
 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+ /**
320419 * Returns if the parameter is a required one or not.
321420 *
322421 * @since 0.4
Index: trunk/extensions/Validator/includes/Validator.php
@@ -18,30 +18,8 @@
1919 class Validator {
2020
2121 /**
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+ *
4624 * @var array Holder for the formatting functions.
4725 */
4826 protected static $mOutputFormats = array(
@@ -55,37 +33,15 @@
5634 );
5735
5836 /**
59 - * Array containing parameter definitions.
 37+ * Array containing the parameters.
6038 *
6139 * @since 0.4
6240 *
6341 * @var array of Parameter
6442 */
65 - protected $parameterInfo;
 43+ protected $parameters;
6644
6745 /**
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 - /**
9046 * List of ValidatorError.
9147 *
9248 * @since 0.4
@@ -95,8 +51,8 @@
9652 protected $errors = array();
9753
9854 /**
 55+ * Name of the element that's being validated.
9956 *
100 - *
10157 * @since 0.4
10258 *
10359 * @var string
@@ -115,6 +71,8 @@
11672 }
11773
11874 /**
 75+ * @deprecated TODO: remove
 76+ *
11977 * Adds a new output format and the formatting function that should validate values of this type.
12078 * You can use this function to override existing criteria type handlers.
12179 *
@@ -127,47 +85,17 @@
12886 }
12987
13088 /**
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 - /**
16389 * Determines the names and values of all parameters. Also takes care of default parameters.
16490 * After that the resulting parameter list is passed to Validator::setParameters
16591 *
 92+ * @since 0.4
 93+ *
16694 * @param array $rawParams
16795 * @param array $parameterInfo
16896 * @param array $defaultParams
16997 * @param boolean $toLower Indicates if the parameter values should be put to lower case. Defaults to true.
17098 */
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 ) {
172100 $this->cleanParameterInfo( $parameterInfo );
173101
174102 $parameters = array();
@@ -187,8 +115,6 @@
188116 if ( count( $defaultParams ) > 0 ) {
189117 $defaultParam = strtolower( array_shift( $defaultParams ) );
190118
191 - $this->lowerCaseIfNeeded( $parts[0], $defaultParam, $parameterInfo, $toLower );
192 -
193119 $parameters[$defaultParam] = array(
194120 'original-value' => trim( $parts[0] ),
195121 'default' => $defaultNr,
@@ -202,8 +128,6 @@
203129 } else {
204130 $paramName = trim( strtolower( $parts[0] ) );
205131
206 - $this->lowerCaseIfNeeded( $parts[1], $paramName, $parameterInfo, $toLower );
207 -
208132 $parameters[$paramName] = array(
209133 'original-value' => trim( $parts[1] ),
210134 'default' => false,
@@ -238,8 +162,8 @@
239163 public function setParameters( array $parameters, array $parameterInfo, $toLower = true ) {
240164 $this->cleanParameterInfo( $parameterInfo );
241165
242 - $this->parameterInfo = $parameterInfo;
243 -
 166+ $this->parameters = $parameterInfo;
 167+
244168 // Loop through all the user provided parameters, and destinguise between those that are allowed and those that are not.
245169 foreach ( $parameters as $paramName => $paramData ) {
246170 $paramName = trim( strtolower( $paramName ) );
@@ -249,42 +173,21 @@
250174
251175 // If the parameter is found in the list of allowed ones, add it to the $mParameters array.
252176 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'] );
273181 }
274182 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 );
285188 }
 189+
286190 }
287191 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;
289192 $this->registerError(
290193 wfMsgExt(
291194 'validator_error_unknown_argument',
@@ -298,18 +201,37 @@
299202 }
300203
301204 /**
302 - * Lowercases the provided $paramValue if needed.
 205+ * Registers an error.
303206 *
304 - * @since 0.3.6
 207+ * @since 0.4
305208 *
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
310212 */
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+ }
314236 }
315237
316238 /**
@@ -323,11 +245,11 @@
324246 protected function getMainParamName( $paramName ) {
325247 $result = false;
326248
327 - if ( array_key_exists( $paramName, $this->parameterInfo ) ) {
 249+ if ( array_key_exists( $paramName, $this->parameters ) ) {
328250 $result = $paramName;
329251 }
330252 else {
331 - foreach ( $this->parameterInfo as $name => $parameter ) {
 253+ foreach ( $this->parameters as $name => $parameter ) {
332254 if ( $parameter->hasAlias( $paramName ) ) {
333255 $result = $name;
334256 break;
@@ -336,19 +258,17 @@
337259 }
338260
339261 return $result;
340 - }
 262+ }
341263
342264 /**
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).
346266 *
347 - * The 'value' element is set here, either by the cleaned 'original-value' or default.
 267+ * @since 0.4
348268 */
349 - public function validateAndFormatParameters() {
 269+ public function validateParameters() {
350270 $dependencyList = array();
351271
352 - foreach ( $this->parameterInfo as $paramName => $parameter ) {
 272+ foreach ( $this->parameters as $paramName => $parameter ) {
353273 $dependencyList[$paramName] = $parameter->dependencies;
354274 }
355275
@@ -356,275 +276,34 @@
357277 $orderedParameters = $sorter->doSort();
358278
359279 foreach ( $orderedParameters as $paramName ) {
360 - $parameter = $this->parameterInfo[$paramName];
 280+ $parameter = $this->parameters[$paramName];
361281
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();
409284
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;
415288 }
416 -
417 - $this->mParameters[$name]['value'] = $value;
418289 }
419290 }
420291
421292 /**
422 - * Valides the provided parameter.
 293+ * Applies the output formats to all parameters.
423294 *
424 - * This method itself validates the list criteria, if any. After this the regular criteria
425 - * are validated by calling the doItemValidation method.
426 - *
427295 * @param string $name
428 - *
429 - * @return boolean Indicates whether there the parameter value(s) is/are valid.
430296 */
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+ }
436302 }
437 -
438 - return $hasNoErrors;
439303 }
440304
441305 /**
442 - * Validates the list criteria for a parameter, if there are any.
 306+ * @deprecated TODO: remove
443307 *
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 - /**
629308 * Calls the formatting function for the provided output format with these parameters:
630309 * - parameter value: ByRef for easy manipulation.
631310 * - parameter name: For lookups in the param info array.
@@ -639,12 +318,12 @@
640319 // The remaining ones will be any extra arguments.
641320 $outputType = strtolower( array_shift( $typeInfo ) );
642321
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'];
645324 }
646325
647326 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 );
649328 $parameters = array_merge( $parameters, $typeInfo );
650329 call_user_func_array( self::$mOutputFormats[$outputType], $parameters );
651330 }
@@ -654,43 +333,34 @@
655334 }
656335
657336 /**
658 - * Returns the valid parameters.
659 - *
660 - * @param boolean $includeMetaData
661 - *
 337+ * Returns the parameters.
 338+ *
 339+ * @since 0.4
 340+ *
662341 * @return array
663342 */
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;
678345 }
679 -
 346+
680347 /**
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+ *
683353 * @return array
684354 */
685 - public static function getUnknownParams() {
686 - $unknownParams = array();
 355+ public function getParameterValues() {
 356+ $parameters = array();
687357
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+ }
691361
692 - return $unknownParams;
 362+ return $parameters;
693363 }
694 -
 364+
695365 /**
696366 * Returns the errors.
697367 *
@@ -731,7 +401,7 @@
732402 /**
733403 * Returns an error message for a criteria validation that failed.
734404 *
735 - * TODO: integrate this further with the hook mechanisms
 405+ * TODO: move individual messgaes over to their corresponding criterion class
736406 * TODO: proper escaping
737407 *
738408 * @since 0.4

Status & tagging log