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

Revision: 60380
Author:   jeroendedauw
Date:     2009-12-24 23:30:30 +0000 (Thu, 24 Dec 2009)

Log Message:
-----------
Changes for 0.2.
* Added list validation functions
* Added formatting functions
* Added php docs
* Added Validator_ERRORS_WARN support to ValidationManager

Modified Paths:
--------------
    trunk/extensions/Validator/Validator.class.php
    trunk/extensions/Validator/Validator.i18n.php
    trunk/extensions/Validator/Validator.php
    trunk/extensions/Validator/Validator_Formats.php
    trunk/extensions/Validator/Validator_Functions.php
    trunk/extensions/Validator/Validator_Manager.php

Modified: trunk/extensions/Validator/Validator.class.php
===================================================================
--- trunk/extensions/Validator/Validator.class.php      2009-12-24 23:28:57 UTC 
(rev 60379)
+++ trunk/extensions/Validator/Validator.class.php      2009-12-24 23:30:30 UTC 
(rev 60380)
@@ -53,8 +53,7 @@
                        'in_array' => 'in_array',
                        'in_range' => array( 'ValidatorFunctions', 'in_range' ),
                        'is_numeric' => 'is_numeric',
-                       'is_integer' => array( 'ValidatorFunctions', 
'is_integer' ),
-                       'is_boolean' => array( 'ValidatorFunctions', 
'is_boolean' ),    
+                       'is_integer' => array( 'ValidatorFunctions', 
'is_integer' ),    
                        'not_empty' => array( 'ValidatorFunctions', 'not_empty' 
),
                        'has_length' => array( 'ValidatorFunctions', 
'has_length' ),
                        );
@@ -74,7 +73,9 @@
                        'array' => array( 'ValidatorFormats', 'format_array' ),
                        'list' => array( 'ValidatorFormats', 'format_list' ),
                        'boolean' => array( 'ValidatorFormats', 
'format_boolean' ),
+                       'boolstr' => array( 'ValidatorFormats', 
'format_boolean_string' ),
                        'string' => array( 'ValidatorFormats', 'format_string' 
),
+                       'unique_items' => array( 'ValidatorFormats', 
'format_unique_items' ),
                        );
 
        private $parameterInfo;
@@ -229,11 +230,11 @@
                                        
$this->parameterInfo[$name]['criteria']['is_numeric'] = array();
                                        break;
                                case 'boolean':
-                                       
$this->parameterInfo[$name]['criteria']['is_boolean'] = array();
+                                       
$this->parameterInfo[$name]['criteria']['in_array'] = array('yes', 'no', 'on', 
'off');
                                        break;
                                case 'char':
                                        
$this->parameterInfo[$name]['criteria']['has_length'] = array(1);
-                                       break;                                  
                                        
+                                       break;  
                        }
                }
                

Modified: trunk/extensions/Validator/Validator.i18n.php
===================================================================
--- trunk/extensions/Validator/Validator.i18n.php       2009-12-24 23:28:57 UTC 
(rev 60379)
+++ trunk/extensions/Validator/Validator.i18n.php       2009-12-24 23:30:30 UTC 
(rev 60380)
@@ -19,6 +19,7 @@
        'validator-desc' => 'Validator provides an easy way for other 
extensions to validate parameters of parser functions and tag extensions, set 
default values and generate error messages',
 
        'validator_error_parameters' => 'The following {{PLURAL:$1|error 
has|errors have}} been detected in your syntax',
+       'validator_warning_parameters' => 'There {{PLURAL:$1|is an error|are 
errors}} in your syntax.',
 
        // General errors
        'validator_error_unknown_argument' => '$1 is not a valid parameter.',

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2009-12-24 23:28:57 UTC (rev 
60379)
+++ trunk/extensions/Validator/Validator.php    2009-12-24 23:30:30 UTC (rev 
60380)
@@ -24,7 +24,7 @@
        die( 'Not an entry point.' );
 }
 
-define( 'Validator_VERSION', '0.2 a4' );
+define( 'Validator_VERSION', '0.2 a6' );
 
 // Constants indicating the strictness of the parameter validation.
 define( 'Validator_ERRORS_NONE', 0 );

