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

Revision: 72414
Author:   jeroendedauw
Date:     2010-09-05 12:24:04 +0000 (Sun, 05 Sep 2010)

Log Message:
-----------
Changes for 0.4 - added ListParameter class

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

Added Paths:
-----------
    trunk/extensions/Validator/includes/ListParameter.php

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-05 10:38:54 UTC (rev 
72413)
+++ trunk/extensions/Validator/Validator.php    2010-09-05 12:24:04 UTC (rev 
72414)
@@ -53,6 +53,7 @@
 
 // Autoload the general classes.
 $incDir = dirname( __FILE__ ) . '/includes/';
+$wgAutoloadClasses['ListParameter']            = $incDir . 'ListParameter.php';
 $wgAutoloadClasses['Parameter']                        = $incDir . 
'Parameter.php';
 $wgAutoloadClasses['ParserHook']                       = $incDir . 
'ParserHook.php';
 $wgAutoloadClasses['Validator']                        = $incDir . 
'Validator.php';

Added: trunk/extensions/Validator/includes/ListParameter.php
===================================================================
--- trunk/extensions/Validator/includes/ListParameter.php                       
        (rev 0)
+++ trunk/extensions/Validator/includes/ListParameter.php       2010-09-05 
12:24:04 UTC (rev 72414)
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * Class for list parameters.
+ * 
+ * @since 0.4
+ * 
+ * @file ListParameter.php
+ * @ingroup Validator
+ * 
+ * @author Jeroen De Dauw
+ */
+class ListParameter extends Parameter {
+       
+       /**
+        * The default delimiter for lists, used when the parameter definition 
does not specify one.
+        * 
+        * @since 0.4
+        * 
+        * @var string 
+        */
+       const DEFAULT_DELIMITER = ',';          
+       
+       /**
+        * The list delimiter.
+        * 
+        * @since 0.4
+        * 
+        * @var string
+        */
+       protected $delimiter;
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        * 
+        * @param string $name
+        * @param string $delimiter
+        * @param mixed $type
+        * @param mixed $default Use null for no default (which makes the 
parameter required)
+        * @param array $aliases
+        * @param array $criteria
+        */
+       public function __construct( $name, $delimiter = 
ListParameter::DEFAULT_DELIMITER, $type = Parameter::TYPE_STRING,
+                                                                $default = 
null, array $aliases = array(), array $criteria = array() ) {
+               parent::construct( $name, $type, $default, $aliases, $criteria 
);
+               $this->delimiter = $delimiter;
+       }
+       
+       /**
+        * Returns if the parameter is a list or not.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */             
+       public function isList() {
+               return true;
+       }       
+       
+}
\ No newline at end of file


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

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-09-05 10:38:54 UTC 
(rev 72413)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-05 12:24:04 UTC 
(rev 72414)
@@ -3,8 +3,6 @@
 /**
  * Parameter definition class.
  * 
- * TODO: create deriving ListParameter class and split list logic off to it
- * 
  * @since 0.4
  * 
  * @file Parameter.php
@@ -22,15 +20,6 @@
        const TYPE_CHAR = 'char';
        
        /**
-        * The default delimiter for lists, used when the parameter definition 
does not specify one.
-        * 
-        * @since 0.4
-        * 
-        * @var string 
-        */
-       public static $defaultListDelimeter = ',';      
-       
-       /**
         * Indicates if the parameter value should be lowercased.
         * 
         * @since 0.4
@@ -76,13 +65,11 @@
        protected $name;
        
        /**
-        * The type of the parameter. Is either a string of the 
Parameter::TYPE_ enum,
-        * or an array with such a string as first element, and optionaly a 
delimiter
-        * as second element for list types.
+        * The type of the parameter, element of the Parameter::TYPE_ enum.
         * 
         * @since 0.4
         * 
-        * @var mixed
+        * @var string
         */
        protected $type;
        
@@ -100,11 +87,41 @@
         * 
         * @since 0.4
         * 
