r60002 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r60001‎ | r60002 | r60003 >
Date:18:39, 12 December 2009
Author:jeroendedauw
Status:deferred (Comments)
Tags:
Comment:
Added parameter type handling.
Modified paths:
  • /trunk/extensions/Validator/Validator.class.php (modified) (history)
  • /trunk/extensions/Validator/Validator.i18n.php (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Functions.php (modified) (history)
  • /trunk/extensions/Validator/Validator_Manager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/Validator.class.php
@@ -22,8 +22,6 @@
2323 */
2424 final class Validator {
2525
26 - // TODO: add lower/upper case nullification
27 -
2826 /**
2927 * @var boolean Indicates whether parameters not found in the criteria list
3028 * should be stored in case they are not accepted. The default is false.
@@ -35,7 +33,7 @@
3634 * should be stored in case they are not accepted. The default is false.
3735 */
3836 public static $accumulateParameterErrors = false;
39 -
 37+
4038 /**
4139 * @var array Holder for the validation functions.
4240 */
@@ -43,11 +41,10 @@
4442 'in_array' => 'in_array',
4543 'in_range' => array( 'ValidatorFunctions', 'in_range' ),
4644 'is_numeric' => 'is_numeric',
 45+ 'is_integer' => array( 'ValidatorFunctions', 'is_integer' ),
4746 'not_empty' => array( 'ValidatorFunctions', 'not_empty' ),
4847 'all_in_array' => array( 'ValidatorFunctions', 'all_in_array' ),
4948 'any_in_array' => array( 'ValidatorFunctions', 'any_in_array' ),
50 - 'all_str_in_array' => array( 'ValidatorFunctions', 'all_str_in_array' ),
51 - 'any_str_in_array' => array( 'ValidatorFunctions', 'any_str_in_array' ),
5249 );
5350
5451 private $parameterInfo;
@@ -107,25 +104,31 @@
108105 if ( array_key_exists( $paramName, $this->rawParameters ) ) {
109106
110107 $paramValue = $this->rawParameters[$paramName];
 108+ $this->cleanParameter( $paramName, $paramValue );
111109 $validationErrors = $this->validateParameter( $paramName, $paramValue );
112110
113111 if ( count( $validationErrors ) == 0 ) {
 112+ // If the validation succeeded, add the parameter to the list of valid ones.
114113 $this->valid[$paramName] = $paramValue;
115114 }
116115 else {
 116+ // If the validation failed, add the parameter to the list of invalid ones and add the errors to the error list.
117117 $this->invalid[$paramName] = $paramValue;
118118 foreach ( $validationErrors as $error ) {
119119 $this->errors[] = array( 'error' => $error, 'name' => $paramName );
120120 }
121121 }
122122 }
123 - else { // If the user did not provide a value for this parameter, set the default if there is one.
124 - if ( array_key_exists( 'default', $paramInfo ) ) {
125 - $this->valid[$paramName] = $paramInfo['default'];
126 - }
127 - else { // If there is no default, the parameter must be provided, so add an error.
 123+ else {
 124+ // If the parameter is required, add a new error of type 'missing'.
 125+ if ( array_key_exists( 'required', $paramInfo ) && $paramInfo['required'] ) {
128126 $this->errors[] = array( 'error' => array( 'missing' ), 'name' => $paramName );
129127 }
 128+ else {
 129+ // Set the default value (or default 'default value' if none is provided), and ensure the type is correct.
 130+ $this->valid[$paramName] = array_key_exists( 'default', $paramInfo ) ? $paramInfo['default'] : '';
 131+ $this->setFinalType($this->valid[$paramName], $paramInfo);
 132+ }
130133 }
131134 }
132135
@@ -162,6 +165,48 @@
163166 }
164167
165168 /**
 169+ *
 170+ * @param $name
 171+ * @param $value
 172+ *
 173+ * @return unknown_type
 174+ */
 175+ private function cleanParameter( $name, &$value ) {
 176+ if (array_key_exists('default', $this->parameterInfo[$name])) {
 177+ $this->setFinalType($this->parameterInfo[$name]['default'], $this->parameterInfo[$name]);
 178+ }
 179+
 180+ // Ensure there is a criteria array.
 181+ if (! array_key_exists('criteria', $this->parameterInfo[$name] )) {
 182+ $this->parameterInfo[$name]['criteria'] = array();
 183+ }
 184+
 185+ if ( array_key_exists( 'type', $this->parameterInfo[$name] ) ) {
 186+ // Add type specific criteria.
 187+ switch(strtolower($this->parameterInfo[$name]['type'])) {
 188+ case 'integer':
 189+ $this->parameterInfo[$name]['criteria']['is_integer'] = array();
 190+ break;
 191+ case 'list' : case 'list-string' :
 192+ if (! array_key_exists('delimiter', $this->parameterInfo[$name])) $this->parameterInfo[$name]['delimiter'] = ',';
 193+ }
 194+
 195+ // Remove redundant spaces.
 196+ switch(strtolower($this->parameterInfo[$name]['type'])) {
 197+ case 'list' : case 'list-string':
 198+ // TODO: make sure the delimiter doesn't mess up the regex when it's a special char.
 199+ $value = preg_replace('/((\s)*' .
 200+ $this->parameterInfo[$name]['delimiter'] .
 201+ '(\s)*)/', $this->parameterInfo[$name]['delimiter'], $value);
 202+ break;
 203+ default :
 204+ $value = trim ($value);
 205+ break;
 206+ }
 207+ }
 208+ }
 209+
 210+ /**
166211 * Valides the provided parameter by matching the value against the criteria for the name.
167212 *
168213 * @param string $name
@@ -171,37 +216,68 @@
172217 */
173218 private function validateParameter( $name, $value ) {
174219 $errors = array();
175 -
176 - if ( array_key_exists( 'criteria', $this->parameterInfo[$name] ) ) {
177 - foreach ( $this->parameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) {
 220+
 221+ // Split list types into arrays.
 222+ switch(strtolower($this->parameterInfo[$name]['type'])) {
 223+ case 'list' : case 'list-string' :
 224+ $value = explode($this->parameterInfo[$name]['delimiter'], $value);
 225+ break;
 226+ }
 227+
 228+ // Go through all criteria.
 229+ foreach ( $this->parameterInfo[$name]['criteria'] as $criteriaName => $criteriaArgs ) {
 230+ // Get the validation function. If there is no matching function, throw an exception.
 231+ if (array_key_exists($criteriaName, self::$validationFunctions)) {
178232 $validationFunction = self::$validationFunctions[$criteriaName];
179 - $arguments = array( $value );
180 - if ( count( $criteriaArgs ) > 0 ) $arguments[] = $criteriaArgs;
181 - $isValid = call_user_func_array( $validationFunction, $arguments );
 233+ }
 234+ else {
 235+ throw new Exception( 'There is no validation function for criteria type ' . $criteriaName );
 236+ }
 237+
 238+ // Build up the array of parameters to be passed to call_user_func_array.
 239+ $arguments = array( $value );
 240+ if ( count( $criteriaArgs ) > 0 ) $arguments[] = $criteriaArgs;
 241+
 242+ // Call the validation function and store the result.
 243+ $isValid = call_user_func_array( $validationFunction, $arguments );
182244
183 - if ( ! $isValid ) {
184 - $errors[] = array( $criteriaName, $criteriaArgs, $value );
185 - if ( ! self::$accumulateParameterErrors ) break;
186 - }
 245+ // Add a new error when the validation failed, and break the loop if errors for one parameter should not be accumulated.
 246+ if ( ! $isValid ) {
 247+ $errors[] = array( $criteriaName, $criteriaArgs, $value );
 248+ if ( ! self::$accumulateParameterErrors ) break;
187249 }
188250 }
189251
190252 return $errors;
191253 }
 254+
 255+ /**
 256+ * Ensures the type of the value is correct.
 257+ *
 258+ * @param $value
 259+ * @param array $info
 260+ */
 261+ private function setFinalType(&$value, array $info) {
 262+ if (array_key_exists('type', $info)) {
 263+ switch(strtolower($info['type'])) {
 264+ case 'list-string' :
 265+ if (is_array($value)) {
 266+ $delimiter = array_key_exists('delimiter', $info) ? $info['delimiter'] : ',';
 267+ $value = implode($delimiter, $value);
 268+ }
 269+ break;
 270+ }
 271+ }
 272+ }
192273
193274 /**
194275 * Changes the invalid parameters to their default values, and changes their state to valid.
195276 */
196277 public function correctInvalidParams() {
197278 foreach ( $this->invalid as $paramName => $paramValue ) {
198 -
199 - if ( array_key_exists( 'default', $this->parameterInfo[$paramName] ) ) {
200 - unset( $this->invalid[$paramName] );
201 - $this->valid[$paramName] = $this->parameterInfo[$paramName]['default'];
202 - }
203 - else {
204 - throw new Exception( 'The default value for parameter ' . $paramName . ' is not set.' );
205 - }
 279+ unset( $this->invalid[$paramName] );
 280+ $this->valid[$paramName] = array_key_exists( 'default', $this->parameterInfo[$paramName] ) ? $this->parameterInfo[$paramName]['default'] : '';
 281+ $this->setFinalType($this->valid[$paramName], $this->parameterInfo[$paramName]);
206282 }
207283 }
208284
Index: trunk/extensions/Validator/Validator_Functions.php
@@ -14,6 +14,7 @@
1515
1616 /**
1717 * Class holding variouse static methods for the validation of parameters that have to comply to cetrain criteria.
 18+ * Functions are called by Validator with the parameters $value, $arguments, where $arguments is optional.
1819 *
1920 * @ingroup Validator
2021 *
@@ -22,9 +23,9 @@
2324 final class ValidatorFunctions {
2425
2526 /**
26 - * Returns whether the provided value, which must be a number, is within a certain range.
 27+ * Returns whether the provided value, which must be a number, is within a certain range. Upper bound not included.
2728 *
28 - * @param string $value
 29+ * @param $value
2930 * @param array $limits
3031 *
3132 * @return boolean
@@ -32,19 +33,30 @@
3334 public static function in_range( $value, array $limits ) {
3435 if ( ! is_numeric( $value ) ) return false;
3536 $value = (int)$value;
36 - return ( $value >= $limits[0] && $value <= $limits[1] ) || ( $value <= $limits[0] And $value >= $limits[1] );
 37+ return ( $value >= $limits[0] && $value < $limits[1] ) || ( $value < $limits[0] And $value >= $limits[1] );
3738 }
3839
3940 /**
4041 * Returns whether the string value is not empty. Not empty is defined as having at least one character after trimming.
4142 *
42 - * @param string $value
 43+ * @param $value
4344 *
4445 * @return boolean
4546 */
4647 public static function not_empty( $value ) {
4748 return strlen( trim( $value ) ) > 0;
4849 }
 50+
 51+ /**
 52+ * Returns whether a variable is an integer or an integer string. Uses the native PHP function.
 53+ *
 54+ * @param $value
 55+ *
 56+ * @return boolean
 57+ */
 58+ public static function is_integer( $value ) {
 59+ return is_numeric($value); // TODO: int check
 60+ }
4961
5062 /**
5163 * Returns if all items of the first array are present in the second one.
@@ -57,7 +69,7 @@
5870 public static function all_in_array( array $needles, array $haystack ) {
5971 $true = true;
6072 foreach ( $needles as $needle ) {
61 - if ( ! in_array( trim( $needle ), $haystack ) ) {
 73+ if ( ! in_array( $needle , $haystack ) ) {
6274 $true = false;
6375 break;
6476 }
@@ -76,39 +88,11 @@
7789 public static function any_in_array( array $needles, array $haystack ) {
7890 $true = false;
7991 foreach ( $needles as $needle ) {
80 - if ( in_array( trim( $needle ), $haystack ) ) {
 92+ if ( in_array( $needle , $haystack ) ) {
8193 $true = true;
8294 break;
8395 }
8496 }
8597 return $true;
8698 }
87 -
88 - /**
89 - * Returns if all items in the string are present in the array.
90 - * The first element of the $args array should be the delimieter,
91 - * the second one an array holding the haystack.
92 - *
93 - * @param string $needles
94 - * @param array $args
95 - *
96 - * @return boolean
97 - */
98 - public static function all_str_in_array( $needles, array $args ) {
99 - return self::all_in_array( explode( $args[0], $needles ), $args[1] );
100 - }
101 -
102 - /**
103 - * Returns if any items in the string are present in the array.
104 - * The first element of the $args array should be the delimieter,
105 - * the second one an array holding the haystack.
106 - *
107 - * @param string $needles
108 - * @param array $args
109 - *
110 - * @return boolean
111 - */
112 - public static function any_str_in_array( $needles, array $args ) {
113 - return self::any_in_array( explode( $args[0], $needles ), $args[1] );
114 - }
11599 }
Index: trunk/extensions/Validator/Validator_Manager.php
@@ -25,12 +25,12 @@
2626 private $errors = array();
2727
2828 /**
29 - * Validates the provided parameters, and corrects them depedning on the error level.
 29+ * Validates the provided parameters, and corrects them depending on the error level.
3030 *
31 - * @param array $rawParameters
32 - * @param array $parameterInfo
 31+ * @param array $rawParameters The raw parameters, as provided by the user.
 32+ * @param array $parameterInfo Array containing the parameter definitions, which are needed for validation and defaulting.
3333 *
34 - * @return boolean Indicates whether the regular output should be shown or not.
 34+ * @return array or false The valid parameters or false when the output should not be shown.
3535 */
3636 public function manageMapparameters( array $rawParameters, array $parameterInfo ) {
3737 global $egValidatorErrorLevel;
@@ -85,6 +85,9 @@
8686 case 'is_numeric' :
8787 $errors[] = wfMsgExt( 'validator_error_must_be_number', array( 'parsemag' ), $error['name'] );
8888 break;
 89+ case 'is_integer' :
 90+ $errors[] = wfMsgExt( 'validator_error_must_be_integer', array( 'parsemag' ), $error['name'] );
 91+ break;
8992 case 'in_array' : case 'all_in_array' : case 'all_str_in_array' :
9093 $items = $error['error'][0] == 'all_str_in_array' ? $error['error'][1][1] : $error['error'][1];
9194 $itemsText = $wgLang->listToText( $items );
Index: trunk/extensions/Validator/Validator.i18n.php
@@ -26,6 +26,7 @@
2727 'validator_error_required_missing' => 'The required parameter $1 is not provided.',
2828
2929 'validator_error_must_be_number' => 'Parameter $1 can only be a number.',
 30+ 'validator_error_must_be_integer' => 'Parameter $1 can only be an intger.',
3031 'validator_error_invalid_range' => 'Parameter $1 must be between $2 and $3.',
3132 'maps_error_accepts_only' => 'Parameter $1 only accepts {{PLURAL:$3|this value|these values}}: $2.',
3233 );
Index: trunk/extensions/Validator/Validator.php
@@ -24,7 +24,7 @@
2525 die( 'Not an entry point.' );
2626 }
2727
28 -define( 'Validator_VERSION', '0.1 a2' );
 28+define( 'Validator_VERSION', '0.1 a3' );
2929
3030 // Constants indicating the strictness of the parameter validation.
3131 define( 'Validator_ERRORS_NONE', 0 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r60003Fix typo from r60002siebrand18:54, 12 December 2009

Comments

#Comment by Raymond (talk | contribs)   22:20, 12 December 2009

Seen at translatewiki:

PHP Notice: Undefined index: type in /var/www/w/extensions/Validator/Validator.class.php on line 221

#Comment by Jeroen De Dauw (talk | contribs)   22:25, 12 December 2009

Status & tagging log