http://www.mediawiki.org/wiki/Special:Code/MediaWiki/73697

Revision: 73697
Author:   jeroendedauw
Date:     2010-09-25 01:12:49 +0000 (Sat, 25 Sep 2010)

Log Message:
-----------
Changes for 0.4 - fixed a few typo's and removed old way of handling parameter 
formats

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/includes/Parameter.php
    trunk/extensions/Validator/includes/ParserHook.php
    trunk/extensions/Validator/includes/Validator.php

Added Paths:
-----------
    trunk/extensions/Validator/includes/ItemParameterManipulation.php
    
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php
    
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php

Removed Paths:
-------------
    trunk/extensions/Validator/includes/ItemParameterManipualtion.php
    trunk/extensions/Validator/includes/ValidationFormats.php
    
trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-25 00:04:11 UTC (rev 
73696)
+++ trunk/extensions/Validator/Validator.php    2010-09-25 01:12:49 UTC (rev 
73697)
@@ -65,7 +65,6 @@
 $wgAutoloadClasses['ParserHook']                               = $incDir . 
'ParserHook.php';
 $wgAutoloadClasses['Validator']                                = $incDir . 
'Validator.php';
 $wgAutoloadClasses['TopologicalSort']                  = $incDir . 
'TopologicalSort.php';
-$wgAutoloadClasses['ValidationFormats']                = $incDir . 
'ValidationFormats.php'; // TODO: remove
 $wgAutoloadClasses['ValidationError']                  = $incDir . 
'ValidationError.php';
 $wgAutoloadClasses['ValidationErrorHandler']   = $incDir . 
'ValidationErrorHandler.php';
 
@@ -81,6 +80,9 @@
 $wgAutoloadClasses['CriterionTrue']                            = $incDir . 
'criteria/CriterionTrue.php';
 $wgAutoloadClasses['CriterionUniqueItems']             = $incDir . 
'criteria/CriterionUniqueItems.php';
 
+$wgAutoloadClasses['ParamManipulationBoolean'] = $incDir . 
'manipulations/ParamManipulationBoolean.php';
+$wgAutoloadClasses['ParamManipulationBoolstr'] = $incDir . 
'manipulations/ParamManipulationBoolstr.php';
+
 $wgAutoloadClasses['ValidatorListErrors']              = $incDir . 
'parserHooks/Validator_ListErrors.php';
 unset( $incDir );
 

Deleted: trunk/extensions/Validator/includes/ItemParameterManipualtion.php
===================================================================
--- trunk/extensions/Validator/includes/ItemParameterManipualtion.php   
2010-09-25 00:04:11 UTC (rev 73696)
+++ trunk/extensions/Validator/includes/ItemParameterManipualtion.php   
2010-09-25 01:12:49 UTC (rev 73697)
@@ -1,64 +0,0 @@
-<?php
-
-/**
- * Item parameter manipulation base class. This is for manipulations
- * that apply to individial values, which can either be the whole value
- * of a non-list parameter, or a single item of a list parameter.
- * 
- * @since 0.4
- * 
- * @file ItemParameterManipulation.php
- * @ingroup Validator
- * @ingroup ParameterManipulations
- * 
- * @author Jeroen De Dauw
- */
-abstract class ItemParameterManipulation extends ParameterManipulation {
-       
-       /**
-        * Manipulate an actual value.
-        * 
-        * @param string $value
-        * @param array $parameters
-        * 
-        * @since 0.4
-        * 
-        * @return mixed
-        */     
-       protected abstract function doManipulation( &$value, array &$parameters 
);      
-       
-       /**
-        * Constructor.
-        * 
-        * @since 0.4
-        */
-       public function __construct() {
-               parent::__construct();
-       }
-       
-       /**
-        * @see ParameterManipulation::isForLists
-        */
-       public function isForLists() {
-               return false;
-       }
-       /**
-        * Validate a parameter against the criterion.
-        * 
-        * @param Parameter $parameter
-        * @param array $parameters
-        * 
-        * @since 0.4
-        */     
-       public abstract function manipulate( Parameter &$parameter, array 
&$parameters ) {
-               if ( is_array( $parameter->value ) ) {
-                       foreach ( $parameter->value as &$item ) {
-                               $this->doManipulation( $item, $parameters );
-                       }
-               }
-               else {
-                       $this->doManipulation( $parameter->value, $parameters );
-               }       
-       }       
-       
-}
\ No newline at end of file

