Index: trunk/extensions/Validator/INSTALL |
— | — | @@ -1,4 +1,4 @@ |
2 | | -[[Validator 0.4.1]] |
| 2 | +[[Validator 0.4.2]] |
3 | 3 | |
4 | 4 | Once you have downloaded the code, place the 'Validator' directory within |
5 | 5 | your MediaWiki 'extensions' directory. Then add the following code to your |
Index: trunk/extensions/Validator/RELEASE-NOTES |
— | — | @@ -6,6 +6,11 @@ |
7 | 7 | |
8 | 8 | == Validator change log == |
9 | 9 | |
| 10 | +=== Validator 0.4.2 === |
| 11 | +(2010-1x-xx) |
| 12 | + |
| 13 | +* Removed the lowerCaseValue field in the Parameter class and replaced it's functionality with a ParameterManipulation. |
| 14 | + |
10 | 15 | === Validator 0.4.1 === |
11 | 16 | (2010-10-20) |
12 | 17 | |
Index: trunk/extensions/Validator/includes/manipulations/ParamManipulationFunctions.php |
— | — | @@ -0,0 +1,54 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Parameter manipulation for assigning the value to the result of one |
| 6 | + * or more functions with as only argument the value itself. |
| 7 | + * |
| 8 | + * @since 0.4.2 |
| 9 | + * |
| 10 | + * @file ParamManipulationFunctions.php |
| 11 | + * @ingroup Validator |
| 12 | + * @ingroup ParameterManipulations |
| 13 | + * |
| 14 | + * @author Jeroen De Dauw |
| 15 | + */ |
| 16 | +class ParamManipulationFunctions extends ItemParameterManipulation { |
| 17 | + |
| 18 | + /** |
| 19 | + * The names of functions to apply. |
| 20 | + * |
| 21 | + * @since 0.4.2 |
| 22 | + * |
| 23 | + * @var array of callbacks |
| 24 | + */ |
| 25 | + protected $functions = array(); |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor. |
| 29 | + * |
| 30 | + * @since 0.4.2 |
| 31 | + * |
| 32 | + * You can provide an array with function names or pass each function name as a seperate argument. |
| 33 | + */ |
| 34 | + public function __construct() { |
| 35 | + parent::__construct(); |
| 36 | + |
| 37 | + $args = func_get_args(); |
| 38 | + |
| 39 | + if ( count( $args ) > 0 ) { |
| 40 | + $this->functions = is_array( $args[0] ) ? $args[0] : $args; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @see ItemParameterManipulation::doManipulation |
| 46 | + * |
| 47 | + * @since 0.4.2 |
| 48 | + */ |
| 49 | + public function doManipulation( &$value, Parameter $parameter, array &$parameters ) { |
| 50 | + foreach ( $this->functions as $function ) { |
| 51 | + $value = call_user_func( $function, $value ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | +} |
\ No newline at end of file |
Property changes on: trunk/extensions/Validator/includes/manipulations/ParamManipulationFunctions.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 56 | + native |
Index: trunk/extensions/Validator/includes/ListParameter.php |
— | — | @@ -118,12 +118,6 @@ |
119 | 119 | $item = trim( $item ); |
120 | 120 | } |
121 | 121 | } |
122 | | - |
123 | | - if ( $this->lowerCaseValue ) { |
124 | | - foreach ( $this->value as &$item ) { |
125 | | - $item = strtolower( $item ); |
126 | | - } |
127 | | - } |
128 | 122 | } |
129 | 123 | |
130 | 124 | /** |
Index: trunk/extensions/Validator/includes/criteria/CriterionInArray.php |
— | — | @@ -23,9 +23,21 @@ |
24 | 24 | protected $allowedValues; |
25 | 25 | |
26 | 26 | /** |
| 27 | + * If the values should match case. |
| 28 | + * |
| 29 | + * @since 0.4.2 |
| 30 | + * |
| 31 | + * @var boolean |
| 32 | + */ |
| 33 | + protected $careAboutCapitalization = false; |
| 34 | + |
| 35 | + /** |
27 | 36 | * Constructor. |
28 | 37 | * |
29 | | - * @param mixed $allowedValues |
| 38 | + * You can specify the allowed values either by passing an array, |
| 39 | + * or by passing each value as an argument. You can also specify |
| 40 | + * if the criterion should care about capitalization or not by |
| 41 | + * adding a boolean as last argument. This value default to false. |
30 | 42 | * |
31 | 43 | * @since 0.4 |
32 | 44 | */ |
— | — | @@ -34,6 +46,17 @@ |
35 | 47 | |
36 | 48 | $args = func_get_args(); |
37 | 49 | |
| 50 | + $lastElement = array_pop( $args ); |
| 51 | + |
| 52 | + if ( is_bool( $lastElement ) ) { |
| 53 | + // The element is a boolean, so it's the capitalization parameter. |
| 54 | + $this->careAboutCapitalization = $lastElement; |
| 55 | + } |
| 56 | + else { |
| 57 | + // Add the element back to the array. |
| 58 | + $args[] = $lastElement; |
| 59 | + } |
| 60 | + |
38 | 61 | if ( count( $args ) > 1 ) { |
39 | 62 | $this->allowedValues = $args; |
40 | 63 | } |
— | — | @@ -41,15 +64,24 @@ |
42 | 65 | $this->allowedValues = (array)$args[0]; |
43 | 66 | } |
44 | 67 | else { |
| 68 | + // Not a lot that will pass validation in this case :D |
45 | 69 | $this->allowedValues = array(); |
46 | 70 | } |
| 71 | + |
| 72 | + if ( !$this->careAboutCapitalization ) { |
| 73 | + // If case doesn't matter, lowercase everything and later on compare a lowercased value. |
| 74 | + $this->allowedValues = array_map( 'strtolower', $this->allowedValues ); |
| 75 | + } |
47 | 76 | } |
48 | 77 | |
49 | 78 | /** |
50 | 79 | * @see ItemParameterCriterion::validate |
51 | 80 | */ |
52 | 81 | protected function doValidation( $value, Parameter $parameter, array $parameters ) { |
53 | | - return in_array( $value, $this->allowedValues ); |
| 82 | + return in_array( |
| 83 | + $this->careAboutCapitalization ? $value : strtolower( $value ), |
| 84 | + $this->allowedValues |
| 85 | + ); |
54 | 86 | } |
55 | 87 | |
56 | 88 | /** |
Index: trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php |
— | — | @@ -14,19 +14,33 @@ |
15 | 15 | class CriterionUniqueItems extends ListParameterCriterion { |
16 | 16 | |
17 | 17 | /** |
| 18 | + * If the values should match case. |
| 19 | + * |
| 20 | + * @since 0.4.2 |
| 21 | + * |
| 22 | + * @var boolean |
| 23 | + */ |
| 24 | + protected $careAboutCapitalization; |
| 25 | + |
| 26 | + /** |
18 | 27 | * Constructor. |
19 | 28 | * |
20 | 29 | * @since 0.4 |
21 | 30 | */ |
22 | | - public function __construct() { |
| 31 | + public function __construct( $careAboutCapitalization = false ) { |
23 | 32 | parent::__construct(); |
| 33 | + |
| 34 | + $this->careAboutCapitalization = $careAboutCapitalization; |
24 | 35 | } |
25 | 36 | |
26 | 37 | /** |
27 | 38 | * @see ParameterCriterion::validate |
28 | 39 | */ |
29 | 40 | public function validate( Parameter $parameter, array $parameters ) { |
30 | | - return count( $parameter->getValue() ) == count( array_unique( $parameter->getValue() ) ); |
| 41 | + return count( $parameter->getValue() ) |
| 42 | + == count( array_unique( |
| 43 | + $this->careAboutCapitalization ? $parameter->getValue() : array_map( 'strtolower', $parameter->getValue() ) |
| 44 | + ) ); |
31 | 45 | } |
32 | 46 | |
33 | 47 | } |
\ No newline at end of file |
Index: trunk/extensions/Validator/includes/Parameter.php |
— | — | @@ -40,15 +40,6 @@ |
41 | 41 | public static $accumulateParameterErrors = false; |
42 | 42 | |
43 | 43 | /** |
44 | | - * Indicates if the parameter value should be lowercased. |
45 | | - * |
46 | | - * @since 0.4 |
47 | | - * |
48 | | - * @var boolean |
49 | | - */ |
50 | | - public $lowerCaseValue = true; |
51 | | - |
52 | | - /** |
53 | 44 | * Indicates if the parameter value should trimmed. |
54 | 45 | * |
55 | 46 | * @since 0.4 |
— | — | @@ -187,96 +178,6 @@ |
188 | 179 | protected $defaulted = false; |
189 | 180 | |
190 | 181 | /** |
191 | | - * Returns a new instance of Parameter by converting a Validator 3.x-style parameter array definition. |
192 | | - * Note: this method is for backward compatibility and should not be used in new code. |
193 | | - * |
194 | | - * @since 0.4 |
195 | | - * |
196 | | - * @param string $name |
197 | | - * @param array $definition |
198 | | - * |
199 | | - * @return Parameter |
200 | | - */ |
201 | | - public static function newFromArray( $name, array $definition ) { |
202 | | - $isList = false; |
203 | | - $delimiter = ListParameter::DEFAULT_DELIMITER; |
204 | | - |
205 | | - if ( array_key_exists( 'type', $definition ) ) { |
206 | | - if ( is_array( $definition['type'] ) ) { |
207 | | - if ( count( $definition['type'] ) > 1 ) { |
208 | | - $isList = true; |
209 | | - |
210 | | - if ( count( $definition['type'] ) > 2 ) { |
211 | | - $delimiter = $definition['type'][2]; |
212 | | - } |
213 | | - } |
214 | | - |
215 | | - $type = $definition['type'][0]; |
216 | | - } |
217 | | - else { |
218 | | - $type = $definition['type']; |
219 | | - } |
220 | | - } |
221 | | - else { |
222 | | - $type = 'string'; |
223 | | - } |
224 | | - |
225 | | - if ( array_key_exists( 'required', $definition ) && $definition['required'] ) { |
226 | | - $default = null; |
227 | | - } |
228 | | - else { |
229 | | - $default = array_key_exists( 'default', $definition ) ? $definition['default'] : ''; |
230 | | - } |
231 | | - |
232 | | - if ( $isList ) { |
233 | | - $parameter = new ListParameter( |
234 | | - $name, |
235 | | - $delimiter, |
236 | | - $type, |
237 | | - $default, |
238 | | - array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(), |
239 | | - array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array() |
240 | | - ); |
241 | | - } |
242 | | - else { |
243 | | - $parameter = new Parameter( |
244 | | - $name, |
245 | | - $type, |
246 | | - $default, |
247 | | - array_key_exists( 'aliases', $definition ) ? $definition['aliases'] : array(), |
248 | | - array_key_exists( 'criteria', $definition ) ? $definition['criteria'] : array() |
249 | | - ); |
250 | | - } |
251 | | - |
252 | | - if ( array_key_exists( 'output-types', $definition ) ) { |
253 | | - $types = array(); |
254 | | - |
255 | | - for ( $i = 0, $c = count( $definition['output-types'] ); $i < $c; $i++ ) { |
256 | | - if ( !is_array( $definition['output-types'][$i] ) ) { |
257 | | - $definition['output-types'][$i] = array( $definition['output-types'][$i] ); |
258 | | - } |
259 | | - |
260 | | - $types[$name] = $definition['output-types'][$i]; |
261 | | - } |
262 | | - } |
263 | | - elseif ( array_key_exists( 'output-type', $definition ) ) { |
264 | | - if ( ! is_array( $definition['output-type'] ) ) { |
265 | | - $definition['output-type'] = array( $definition['output-type'] ); |
266 | | - } |
267 | | - } |
268 | | - |
269 | | - if ( array_key_exists( 'tolower', $definition ) ) { |
270 | | - $parameter->lowerCaseValue = (bool)$definition['tolower']; |
271 | | - } |
272 | | - |
273 | | - if ( array_key_exists( 'dependencies', $definition ) ) { |
274 | | - $parameter->addDependencies( $definition['dependencies'] ); |
275 | | - } |
276 | | - |
277 | | - return $parameter; |
278 | | - } |
279 | | - |
280 | | - /** |
281 | 182 | * Constructor. |
282 | 183 | * |
283 | 184 | * @since 0.4 |
— | — | @@ -427,10 +328,6 @@ |
428 | 329 | if ( $this->trimValue ) { |
429 | 330 | $this->value = trim( $this->value ); |
430 | 331 | } |
431 | | - |
432 | | - if ( $this->lowerCaseValue ) { |
433 | | - $this->value = strtolower( $this->value ); |
434 | | - } |
435 | 332 | } |
436 | 333 | |
437 | 334 | /** |
Index: trunk/extensions/Validator/Validator.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | die( 'Not an entry point.' ); |
26 | 26 | } |
27 | 27 | |
28 | | -define( 'Validator_VERSION', '0.4.1' ); |
| 28 | +define( 'Validator_VERSION', '0.4.2 alpha' ); |
29 | 29 | |
30 | 30 | // Register the internationalization file. |
31 | 31 | $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php'; |
— | — | @@ -76,6 +76,7 @@ |
77 | 77 | $wgAutoloadClasses['ParamManipulationBoolean'] = $incDir . 'manipulations/ParamManipulationBoolean.php'; |
78 | 78 | $wgAutoloadClasses['ParamManipulationBoolstr'] = $incDir . 'manipulations/ParamManipulationBoolstr.php'; |
79 | 79 | $wgAutoloadClasses['ParamManipulationFloat'] = $incDir . 'manipulations/ParamManipulationFloat.php'; |
| 80 | +$wgAutoloadClasses['ParamManipulationFunctions']= $incDir . 'manipulations/ParamManipulationFunctions.php'; |
80 | 81 | $wgAutoloadClasses['ParamManipulationImplode'] = $incDir . 'manipulations/ParamManipulationImplode.php'; |
81 | 82 | $wgAutoloadClasses['ParamManipulationInteger'] = $incDir . 'manipulations/ParamManipulationInteger.php'; |
82 | 83 | |