r75077 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r75076‎ | r75077 | r75078 >
Date:09:24, 20 October 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Changes for 0.4.2 - threw out $parameter->lowerCaseValue because it was to evil
Modified paths:
  • /trunk/extensions/Validator/INSTALL (modified) (history)
  • /trunk/extensions/Validator/RELEASE-NOTES (modified) (history)
  • /trunk/extensions/Validator/Validator.php (modified) (history)
  • /trunk/extensions/Validator/includes/ListParameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/Parameter.php (modified) (history)
  • /trunk/extensions/Validator/includes/criteria/CriterionInArray.php (modified) (history)
  • /trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php (modified) (history)
  • /trunk/extensions/Validator/includes/manipulations/ParamManipulationFunctions.php (added) (history)

Diff [purge]

Index: trunk/extensions/Validator/INSTALL
@@ -1,4 +1,4 @@
2 -[[Validator 0.4.1]]
 2+[[Validator 0.4.2]]
33
44 Once you have downloaded the code, place the 'Validator' directory within
55 your MediaWiki 'extensions' directory. Then add the following code to your
Index: trunk/extensions/Validator/RELEASE-NOTES
@@ -6,6 +6,11 @@
77
88 == Validator change log ==
99
 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+
1015 === Validator 0.4.1 ===
1116 (2010-10-20)
1217
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
156 + native
Index: trunk/extensions/Validator/includes/ListParameter.php
@@ -118,12 +118,6 @@
119119 $item = trim( $item );
120120 }
121121 }
122 -
123 - if ( $this->lowerCaseValue ) {
124 - foreach ( $this->value as &$item ) {
125 - $item = strtolower( $item );
126 - }
127 - }
128122 }
129123
130124 /**
Index: trunk/extensions/Validator/includes/criteria/CriterionInArray.php
@@ -23,9 +23,21 @@
2424 protected $allowedValues;
2525
2626 /**
 27+ * If the values should match case.
 28+ *
 29+ * @since 0.4.2
 30+ *
 31+ * @var boolean
 32+ */
 33+ protected $careAboutCapitalization = false;
 34+
 35+ /**
2736 * Constructor.
2837 *
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.
3042 *
3143 * @since 0.4
3244 */
@@ -34,6 +46,17 @@
3547
3648 $args = func_get_args();
3749
 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+
3861 if ( count( $args ) > 1 ) {
3962 $this->allowedValues = $args;
4063 }
@@ -41,15 +64,24 @@
4265 $this->allowedValues = (array)$args[0];
4366 }
4467 else {
 68+ // Not a lot that will pass validation in this case :D
4569 $this->allowedValues = array();
4670 }
 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+ }
4776 }
4877
4978 /**
5079 * @see ItemParameterCriterion::validate
5180 */
5281 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+ );
5486 }
5587
5688 /**
Index: trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php
@@ -14,19 +14,33 @@
1515 class CriterionUniqueItems extends ListParameterCriterion {
1616
1717 /**
 18+ * If the values should match case.
 19+ *
 20+ * @since 0.4.2
 21+ *
 22+ * @var boolean
 23+ */
 24+ protected $careAboutCapitalization;
 25+
 26+ /**
1827 * Constructor.
1928 *
2029 * @since 0.4
2130 */
22 - public function __construct() {
 31+ public function __construct( $careAboutCapitalization = false ) {
2332 parent::__construct();
 33+
 34+ $this->careAboutCapitalization = $careAboutCapitalization;
2435 }
2536
2637 /**
2738 * @see ParameterCriterion::validate
2839 */
2940 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+ ) );
3145 }
3246
3347 }
\ No newline at end of file
Index: trunk/extensions/Validator/includes/Parameter.php
@@ -40,15 +40,6 @@
4141 public static $accumulateParameterErrors = false;
4242
4343 /**
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 - /**
5344 * Indicates if the parameter value should trimmed.
5445 *
5546 * @since 0.4
@@ -187,96 +178,6 @@
188179 protected $defaulted = false;
189180
190181 /**
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 - /**
281182 * Constructor.
282183 *
283184 * @since 0.4
@@ -427,10 +328,6 @@
428329 if ( $this->trimValue ) {
429330 $this->value = trim( $this->value );
430331 }
431 -
432 - if ( $this->lowerCaseValue ) {
433 - $this->value = strtolower( $this->value );
434 - }
435332 }
436333
437334 /**
Index: trunk/extensions/Validator/Validator.php
@@ -24,7 +24,7 @@
2525 die( 'Not an entry point.' );
2626 }
2727
28 -define( 'Validator_VERSION', '0.4.1' );
 28+define( 'Validator_VERSION', '0.4.2 alpha' );
2929
3030 // Register the internationalization file.
3131 $wgExtensionMessagesFiles['Validator'] = dirname( __FILE__ ) . '/Validator.i18n.php';
@@ -76,6 +76,7 @@
7777 $wgAutoloadClasses['ParamManipulationBoolean'] = $incDir . 'manipulations/ParamManipulationBoolean.php';
7878 $wgAutoloadClasses['ParamManipulationBoolstr'] = $incDir . 'manipulations/ParamManipulationBoolstr.php';
7979 $wgAutoloadClasses['ParamManipulationFloat'] = $incDir . 'manipulations/ParamManipulationFloat.php';
 80+$wgAutoloadClasses['ParamManipulationFunctions']= $incDir . 'manipulations/ParamManipulationFunctions.php';
8081 $wgAutoloadClasses['ParamManipulationImplode'] = $incDir . 'manipulations/ParamManipulationImplode.php';
8182 $wgAutoloadClasses['ParamManipulationInteger'] = $incDir . 'manipulations/ParamManipulationInteger.php';
8283

Follow-up revisions

RevisionCommit summaryAuthorDate
r75078Changes for 0.7.2, follow up to r75077jeroendedauw09:25, 20 October 2010

Status & tagging log