r65928 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65927‎ | r65928 | r65929 >
Date:01:47, 5 May 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.3 - improved validation manager class
Modified paths:
  • /trunk/extensions/Validator/Validator.class.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Manager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/Validator.class.php
@@ -27,8 +27,6 @@
2828 * TODO: break on fatal errors, such as missing required parameters that are dependencies
2929 *
3030 * TODO: correct invalid parameters in the main loop, as to have correct dependency handling
31 - *
32 - * FIXME: lists are broken
3331 */
3432 final class Validator {
3533
@@ -540,7 +538,11 @@
541539 }
542540
543541 /**
544 - * Calls the formatting function for the provided output format with the provided value.
 542+ * Calls the formatting function for the provided output format with these parameters:
 543+ * - parameter value: ByRef for easy manipulation.
 544+ * - parameter name: For lookups in the param info array.
 545+ * - parameter array: All data about the parameters gathered so far (this includes dependencies!).
 546+ * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters.
545547 *
546548 * @param string $name
547549 * @param array $typeInfo
@@ -555,13 +557,6 @@
556558 }
557559
558560 if ( array_key_exists( $outputType, self::$mOutputFormats ) ) {
559 - /**
560 - * Call the formatting function with these parameters:
561 - * - parameter value: ByRef for easy manipulation.
562 - * - parameter name: For lookups in the param info array.
563 - * - parameter array: All data about the parameters gathered so far (this includes dependencies!).
564 - * - output type info: Type info as provided by the parameter definition. This can be zero or more parameters.
565 - */
566561 $parameters = array( &$this->mParameters[$name]['formatted-value'], $name, $this->mParameters );
567562 $parameters = array_merge( $parameters, $typeInfo );
568563 call_user_func_array( self::$mOutputFormats[$outputType], $parameters );
@@ -574,17 +569,24 @@
575570 /**
576571 * Returns the valid parameters.
577572 *
 573+ * @param boolean $includeMetaData
 574+ *
578575 * @return array
579576 */
580 - public function getValidParams() {
581 - $validParams = array();
582 -
583 - foreach( $this->mValidParams as $name ) {
584 - $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value';
585 - $validParams[$name] = $this->mParameters[$name][$key];
 577+ public function getValidParams( $includeMetaData ) {
 578+ if ( $includeMetaData ) {
 579+ return $this->mValidParams;
586580 }
587 -
588 - return $validParams;
 581+ else {
 582+ $validParams = array();
 583+
 584+ foreach( $this->mValidParams as $name ) {
 585+ $key = array_key_exists( 'formatted-value', $this->mParameters[$name] ) ? 'formatted-value' : 'value';
 586+ $validParams[$name] = $this->mParameters[$name][$key];
 587+ }
 588+
 589+ return $validParams;
 590+ }
589591 }
590592
591593 /**
@@ -610,7 +612,36 @@
611613 public function getErrors() {
612614 return $this->mErrors;
613615 }
 616+
 617+ /**
 618+ * @return boolean
 619+ */
 620+ public function hasErrors() {
 621+ return count( $this->mErrors ) > 0;
 622+ }
 623+
 624+ /**
 625+ * Returns wether there are any fatal errors. Fatal errors are either missing or invalid required parameters,
 626+ * or simply any sort of error when the validation level is equal to (or bigger then) Validator_ERRORS_STRICT.
 627+ *
 628+ * @return boolean
 629+ */
 630+ public function hasFatalError() {
 631+ global $egValidatorErrorLevel;
 632+ $has = $this->hasErrors() && $egValidatorErrorLevel >= Validator_ERRORS_STRICT;
 633+
 634+ if ( !$has ) {
 635+ foreach ( $this->mErrors as $error ) {
 636+ if ( $error['type'] == 'missing' ) {
 637+ $has = true;
 638+ break;
 639+ }
 640+ }
 641+ }
614642
 643+ return $has;
 644+ }
 645+
615646 /**
616647 * Adds a new criteria type and the validation function that should validate values of this type.
617648 * You can use this function to override existing criteria type handlers.
Index: trunk/extensions/Validator/Validator_Manager.php
@@ -26,8 +26,8 @@
2727 */
2828 final class ValidatorManager {
2929
30 - private $errors = array();
31 -
 30+ private $validator;
 31+
3232 /**
3333 * Validates the provided parameters, and corrects them depending on the error level.
3434 *
@@ -40,44 +40,27 @@
4141 public function manageParameters( array $rawParameters, array $parameterInfo, array $defaultParams = array() ) {
4242 global $egValidatorErrorLevel;
4343
44 - $validator = new Validator();
 44+ $this->validator = new Validator();
4545
46 - $validator->parseAndSetParams( $rawParameters, $parameterInfo, $defaultParams );
47 - $hasNoErrors = $validator->validateAndFormatParameters();
48 -
49 - $hasFatalError = $hasNoErrors ? false : $this->hasFatalError();
50 -
51 - if ( !$hasNoErrors ) {
52 - if ( $egValidatorErrorLevel < Validator_ERRORS_STRICT ) $validator->correctInvalidParams();
53 - if ( $egValidatorErrorLevel >= Validator_ERRORS_WARN ) $this->errors = $validator->getErrors();
54 - }
 46+ $this->validator->parseAndSetParams( $rawParameters, $parameterInfo, $defaultParams );
 47+ $this->validator->validateAndFormatParameters();
5548
56 - return !$hasFatalError ? $validator->getValidParams() : false;
 49+ if ( $this->validator->hasErrors() && $egValidatorErrorLevel < Validator_ERRORS_STRICT ) {
 50+ $this->validator->correctInvalidParams();
 51+ }
 52+
 53+ return !$this->validator->hasFatalError();
5754 }
5855
5956 /**
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.
 57+ * Returns an array with the valid parameters.
6258 *
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.
 59+ * @param boolean $includeMetaData
6560 *
66 - * @return boolean
 61+ * @return array
6762 */
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 - }
80 -
81 - return $has;
 63+ public function getParameters( $includeMetaData = true ) {
 64+ return $this->validator->getValidParams( $includeMetaData );
8265 }
8366
8467 /**
@@ -87,14 +70,15 @@
8871 */
8972 public function getErrorList() {
9073 global $wgLang, $egValidatorErrorLevel;
91 - $errorCount = count( $this->errors ) ;
9274
93 - if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $errorCount > 0 ) {
94 - $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', $errorCount ) . '</b><br /><i>';
 75+ if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW && $this->validator->hasErrors() ) {
 76+ $rawErrors = $this->validator->getErrors();
 77+
 78+ $errorList = '<b>' . wfMsgExt( 'validator_error_parameters', 'parsemag', count( $rawErrors ) ) . '</b><br /><i>';
9579
9680 $errors = array();
9781
98 - foreach ( $this->errors as $error ) {
 82+ foreach ( $rawErrors as $error ) {
9983 $error['name'] = '<b>' . Sanitizer::escapeId( $error['name'] ) . '</b>';
10084
10185 if ( $error['type'] == 'unknown' ) {
@@ -162,8 +146,8 @@
163147
164148 return $errorList . implode( $errors, '<br />' ) . '</i><br />';
165149 }
166 - elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $errorCount > 0 ) {
167 - return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), $errorCount ) . '</b>';
 150+ elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && $this->validator->hasErrors() ) {
 151+ return '<b>' . wfMsgExt( 'validator_warning_parameters', array( 'parsemag' ), count( $this->validator->getErrors() ) ) . '</b>';
168152 }
169153 else {
170154 return '';

Follow-up revisions

RevisionCommit summaryAuthorDate
r65929Follow up to r65928jeroendedauw01:47, 5 May 2010
r65930Follow up to r65928jeroendedauw01:47, 5 May 2010

Status & tagging log