Copied: trunk/extensions/Validator/includes/ItemParameterManipulation.php (from 
rev 73696, trunk/extensions/Validator/includes/ItemParameterManipualtion.php)
===================================================================
--- trunk/extensions/Validator/includes/ItemParameterManipulation.php           
                (rev 0)
+++ trunk/extensions/Validator/includes/ItemParameterManipulation.php   
2010-09-25 01:12:49 UTC (rev 73697)
@@ -0,0 +1,64 @@
+<?php
+
+/**
+ * Item parameter manipulation base class. This is for manipulations
+ * that apply to individial values, which can either be the whole value
+ * of a non-list parameter, or a single item of a list parameter.
+ * 
+ * @since 0.4
+ * 
+ * @file ItemParameterManipulation.php
+ * @ingroup Validator
+ * @ingroup ParameterManipulations
+ * 
+ * @author Jeroen De Dauw
+ */
+abstract class ItemParameterManipulation extends ParameterManipulation {
+       
+       /**
+        * Manipulate an actual value.
+        * 
+        * @param string $value
+        * @param array $parameters
+        * 
+        * @since 0.4
+        * 
+        * @return mixed
+        */     
+       protected abstract function doManipulation( &$value, array &$parameters 
);      
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               parent::__construct();
+       }
+       
+       /**
+        * @see ParameterManipulation::isForLists
+        */
+       public function isForLists() {
+               return false;
+       }
+       /**
+        * Validate a parameter against the criterion.
+        * 
+        * @param Parameter $parameter
+        * @param array $parameters
+        * 
+        * @since 0.4
+        */     
+       public function manipulate( Parameter &$parameter, array &$parameters ) 
{
+               if ( is_array( $parameter->value ) ) {
+                       foreach ( $parameter->value as &$item ) {
+                               $this->doManipulation( $item, $parameters );
+                       }
+               }
+               else {
+                       $this->doManipulation( $parameter->value, $parameters );
+               }       
+       }       
+       
+}
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-09-25 00:04:11 UTC 
(rev 73696)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-25 01:12:49 UTC 
(rev 73697)
@@ -67,15 +67,6 @@
        public $default;        
        
        /**
-        * List of formatting functions to shape the final form of the 
parameter value. 
-        * 
-        * @since 0.4
-        * 
-        * @var array
-        */                     
-       public $outputTypes = array();  
-       
-       /**
         * The main name of the parameter.
         * 
         * @since 0.4
@@ -109,9 +100,18 @@
         * 
         * @var array of ParameterCriterion
         */             