Modified: trunk/extensions/Validator/Validator_Formats.php
===================================================================
--- trunk/extensions/Validator/Validator_Formats.php    2009-12-24 23:28:57 UTC 
(rev 60379)
+++ trunk/extensions/Validator/Validator_Formats.php    2009-12-24 23:30:30 UTC 
(rev 60380)
@@ -21,31 +21,83 @@
  */
 final class ValidatorFormats {
        
+       /**
+        * Ensures the value is an array.
+        * 
+        * @param $value
+        */
        public static function format_array( &$value ) {
                if (! is_array($value)) $value = array($value); 
        }
        
+       /**
+        * Changes the value to list notation, by seperating 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 $delimiter
+        * @param $wrapper
+        */
        public static function format_list( &$value, $delimiter = ',', $wrapper 
= '' ) {
-               if (! is_array($value)) $value = array($value);
+               self::format_array($value);
                $value =  $wrapper . implode($wrapper . $delimiter . $wrapper, 
$value) . $wrapper;      
        }
 
+       /**
+        * Changes every value into a boolean.
+        * 
+        * TODO: work with a list of true-values.
+        * 
+        * @param $value
+        */
        public static function format_boolean( &$value ) {
                if (is_array($value)) {
                        $boolArray = array();
-                       foreach ($value as $item) $boolArray[] = $value == 
'yes';
+                       foreach ($value as $item) $boolArray[] = 
in_array($item, array('yes', 'on'));
                        $value = $boolArray;
                }
                else {
-                       $value == 'yes';
+                       $value = in_array($value, array('yes', 'on'));
                }
        }
+       
+       /**
+        * Changes every value into a boolean, represented by a 'false' or 
'true' string.
+        * 
+        * @param $value
+        */
+       public static function format_boolean_string( &$value ) {
+               self::format_boolean($value);
+               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
+        */
        public static function format_string( &$value ) {
                if (is_array($value)) {
                        global $wgLang;
                        $value = $wgLang->listToText($value);
                }               
-       }       
+       }
        
+       /**
+        * Removes duplicate items from lists.
+        * 
+        * @param $value
+        */
+       public static function format_unique_items( &$value ) {
+               if (is_array($value)) $value = array_unique($value);
+       }
+       
 }
\ No newline at end of file

Modified: trunk/extensions/Validator/Validator_Functions.php
===================================================================
--- trunk/extensions/Validator/Validator_Functions.php  2009-12-24 23:28:57 UTC 
(rev 60379)
+++ trunk/extensions/Validator/Validator_Functions.php  2009-12-24 23:30:30 UTC 
(rev 60380)
@@ -58,19 +58,38 @@
                return ctype_digit( (string)$value );
        }       
        
-       public static function is_boolean( $value ) {
-               
+       /**
+        * Returns whether the length of the value is within a certain range. 
Upper bound not included.
+        * 
+        * @param string $value
+        * @param array $limits
+        * 
+        * @return boolean
+        */
+       public static function has_length( $value, array $limits ) {
+               return self::in_range(strlen($value), $limits);
        }
        
-       public static function has_length( $value, $limits ) {
-               
+       /**
+        * Returns whether the amount of items in the list is within a certain 
range. Upper bound not included.
+        * 
+        * @param array $values
+        * @param $limits
+        * 
+        * @return boolean
+        */
+       public static function has_item_count( array $values, array $limits ) {
+               return self::in_range(count($value), $limits);
        }
        
-       public static function has_item_count( array $values, $limits ) {
-               
-       }
-       
+       /**
+        * Returns whether the list of values does not have any duplicates.
+        * 
+        * @param array $values
+        * 
+        * @return boolean
+        */
        public static function has_unique_items( array $values ) {
-               
+               return count($values) == count(array_unique($values));
        }       
 }

Modified: trunk/extensions/Validator/Validator_Manager.php
===================================================================
--- trunk/extensions/Validator/Validator_Manager.php    2009-12-24 23:28:57 UTC 
(rev 60379)
+++ trunk/extensions/Validator/Validator_Manager.php    2009-12-24 23:30:30 UTC 
(rev 60380)
@@ -42,7 +42,7 @@
 
                if ( ! $validator->validateParameters() ) {
                        if ( $egValidatorErrorLevel != Validator_ERRORS_STRICT 
) $validator->correctInvalidParams();
-                       if ( $egValidatorErrorLevel >= Validator_ERRORS_SHOW ) 
$this->errors = $validator->getErrors();
+                       if ( $egValidatorErrorLevel >= Validator_ERRORS_WARN ) 
$this->errors = $validator->getErrors();
                }
 
                $showOutput = ! ( $egValidatorErrorLevel == 
Validator_ERRORS_STRICT && count( $this->errors ) > 0 );
@@ -132,6 +132,9 @@
 
                        return $errorList . implode( $errors, '<br />' ) . 
'</i><br />';
                }
+               elseif ( $egValidatorErrorLevel == Validator_ERRORS_WARN && 
$error_count > 0 ) {
+                       return '<b>' . wfMsgExt( 
'validator_warning_parameters', array( 'parsemag' ), $error_count ) . '</b>';
+               }
                else {
                        return '';
                }



_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to