r72432 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72431‎ | r72432 | r72433 >
Date:14:29, 5 September 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4 - moving over parameter validation functionality
Modified paths:
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/ParameterCriterion.php (modified) (history)
  • /trunk/extensions/Validator/includes/ValidationFunctions.php (deleted) (history)
  • /trunk/extensions/Validator/includes/Validator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Validator/includes/ValidationFunctions.php
@@ -1,148 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Class holding variouse static methods for the validation of parameters that have to comply to cetrain criteria.
6 - * Functions are called by Validator with the parameters $value, $arguments, where $arguments is optional.
7 - *
8 - * @file ValidationFunctions.php
9 - * @ingroup Validator
10 - *
11 - * @author Jeroen De Dauw
12 - */
13 -final class ValidationFunctions {
14 -
15 - /**
16 - * Returns whether the provided value, which must be a number, is within a certain range. Upper bound included.
17 - *
18 - * @param $value
19 - * @param array $metaData
20 - * @param mixed $lower
21 - * @param mixed $upper
22 - *
23 - * @return boolean
24 - */
25 - public static function in_range( $value, $name, array $parameters, $lower = false, $upper = false ) {
26 - if ( ! is_numeric( $value ) ) return false;
27 - $value = (int)$value;
28 - if ( $lower !== false && $value < $lower ) return false;
29 - if ( $upper !== false && $value > $upper ) return false;
30 - return true;
31 - }
32 -
33 - /**
34 - * Returns whether the string value is not empty. Not empty is defined as having at least one character after trimming.
35 - *
36 - * @param $value
37 - * @param array $metaData
38 - *
39 - * @return boolean
40 - */
41 - public static function not_empty( $value, $name, array $parameters ) {
42 - return strlen( trim( $value ) ) > 0;
43 - }
44 -
45 - /**
46 - * Returns whether the string value is not empty. Not empty is defined as having at least one character after trimming.
47 - *
48 - * @param $value
49 - * @param array $metaData
50 - *
51 - * @return boolean
52 - */
53 - public static function in_array( $value, $name, array $parameters ) {
54 - // TODO: It's possible the way the allowed values are passed here is quite inneficient...
55 - $params = func_get_args();
56 - array_shift( $params ); // Ommit the value
57 - return in_array( $value, $params );
58 - }
59 -
60 - /**
61 - * Returns whether a variable is an integer or an integer string. Uses the native PHP function.
62 - *
63 - * @param $value
64 - * @param array $metaData
65 - *
66 - * @return boolean
67 - */
68 - public static function is_integer( $value, $name, array $parameters ) {
69 - return ctype_digit( (string)$value );
70 - }
71 -
72 - /**
73 - * Returns whether the length of the value is within a certain range. Upper bound included.
74 - *
75 - * @param string $value
76 - * @param array $metaData
77 - * @param mixed $lower
78 - * @param mixed $upper
79 - *
80 - * @return boolean
81 - */
82 - public static function has_length( $value, $name, array $parameters, $lower = false, $upper = false ) {
83 - return self::in_range( strlen( $value ), $lower, $upper );
84 - }
85 -
86 - /**
87 - * Returns whether the amount of items in the list is within a certain range. Upper bound included.
88 - *
89 - * @param array $values
90 - * @param array $metaData
91 - * @param mixed $lower
92 - * @param mixed $upper
93 - *
94 - * @return boolean
95 - */
96 - public static function has_item_count( array $values, $name, array $parameters, $lower = false, $upper = false ) {
97 - return self::in_range( count( $values ), $lower, $upper );
98 - }
99 -
100 - /**
101 - * Returns whether the list of values does not have any duplicates.
102 - *
103 - * @param array $values
104 - * @param array $metaData
105 - *
106 - * @return boolean
107 - */
108 - public static function has_unique_items( array $values, $name, array $parameters ) {
109 - return count( $values ) == count( array_unique( $values ) );
110 - }
111 -
112 - /**
113 - * Returns the result of preg_match.
114 - *
115 - * @param string $value
116 - * @param array $metaData
117 - * @param string $pattern
118 - *
119 - * @return boolean
120 - */
121 - public static function regex( $value, $name, array $parameters, $pattern ) {
122 - return (bool)preg_match( $pattern, $value );
123 - }
124 -
125 - /**
126 - * Wrapper for the native is_numeric function.
127 - *
128 - * @param $value
129 - * @param array $metaData
130 - *
131 - * @return boolean
132 - */
133 - public static function is_numeric( $value, $name, array $parameters ) {
134 - return is_numeric( $value );
135 - }
136 -
137 - /**
138 - * Returns if the value is a floating point number.
139 - * Does NOT check the type of the variable like the native is_float function.
140 - *
141 - * @param $value
142 - * @param array $metaData
143 - *
144 - * @return boolean
145 - */
146 - public static function is_float( $value, $name, array $parameters ) {
147 - return preg_match( '/^\d+(\.\d+)?$/', $value );
148 - }
149 -}
\ No newline at end of file
Index: trunk/extensions/Validator/includes/ParameterCriterion.php
@@ -38,7 +38,39 @@
3939 * @return ParameterCriterion
4040 */
4141 public static function newFromArray( $name, array $definition ) {
 42+ $bcMap = array(
 43+ 'in_array' => 'CriterionInArray',
 44+ 'is_numeric' => 'CriterionIsNumeric',
 45+ 'in_range' => 'CriterionInrange',
 46+ 'is_float' => 'CriterionIsFloat',
 47+ 'is_integer' => 'CriterionIsInteger',
 48+ 'not_empty' => 'CriterionNotEmpty',
 49+ 'has_length' => 'CriterionHasLength',
 50+ 'regex' => 'CriterionMatchesRegex',
 51+ 'item_count' => 'CriterionItemCount',
 52+ 'unique_items' => 'CriterionUniqueItems',
 53+ );
4254
 55+ $className = $bcMap[$name];
 56+
 57+ switch ( $name ) {
 58+ case 'in_array':
 59+ $criterion = new $className( $definition );
 60+ break;
 61+ case 'in_range': case 'item_count' : case 'has_length' :
 62+ if ( count( $definition ) > 1 ) {
 63+ $criterion = new $className( $definition[0], $definition[1] );
 64+ }
 65+ else {
 66+ $criterion = new $className( $definition[0] );
 67+ }
 68+ break;
 69+ default:
 70+ $criterion = new $className();
 71+ break;
 72+ }
 73+
 74+ return $criterion;
4375 }
4476
4577 /**
@@ -50,8 +82,4 @@
5183
5284 }
5385
54 - protected function getValidationFunction() {
55 -
56 - }
57 -
5886 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -232,12 +232,29 @@
233233 $this->type = $type;
234234 $this->default = $default;
235235 $this->aliases = $aliases;
 236+
 237+ $this->cleanCriteria( $criteria );
236238 $this->criteria = $criteria;
237239 }
238240
239241 /**
 242+ * Ensures all Validator 3.x-style criteria definitions are converted into ParameterCriterion instances.
 243+ *
240244 * @since 0.4
241245 *
 246+ * @param array $criteria
 247+ */
 248+ protected function cleanCriteria( array &$criteria ) {
 249+ foreach ( $criteria as $key => &$criterion ) {
 250+ if ( !$criterion instanceof ParameterCriterion ) {
 251+ $criterion = ParameterCriterion::newFromArray( $key, $criterion );
 252+ }
 253+ }
 254+ }
 255+
 256+ /**
 257+ * @since 0.4
 258+ *
242259 * @param string $paramName
243260 * @param string $paramValue
244261 */
@@ -272,6 +289,13 @@
273290 }
274291 }
275292
 293+ /**
 294+ * Validates the provided value against all criteria.
 295+ *
 296+ * @since 0.4
 297+ *
 298+ * @param string $value
 299+ */