-        * @var array
+        * @var array of ParameterCriterion
         */             
        protected $criteria;
        
        /**
+        * The original parameter name as provided by the user. This can be the
+        * main name or an alias.
+        * 
+        * @since 0.4 
+        * 
+        * @var string
+        */
+       protected $originalName;
+       
+       /**
+        * The original value as provided by the user. This is mainly retained 
for
+        * usage in error messages when the parameter turns out to be invalid.
+        * 
+        * @since 0.4 
+        * 
+        * @var string
+        */
+       protected $originalValue;
+       
+       /**
+        * Keeps track of how many times the parameter has been set by the user.
+        * This is used to detect overrides and for figuring out a parameter is 
missing. 
+        * 
+        * @since 0.4 
+        * 
+        * @var integer
+        */
+       protected $setCount = 0;
+       
+       /**
         * Returns a new instance of Parameter by converting a Validator 
3.x-style parameter array definition.
         * Note: this method is for backward compatibility and should not be 
used in new code.
         * 
@@ -116,15 +133,17 @@
         * @return Parameter
         */
        public static function newFromArray( $name, array $definition ) {
+               $isList = false;
+               $delimiter = false;
+               
                if ( array_key_exists( 'type', $definition ) ) {
                        if ( is_array( $definition['type'] ) ) {
                                if ( count( $definition['type'] ) > 1 ) {
+                                       $isList = true;
+                                       
                                        if ( count( $definition['type'] ) > 2 ) 
{
-                                               $type = array( 
$definition['type'][0], $definition['type'][2] );
+                                               $delimiter = 
$definition['type'][2];
                                        }
-                                       else {
-                                               $type = array( 
$definition['type'][0] );
-                                       }
                                }
                                else {
                                        $type = $definition['type'][0];
@@ -145,13 +164,25 @@
                        $default = array_key_exists( 'default', $definition ) ? 
$definition['default'] : '';
                }
                
-               $parameter = new Parameter(
-                       $name,
-                       $type,
-                       $default,
-                       array_key_exists( 'aliases', $definition ) ? 
$definition['aliases'] : array(),
-                       array_key_exists( 'criteria', $definition ) ? 
$definition['criteria'] : array()
-               );
+               if ( $isList ) {
+                       $parameter = new ListParameter(
+                               $name,
+                               $delimiter,
+                               $type,
+                               $default,
+                               array_key_exists( 'aliases', $definition ) ? 
$definition['aliases'] : array(),
+                               array_key_exists( 'criteria', $definition ) ? 
$definition['criteria'] : array()                 
+                       );
+               }
+               else {
+                       $parameter = new Parameter(
+                               $name,
+                               $type,
+                               $default,
+                               array_key_exists( 'aliases', $definition ) ? 
$definition['aliases'] : array(),
+                               array_key_exists( 'criteria', $definition ) ? 
$definition['criteria'] : array()
+                       );                      
+               }
                
                if ( array_key_exists( 'output-types', $definition ) ) {
                        $types = array();
@@ -191,7 +222,7 @@
         * @since 0.4
         * 
         * @param string $name
-        * @param mixed $type
+        * @param string $type
         * @param mixed $default Use null for no default (which makes the 
parameter required)
         * @param array $aliases
         * @param array $criteria
@@ -205,6 +236,47 @@
        }
        
        /**
+        * @since 0.4
+        * 
+        * @param string $paramName
+        * @param string $paramValue
+        */
+       public function setUserValue( $paramName, $paramValue ) {
+               if ( $this->setCount > 0 && true /* TODO: accept overridng? */ 
) {
+                       // TODO: fatal error
+               }
+               else {
+                       $this->originalName = $paramName;
+                       $this->paramValue = $paramValue;
+                       
+                       $this->setCount++;                      
+               }
+       }
+       
+       /**
+        * Validates the parameter value against it's criteria.
+        * 
+        * @since 0.4
+        */
+       public function validate() {
+               if ( $this->setCount == 0 ) {
+                       if ( $this->isRequired() ) {
+                               // TODO: fatal error
+                       }
+                       else {
+                               $this->validateCriteria( $this->default );
+                       }
+               }
+               else {
+                       $this->validateCriteria( $this->originalValue );
+               }
+       }
+       
+       protected function validateCriteria( $value ) {
+               
+       }
+       
+       /**
         * Returns the parameters main name.
         * 
         * @since 0.4
@@ -234,7 +306,7 @@
         * @return boolean
         */             
        public function isList() {
-               return is_array( $this->type );
+               return false;
        }
        
        /**
@@ -251,14 +323,14 @@
                else {
                        return false;
                }
-       }               
+       }
        
        /**
         * Returns the parameter criteria.
         * 
         * @since 0.4
         * 
-        * @return array
+        * @return array of ParameterCriterion
         */     
        public function getCriteria() {
                return array_merge( $this->getCriteriaForType(), 
$this->criteria ); 



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

Reply via email to