-       protected $criteria;
+       protected $criteria = array();
        
        /**
+        * List of manipulations the parameter value needs to undergo.
+        * 
+        * @since 0.4
+        * 
+        * @var array of ParameterManipulation
+        */             
+       protected $manipulations = array();     
+       
+       /**
         * The original parameter name as provided by the user. This can be the
         * main name or an alias.
         * 
@@ -297,10 +297,32 @@
        }
        
        /**
+        * Adds one or more ParameterCriterion.
         * 
+        * @since 0.4
         * 
+        * @param mixed $criteria ParameterCriterion or array of 
ParameterCriterion
+        */
+       public function addCriteria( $criteria ) {
+               $this->criteria = array_merge( $this->criteria, 
(array)$criteria );
+       }
+       
+       /**
+        * Adds one or more ParameterManipulation.
+        * 
         * @since 0.4
         * 
+        * @param mixed $criteria ParameterManipulation or array of 
ParameterManipulation
+        */
+       public function addManipulations( $manipulations ) {
+               $this->manipulations = array_merge( $this->manipulations, 
(array)$manipulations );
+       }       
+       
+       /**
+        * 
+        * 
+        * @since 0.4
+        * 
         * @param string $paramName
         * @param string $paramValue
         * 
@@ -367,6 +389,19 @@
        }
        
        /**
+        * Applies the parameter manipulations.
+        * 
+        * @since 0.4
+        * 
+        * @param array $parameters
+        */
+       public function format( array &$parameters ) {
+               foreach ( $this->getManipulations() as $manipulation ) {
+                       $manipulation->manipulate( $this, $parameters );
+               }
+       }
+       
+       /**
         * Validates the parameter value.
         * 
         * @since 0.4
@@ -526,6 +561,17 @@
        }
        
        /**
+        * Returns the parameter manipulations.
+        * 
+        * @since 0.4
+        * 
+        * @return array of ParameterManipulation
+        */     
+       public function getManipulations() {
+               return array_merge( $this->getManipulationsForType(), 
$this->manipulations ); 
+       }       
+       
+       /**
         * Gets the criteria for the type of the parameter.
         * 
         * @since 0.4
@@ -535,8 +581,6 @@
        protected function getCriteriaForType() {
                $criteria = array();
 
-               // TODO: also have similar auto-adding of manipulations
-               
                switch( $this->type ) {
                        case self::TYPE_INTEGER:
                                $criteria[] = new CriterionIsInteger();
@@ -544,7 +588,7 @@
                        case self::TYPE_FLOAT:
                                $criteria[] = new CriterionIsFloat();
                                break;
-                       case self::TYPE_FLOAT: // Note: This accepts 
non-decimal notations! 
+                       case self::TYPE_NUMBER: // Note: This accepts 
non-decimal notations! 
                                $criteria[] = new CriterionIsNumeric();
                                break;
                        case self::TYPE_BOOLEAN:
@@ -563,6 +607,40 @@
        }
        
        /**
+        * Gets the manipulation for the type of the parameter.
+        * 
+        * @since 0.4
+        * 
+        * @return array
+        */
+       protected function getManipulationsForType() {
+               $manipulations = array();
+               
+               switch( $this->type ) {
+                       case self::TYPE_INTEGER:
+                               //$manipulations[] = new ();
+                               break;
+                       case self::TYPE_FLOAT:
+                               //$manipulations[] = new ();
+                               break;
+                       case self::TYPE_NUMBER: 
+                               //$manipulations[] = new ();
+                               break;
+                       case self::TYPE_BOOLEAN:
+                               $manipulations[] = new 
ParamManipulationBoolean();
+                               break;
+                       case self::TYPE_CHAR:
+                               //$manipulations[] = new ();
+                               break;
+                       case self::TYPE_STRING: default:
+                               // No extra criteria for strings.
+                               break;
+               }               
+               
+               return $manipulations;
+       }       
+       
+       /**
         * Returns the criteria that apply to the list as a whole.
         * 
         * @since 0.4

Modified: trunk/extensions/Validator/includes/ParserHook.php
===================================================================
--- trunk/extensions/Validator/includes/ParserHook.php  2010-09-25 00:04:11 UTC 
(rev 73696)
+++ trunk/extensions/Validator/includes/ParserHook.php  2010-09-25 01:12:49 UTC 
(rev 73697)
@@ -161,8 +161,6 @@
                        $output = 'Demo: fatal error';
                }
                else {
-                       $this->validator->formatParameters();
-                       
                        $output = $this->render( 
$this->validator->getParameterValues() );                      
                }
                

Deleted: trunk/extensions/Validator/includes/ValidationFormats.php
===================================================================
--- trunk/extensions/Validator/includes/ValidationFormats.php   2010-09-25 
00:04:11 UTC (rev 73696)
+++ trunk/extensions/Validator/includes/ValidationFormats.php   2010-09-25 
01:12:49 UTC (rev 73697)
@@ -1,123 +0,0 @@
-<?php
-
-/**
- * Class holding variouse static methods for the appliance of output formats.
- *
- * @file ValidationFormats.php
- * @ingroup Validator
- *
- * @author Jeroen De Dauw
- */
-final class ValidationFormats {
-       
-       /**
-        * Ensures the value is an array.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_array( &$value, $name, array $parameters 
) {
-               if ( ! is_array( $value ) ) $value = array( $value );
-       }
-       
-       /**
-        * Returns an array containing only the specified values.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_filtered_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
-
-               self::format_array( $value, $name, $parameters );
-               $filtered = array();
-               foreach ( $value as $item ) if ( in_array( $item, $params ) ) 
$filtered[] = $item;
-               
-               return $filtered;
-       }
-       
-       /**
-        * Changes the value to list notation, by separating items with a 
delimiter, 
-        * and/or adding wrappers before and after the items. Intended for 
lists, but
-        * will also work for single values.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        * @param $delimiter
-        * @param $wrapper
-        */
-       public static function format_list( &$value, $name, array $parameters, 
$delimiter = ',', $wrapper = '' ) {
-               self::format_array( $value, $name, $parameters );
-               $value =  $wrapper . implode( $wrapper . $delimiter . $wrapper, 
$value ) . $wrapper;
-       }
-
-       /**
-        * Changes every value into a boolean.
-        * 
-        * TODO: work with a list of true-values.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_boolean( &$value, $name, array 
$parameters ) {
-               if ( is_array( $value ) ) {
-                       $boolArray = array();
-                       foreach ( $value as $item ) $boolArray[] = in_array( 
$item, array( 'yes', 'on' ) );
-                       $value = $boolArray;
-               }
-               else {
-                       $value = in_array( $value, array( 'yes', 'on' ) );
-               }
-       }
-       
-       /**
-        * Changes every value into a boolean, represented by a 'false' or 
'true' string.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_boolean_string( &$value, $name, array 
$parameters ) {
-               self::format_boolean( $value, $name, $parameters );
-               if ( is_array( $value ) ) {
-                       $boolArray = array();
-                       foreach ( $value as $item ) $boolArray[] = $item ? 
'true' : 'false';
-                       $value = $boolArray;
-               }
-               else {
-                       $value = $value ? 'true' : 'false';
-               }
-       }
-
-       /**
-        * Changes lists into strings, by enumerating the items using 
$wgLang->listToText.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_string( &$value, $name, array $parameters 
) {
-               if ( is_array( $value ) ) {
-                       global $wgLang;
-                       $value = $wgLang->listToText( $value );
-               }
-       }
-       
-       /**
-        * Removes duplicate items from lists.
-        * 
-        * @param $value
-        * @param string name The name of the parameter.
-        * @param array $parameters Array containing data about the so far 
handled parameters.
-        */
-       public static function format_unique_items( &$value, $name, array 
$parameters ) {
-               if ( is_array( $value ) ) $value = array_unique( $value );
-       }
-       
-}
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Validator.php
===================================================================
--- trunk/extensions/Validator/includes/Validator.php   2010-09-25 00:04:11 UTC 
(rev 73696)
+++ trunk/extensions/Validator/includes/Validator.php   2010-09-25 01:12:49 UTC 
(rev 73697)
@@ -16,21 +16,6 @@
  *              as it is not wished for all output formats in every case, and 
now a hacky approach is required there.
  */
 class Validator {
-
-       /**
-        * @deprecated TODO: remove
-        * 
-        * @var array Holder for the formatting functions.
-        */
-       protected static $mOutputFormats = array(
-               'array' => array( 'ValidationFormats', 'format_array' ),
-               'list' => array( 'ValidationFormats', 'format_list' ),
-               'boolean' => array( 'ValidationFormats', 'format_boolean' ),
-               'boolstr' => array( 'ValidationFormats', 
'format_boolean_string' ),
-               'string' => array( 'ValidationFormats', 'format_string' ),
-               'unique_items' => array( 'ValidationFormats', 
'format_unique_items' ),
-               'filtered_array' => array( 'ValidationFormats', 
'format_filtered_array' ),
-       );
        
        /**
         * Array containing the parameters.
@@ -71,20 +56,6 @@
        }
        
        /**
-        * @deprecated TODO: remove
-        * 
-        * 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.
-        *
-        * @param string $formatName The name of the format.
-        * @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 addOutputFormat( $formatName, array 
$functionName ) {
-               self::$mOutputFormats[strtolower( $formatName )] = 
$functionName;
-       }       
-       
-       /**
         * Determines the names and values of all parameters. Also takes care 
of default parameters. 
         * After that the resulting parameter list is passed to 
Validator::setParameters
         * 
@@ -145,6 +116,7 @@
                                        $defaultParams = $newDefaults;
                                }
                        }
+                       
                        $nr++;
                }       
 
@@ -272,7 +244,7 @@
        }
        
        /**
-        * Validates all the parameters (but aborts when a fatal error occurs).
+        * Validates and formats all the parameters (but aborts when a fatal 
error occurs).
         * 
         * @since 0.4
         */
@@ -289,37 +261,19 @@
                foreach ( $orderedParameters as $paramName ) {
                        $parameter = $this->parameters[$paramName];
                        
-                       if ( !$parameter->validate() ) {
+                       $validationSucceeded = $parameter->validate();
+                       
+                       if ( !$validationSucceeded ) {
                                foreach ( $parameter->getErrors() as $error ) {
                                        $this->registerError( $error );
                                }
                        }
+                       
+                       $parameter->format( $this->parameters );
                }
        }
        
        /**
-        * Applies the output formats to all parameters.
-        * 
-        * @param string $name
-        */
-       public function formatParameters() {
-               foreach ( $this->parameters as $parameter ) {
-                       foreach ( $parameter->outputTypes as $outputType ) {
-                               $outputType[0] = strtolower( $outputType[0] );
-                               if ( array_key_exists( $outputType[0], 
self::$mOutputFormats ) ) {
-                                       $parameters = array( 
&$parameter->value, $parameter->getName(), $this->parameters );
-                                       $name = array_shift( $outputType );
-                                       $parameters = array_merge( $parameters, 
$outputType );
-                                       call_user_func_array( 
self::$mOutputFormats[$name], $parameters );
-                               }
-                               else {
-                                       throw new Exception( 'There is no 
formatting function for output format ' . $outputType[0] );
-                               }                               
-                       }                       
-               }
-       }
-
-       /**
         * Returns the parameters.
         * 
         * @since 0.4

Deleted: 
trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php
===================================================================
--- 
trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php  
    2010-09-25 00:04:11 UTC (rev 73696)
+++ 
trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php  
    2010-09-25 01:12:49 UTC (rev 73697)
@@ -1,34 +0,0 @@
-<?php
-
-/**
- * Parameter manipulation converting the value to a boolean.
- * 
- * @since 0.4
- * 
- * @file ParamManipulationBoolean.php
- * @ingroup Validator
- * @ingroup ParameterManipulations
- * 
- * @author Jeroen De Dauw
- */
-class ParamManipulationBoolean extends ItemParameterManipulation {
-       
-       /**
-        * Constructor.
-        * 
-        * @since 0.4
-        */
-       public function __construct() {
-               parent::__construct();
-       }       
-       
-       /**
-        * @see ItemParameterManipulation::doManipulation
-        * 
-        * @since 0.4
-        */     
-       public function doManipulation( &$value, array &$parameters ) {
-               $value = in_array( $value, array( 'yes', 'on' ) );
-       }
-       
-}
\ No newline at end of file

Copied: 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php 
(from rev 73696, 
trunk/extensions/Validator/includes/manipulations/ParamManipualtionBoolean.php)
===================================================================
--- 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php  
                            (rev 0)
+++ 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolean.php  
    2010-09-25 01:12:49 UTC (rev 73697)
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Parameter manipulation converting the value to a boolean.
+ * 
+ * @since 0.4
+ * 
+ * @file ParamManipulationBoolean.php
+ * @ingroup Validator
+ * @ingroup ParameterManipulations
+ * 
+ * @author Jeroen De Dauw
+ */
+class ParamManipulationBoolean extends ItemParameterManipulation {
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               parent::__construct();
+       }       
+       
+       /**
+        * @see ItemParameterManipulation::doManipulation
+        * 
+        * @since 0.4
+        */     
+       public function doManipulation( &$value, array &$parameters ) {
+               $value = in_array( $value, array( 'yes', 'on' ) );
+       }
+       
+}
\ No newline at end of file

Added: 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php
===================================================================
--- 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php  
                            (rev 0)
+++ 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php  
    2010-09-25 01:12:49 UTC (rev 73697)
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Parameter manipulation converting the value to a boolean value as string.
+ * 
+ * @since 0.4
+ * 
+ * @file ParamManipulationBoolstr.php
+ * @ingroup Validator
+ * @ingroup ParameterManipulations
+ * 
+ * @author Jeroen De Dauw
+ */
+class ParamManipulationBoolstr extends ItemParameterManipulation {
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               parent::__construct();
+       }       
+       
+       /**
+        * @see ItemParameterManipulation::doManipulation
+        * 
+        * @since 0.4
+        */     
+       public function doManipulation( &$value, array &$parameters ) {
+               $value = $value ? 'true' : 'false';
+       }
+       
+}
\ No newline at end of file


Property changes on: 
trunk/extensions/Validator/includes/manipulations/ParamManipulationBoolstr.php
___________________________________________________________________
Added: svn:eol-style
   + native



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to