276300 protected function validateCriteria( $value ) {
277301 foreach ( $this->getCriteria() as $criterion ) {
278302 if ( !$criterion->validate( $value ) ) {
Index: trunk/extensions/Validator/includes/Validator.php
@@ -40,28 +40,6 @@
4141 * versus having the whole list be set to it's default.
4242 */
4343 public static $perItemValidation = true;
44 -
45 - /**
46 - * @var array Holder for the validation functions.
47 - */
48 - protected static $mValidationFunctions = array(
49 - 'in_array' => array( 'ValidationFunctions', 'in_array' ),
50 - 'in_range' => array( 'ValidationFunctions', 'in_range' ),
51 - 'is_numeric' => array( 'ValidationFunctions', 'is_numeric' ),
52 - 'is_float' => array( 'ValidationFunctions', 'is_float' ),
53 - 'is_integer' => array( 'ValidationFunctions', 'is_integer' ),
54 - 'not_empty' => array( 'ValidationFunctions', 'not_empty' ),
55 - 'has_length' => array( 'ValidationFunctions', 'has_length' ),
56 - 'regex' => array( 'ValidationFunctions', 'regex' ),
57 - );
58 -
59 - /**
60 - * @var array Holder for the list validation functions.
61 - */
62 - protected static $mListValidationFunctions = array(
63 - 'item_count' => array( 'ValidationFunctions', 'has_item_count' ),
64 - 'unique_items' => array( 'ValidationFunctions', 'has_unique_items' ),
65 - );
6644
6745 /**
6846 * @var array Holder for the formatting functions.
@@ -137,30 +115,6 @@
138116 }
139117
140118 /**
141 - * Adds a new criteria type and the validation function that should validate values of this type.
142 - * You can use this function to override existing criteria type handlers.
143 - *
144 - * @param string $criteriaName The name of the cirteria.
145 - * @param array $functionName The functions location. If it's a global function, only the name,
146 - * if it's in a class, first the class name, then the method name.
147 - */
148 - public static function addValidationFunction( $criteriaName, array $functionName ) {
149 - self::$mValidationFunctions[$criteriaName] = $functionName;
150 - }
151 -
152 - /**
153 - * Adds a new list criteria type and the validation function that should validate values of this type.
154 - * You can use this function to override existing criteria type handlers.
155 - *
156 - * @param string $criteriaName The name of the list cirteria.
157 - * @param array $functionName The functions location. If it's a global function, only the name,
158 - * if it's in a class, first the class name, then the method name.
159 - */
160 - public static function addListValidationFunction( $criteriaName, array $functionName ) {
161 - self::$mListValidationFunctions[strtolower( $criteriaName )] = $functionName;
162 - }
163 -
164 - /**
165119 * Adds a new output format and the formatting function that should validate values of this type.
166120 * You can use this function to override existing criteria type handlers.
167121 *
@@ -491,6 +445,7 @@
492446 protected function doListValidation( $name ) {
493447 $hasNoErrors = true;
494448
 449+ /* TODO
495450 foreach ( $this->parameterInfo[$name]->getListCriteria() as $criteriaName => $criteriaArgs ) {
496451 // Get the validation function. If there is no matching function, throw an exception.
497452 if ( array_key_exists( $criteriaName, self::$mListValidationFunctions ) ) {
@@ -522,6 +477,7 @@
523478 throw new Exception( 'There is no validation function for list criteria type ' . $criteriaName );
524479 }
525480 }
 481+ */
526482
527483 return $hasNoErrors;
528484 }
@@ -538,6 +494,7 @@
539495
540496 $value = &$this->mParameters[$name]['value'];
541497
 498+ /* TODO
542499 // Go through all item criteria.
543500 foreach ( $this->parameterInfo[$name]->getCriteria() as $criteriaName => $criteriaArgs ) {
544501 // Get the validation function. If there is no matching function, throw an exception.
@@ -618,6 +575,7 @@
619576 throw new Exception( 'There is no validation function for criteria type ' . $criteriaName );
620577 }
621578 }
 579+ */
622580
623581 return $hasNoErrors;
624582 }
Index: trunk/extensions/Validator/Validator.php
@@ -61,7 +61,6 @@
6262 $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php';
6363 $wgAutoloadClasses['TopologicalSort'] = $incDir . 'TopologicalSort.php';
6464 $wgAutoloadClasses['ValidationFormats'] = $incDir . 'ValidationFormats.php';
65 -$wgAutoloadClasses['ValidationFunctions'] = $incDir . 'ValidationFunctions.php';
6665 $wgAutoloadClasses['ValidationManager'] = $incDir . 'ValidationManager.php'; // TODO: remove
6766 $wgAutoloadClasses['ValidatorError'] = $incDir . 'Validator_Error.php';
6867 $wgAutoloadClasses['ValidatorErrorHandler'] = $incDir . 'Validator_ErrorHandler.php';

Follow-up revisions

RevisionCommit summaryAuthorDate
r72433Follow up to r72432jeroendedauw14:29, 5 September 2010

Status & tagging log