http://www.mediawiki.org/wiki/Special:Code/MediaWiki/72432
Revision: 72432
Author: jeroendedauw
Date: 2010-09-05 14:29:16 +0000 (Sun, 05 Sep 2010)
Log Message:
-----------
Changes for 0.4 - moving over parameter validation functionality
Modified Paths:
--------------
trunk/extensions/Validator/Validator.php
trunk/extensions/Validator/includes/Parameter.php
trunk/extensions/Validator/includes/ParameterCriterion.php
trunk/extensions/Validator/includes/Validator.php
Removed Paths:
-------------
trunk/extensions/Validator/includes/ValidationFunctions.php
Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php 2010-09-05 14:28:42 UTC (rev
72431)
+++ trunk/extensions/Validator/Validator.php 2010-09-05 14:29:16 UTC (rev
72432)
@@ -61,7 +61,6 @@
$wgAutoloadClasses['Validator'] = $incDir .
'Validator.php';
$wgAutoloadClasses['TopologicalSort'] = $incDir .
'TopologicalSort.php';
$wgAutoloadClasses['ValidationFormats'] = $incDir .
'ValidationFormats.php';
-$wgAutoloadClasses['ValidationFunctions'] = $incDir .
'ValidationFunctions.php';
$wgAutoloadClasses['ValidationManager'] = $incDir .
'ValidationManager.php'; // TODO: remove
$wgAutoloadClasses['ValidatorError'] = $incDir .
'Validator_Error.php';
$wgAutoloadClasses['ValidatorErrorHandler'] = $incDir .
'Validator_ErrorHandler.php';
Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php 2010-09-05 14:28:42 UTC
(rev 72431)
+++ trunk/extensions/Validator/includes/Parameter.php 2010-09-05 14:29:16 UTC
(rev 72432)
@@ -232,12 +232,29 @@
$this->type = $type;
$this->default = $default;
$this->aliases = $aliases;
+
+ $this->cleanCriteria( $criteria );
$this->criteria = $criteria;
}
/**
+ * Ensures all Validator 3.x-style criteria definitions are converted
into ParameterCriterion instances.
+ *
* @since 0.4
*
+ * @param array $criteria
+ */
+ protected function cleanCriteria( array &$criteria ) {
+ foreach ( $criteria as $key => &$criterion ) {
+ if ( !$criterion instanceof ParameterCriterion ) {
+ $criterion = ParameterCriterion::newFromArray(
$key, $criterion );
+ }
+ }
+ }
+
+ /**
+ * @since 0.4
+ *
* @param string $paramName
* @param string $paramValue
*/
@@ -272,6 +289,13 @@
}
}
+ /**
+ * Validates the provided value against all criteria.
+ *
+ * @since 0.4
+ *
+ * @param string $value
+ */
protected function validateCriteria( $value ) {
foreach ( $this->getCriteria() as $criterion ) {
if ( !$criterion->validate( $value ) ) {
Modified: trunk/extensions/Validator/includes/ParameterCriterion.php
===================================================================
--- trunk/extensions/Validator/includes/ParameterCriterion.php 2010-09-05
14:28:42 UTC (rev 72431)
+++ trunk/extensions/Validator/includes/ParameterCriterion.php 2010-09-05
14:29:16 UTC (rev 72432)
@@ -38,7 +38,39 @@
* @return ParameterCriterion
*/
public static function newFromArray( $name, array $definition ) {
+ $bcMap = array(
+ 'in_array' => 'CriterionInArray',
+ 'is_numeric' => 'CriterionIsNumeric',
+ 'in_range' => 'CriterionInrange',
+ 'is_float' => 'CriterionIsFloat',
+ 'is_integer' => 'CriterionIsInteger',
+ 'not_empty' => 'CriterionNotEmpty',
+ 'has_length' => 'CriterionHasLength',
+ 'regex' => 'CriterionMatchesRegex',
+ 'item_count' => 'CriterionItemCount',
+ 'unique_items' => 'CriterionUniqueItems',
+ );
+ $className = $bcMap[$name];
+
+ switch ( $name ) {
+ case 'in_array':
+ $criterion = new $className( $definition );
+ break;
+ case 'in_range': case 'item_count' : case 'has_length' :
+ if ( count( $definition ) > 1 ) {
+ $criterion = new $className(
$definition[0], $definition[1] );
+ }
+ else {
+ $criterion = new $className(
$definition[0] );
+ }
+ break;
+ default:
+ $criterion = new $className();
+ break;
+ }
+
+ return $criterion;
}
/**
@@ -50,8 +82,4 @@
}
- protected function getValidationFunction() {
-
- }
-
}
\ No newline at end of file
Deleted: trunk/extensions/Validator/includes/ValidationFunctions.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationFunctions.php 2010-09-05
14:28:42 UTC (rev 72431)
+++ trunk/extensions/Validator/includes/ValidationFunctions.php 2010-09-05
14:29:16 UTC (rev 72432)
@@ -1,148 +0,0 @@
-<?php
-
-/**
- * Class holding variouse static methods for the validation of parameters that
have to comply to cetrain criteria.
- * Functions are called by Validator with the parameters $value, $arguments,
where $arguments is optional.
- *
- * @file ValidationFunctions.php
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-final class ValidationFunctions {
-
- /**
- * Returns whether the provided value, which must be a number, is
within a certain range. Upper bound included.
- *
- * @param $value
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function in_range( $value, $name, array $parameters,
$lower = false, $upper = false ) {
- if ( ! is_numeric( $value ) ) return false;
- $value = (int)$value;
- if ( $lower !== false && $value < $lower ) return false;
- if ( $upper !== false && $value > $upper ) return false;
- return true;
- }
-
- /**
- * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function not_empty( $value, $name, array $parameters ) {
- return strlen( trim( $value ) ) > 0;
- }
-
- /**
- * Returns whether the string value is not empty. Not empty is defined
as having at least one character after trimming.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function in_array( $value, $name, array $parameters ) {
- // TODO: It's possible the way the allowed values are passed
here is quite inneficient...
- $params = func_get_args();
- array_shift( $params ); // Ommit the value
- return in_array( $value, $params );
- }
-
- /**
- * Returns whether a variable is an integer or an integer string. Uses
the native PHP function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_integer( $value, $name, array $parameters ) {
- return ctype_digit( (string)$value );
- }
-
- /**
- * Returns whether the length of the value is within a certain range.
Upper bound included.
- *
- * @param string $value
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function has_length( $value, $name, array $parameters,
$lower = false, $upper = false ) {
- return self::in_range( strlen( $value ), $lower, $upper );
- }
-
- /**
- * Returns whether the amount of items in the list is within a certain
range. Upper bound included.
- *
- * @param array $values
- * @param array $metaData
- * @param mixed $lower
- * @param mixed $upper
- *
- * @return boolean
- */
- public static function has_item_count( array $values, $name, array
$parameters, $lower = false, $upper = false ) {
- return self::in_range( count( $values ), $lower, $upper );
- }
-
- /**
- * Returns whether the list of values does not have any duplicates.
- *
- * @param array $values
- * @param array $metaData
- *
- * @return boolean
- */
- public static function has_unique_items( array $values, $name, array
$parameters ) {
- return count( $values ) == count( array_unique( $values ) );
- }
-
- /**
- * Returns the result of preg_match.
- *
- * @param string $value
- * @param array $metaData
- * @param string $pattern
- *
- * @return boolean
- */
- public static function regex( $value, $name, array $parameters,
$pattern ) {
- return (bool)preg_match( $pattern, $value );
- }
-
- /**
- * Wrapper for the native is_numeric function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_numeric( $value, $name, array $parameters ) {
- return is_numeric( $value );
- }
-
- /**
- * Returns if the value is a floating point number.
- * Does NOT check the type of the variable like the native is_float
function.
- *
- * @param $value
- * @param array $metaData
- *
- * @return boolean
- */
- public static function is_float( $value, $name, array $parameters ) {
- return preg_match( '/^\d+(\.\d+)?$/', $value );
- }
-}
\ No newline at end of file
Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php 2010-09-05 14:28:42 UTC
(rev 72431)
+++ trunk/extensions/Validator/includes/Validator.php 2010-09-05 14:29:16 UTC
(rev 72432)
@@ -40,28 +40,6 @@
* versus having the whole list be set to it's default.
*/
public static $perItemValidation = true;
-
- /**
- * @var array Holder for the validation functions.
- */
- protected static $mValidationFunctions = array(
- 'in_array' => array( 'ValidationFunctions', 'in_array' ),
- 'in_range' => array( 'ValidationFunctions', 'in_range' ),
- 'is_numeric' => array( 'ValidationFunctions', 'is_numeric' ),
- 'is_float' => array( 'ValidationFunctions', 'is_float' ),
- 'is_integer' => array( 'ValidationFunctions', 'is_integer' ),
- 'not_empty' => array( 'ValidationFunctions', 'not_empty' ),
- 'has_length' => array( 'ValidationFunctions', 'has_length' ),
- 'regex' => array( 'ValidationFunctions', 'regex' ),
- );
-
- /**
- * @var array Holder for the list validation functions.
- */
- protected static $mListValidationFunctions = array(
- 'item_count' => array( 'ValidationFunctions', 'has_item_count'
),
- 'unique_items' => array( 'ValidationFunctions',
'has_unique_items' ),
- );
/**
* @var array Holder for the formatting functions.
@@ -137,30 +115,6 @@
}
/**
- * Adds a new criteria type and the validation function that should
validate values of this type.
- * You can use this function to override existing criteria type
handlers.
- *
- * @param string $criteriaName The name of the cirteria.
- * @param array $functionName The functions location. If it's a global
function, only the name,
- * if it's in a class, first the class name, then the method name.
- */
- public static function addValidationFunction( $criteriaName, array
$functionName ) {
- self::$mValidationFunctions[$criteriaName] = $functionName;
- }
-
- /**
- * Adds a new list criteria type and the validation function that
should validate values of this type.
- * You can use this function to override existing criteria type
handlers.
- *
- * @param string $criteriaName The name of the list cirteria.
- * @param array $functionName The functions location. If it's a global
function, only the name,
- * if it's in a class, first the class name, then the method name.
- */
- public static function addListValidationFunction( $criteriaName, array
$functionName ) {
- self::$mListValidationFunctions[strtolower( $criteriaName )] =
$functionName;
- }
-
- /**
* Adds a new output format and the formatting function that should
validate values of this type.
* You can use this function to override existing criteria type
handlers.
*
@@ -491,6 +445,7 @@
protected function doListValidation( $name ) {
$hasNoErrors = true;
+ /* TODO
foreach ( $this->parameterInfo[$name]->getListCriteria() as
$criteriaName => $criteriaArgs ) {
// Get the validation function. If there is no matching
function, throw an exception.
if ( array_key_exists( $criteriaName,
self::$mListValidationFunctions ) ) {
@@ -522,6 +477,7 @@
throw new Exception( 'There is no validation
function for list criteria type ' . $criteriaName );
}
}
+ */
return $hasNoErrors;
}
@@ -538,6 +494,7 @@
$value = &$this->mParameters[$name]['value'];
+ /* TODO
// Go through all item criteria.
foreach ( $this->parameterInfo[$name]->getCriteria() as
$criteriaName => $criteriaArgs ) {
// Get the validation function. If there is no matching
function, throw an exception.
@@ -618,6 +575,7 @@
throw new Exception( 'There is no validation
function for criteria type ' . $criteriaName );
}
}
+ */
return $hasNoErrors;
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs