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 @@ |
39 | 39 | * @return ParameterCriterion |
40 | 40 | */ |
41 | 41 | 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 | + ); |
42 | 54 | |
| 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; |
43 | 75 | } |
44 | 76 | |
45 | 77 | /** |
— | — | @@ -50,8 +82,4 @@ |
51 | 83 | |
52 | 84 | } |
53 | 85 | |
54 | | - protected function getValidationFunction() { |
55 | | - |
56 | | - } |
57 | | - |
58 | 86 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -232,12 +232,29 @@ |
233 | 233 | $this->type = $type; |
234 | 234 | $this->default = $default; |
235 | 235 | $this->aliases = $aliases; |
| 236 | + |
| 237 | + $this->cleanCriteria( $criteria ); |
236 | 238 | $this->criteria = $criteria; |
237 | 239 | } |
238 | 240 | |
239 | 241 | /** |
| 242 | + * Ensures all Validator 3.x-style criteria definitions are converted into ParameterCriterion instances. |
| 243 | + * |
240 | 244 | * @since 0.4 |
241 | 245 | * |
| 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 | + * |
242 | 259 | * @param string $paramName |
243 | 260 | * @param string $paramValue |
244 | 261 | */ |
— | — | @@ -272,6 +289,13 @@ |
273 | 290 | } |
274 | 291 | } |
275 | 292 | |
| 293 | + /** |
| 294 | + * Validates the provided value against all criteria. |
| 295 | + * |
| 296 | + * @since 0.4 |
| 297 | + * |
| 298 | + * @param string $value |
| 299 | + */ |
276 | 300 | protected function validateCriteria( $value ) { |
277 | 301 | foreach ( $this->getCriteria() as $criterion ) { |
278 | 302 | if ( !$criterion->validate( $value ) ) { |
Index: trunk/extensions/Validator/includes/Validator.php |
— | — | @@ -40,28 +40,6 @@ |
41 | 41 | * versus having the whole list be set to it's default. |
42 | 42 | */ |
43 | 43 | 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 | | - ); |
66 | 44 | |
67 | 45 | /** |
68 | 46 | * @var array Holder for the formatting functions. |
— | — | @@ -137,30 +115,6 @@ |
138 | 116 | } |
139 | 117 | |
140 | 118 | /** |
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 | | - /** |
165 | 119 | * Adds a new output format and the formatting function that should validate values of this type. |
166 | 120 | * You can use this function to override existing criteria type handlers. |
167 | 121 | * |
— | — | @@ -491,6 +445,7 @@ |
492 | 446 | protected function doListValidation( $name ) { |
493 | 447 | $hasNoErrors = true; |
494 | 448 | |
| 449 | + /* TODO |
495 | 450 | foreach ( $this->parameterInfo[$name]->getListCriteria() as $criteriaName => $criteriaArgs ) { |
496 | 451 | // Get the validation function. If there is no matching function, throw an exception. |
497 | 452 | if ( array_key_exists( $criteriaName, self::$mListValidationFunctions ) ) { |
— | — | @@ -522,6 +477,7 @@ |
523 | 478 | throw new Exception( 'There is no validation function for list criteria type ' . $criteriaName ); |
524 | 479 | } |
525 | 480 | } |
| 481 | + */ |
526 | 482 | |
527 | 483 | return $hasNoErrors; |
528 | 484 | } |
— | — | @@ -538,6 +494,7 @@ |
539 | 495 | |
540 | 496 | $value = &$this->mParameters[$name]['value']; |
541 | 497 | |
| 498 | + /* TODO |
542 | 499 | // Go through all item criteria. |
543 | 500 | foreach ( $this->parameterInfo[$name]->getCriteria() as $criteriaName => $criteriaArgs ) { |
544 | 501 | // Get the validation function. If there is no matching function, throw an exception. |
— | — | @@ -618,6 +575,7 @@ |
619 | 576 | throw new Exception( 'There is no validation function for criteria type ' . $criteriaName ); |
620 | 577 | } |
621 | 578 | } |
| 579 | + */ |
622 | 580 | |
623 | 581 | return $hasNoErrors; |
624 | 582 | } |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -61,7 +61,6 @@ |
62 | 62 | $wgAutoloadClasses['Validator'] = $incDir . 'Validator.php'; |
63 | 63 | $wgAutoloadClasses['TopologicalSort'] = $incDir . 'TopologicalSort.php'; |
64 | 64 | $wgAutoloadClasses['ValidationFormats'] = $incDir . 'ValidationFormats.php'; |
65 | | -$wgAutoloadClasses['ValidationFunctions'] = $incDir . 'ValidationFunctions.php'; |
66 | 65 | $wgAutoloadClasses['ValidationManager'] = $incDir . 'ValidationManager.php'; // TODO: remove |
67 | 66 | $wgAutoloadClasses['ValidatorError'] = $incDir . 'Validator_Error.php'; |
68 | 67 | $wgAutoloadClasses['ValidatorErrorHandler'] = $incDir . 'Validator_ErrorHandler.php'; |