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

Revision: 72428
Author:   jeroendedauw
Date:     2010-09-05 14:06:19 +0000 (Sun, 05 Sep 2010)

Log Message:
-----------
Changes for 0.4 - copied over list criteria validation functions

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

Added Paths:
-----------
    trunk/extensions/Validator/includes/ListParameterCriterion.php
    trunk/extensions/Validator/includes/criteria/CriterionItemCount.php
    trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php

Modified: trunk/extensions/Validator/Validator.php
===================================================================
--- trunk/extensions/Validator/Validator.php    2010-09-05 13:58:09 UTC (rev 
72427)
+++ trunk/extensions/Validator/Validator.php    2010-09-05 14:06:19 UTC (rev 
72428)
@@ -24,7 +24,7 @@
        die( 'Not an entry point.' );
 }
 
-define( 'Validator_VERSION', '0.4 alpha-3' );
+define( 'Validator_VERSION', '0.4 alpha-4' );
 
 // Constants indicating the strictness of the parameter validation.
 define( 'Validator_ERRORS_NONE', 0 );
@@ -54,7 +54,9 @@
 // Autoload the classes.
 $incDir = dirname( __FILE__ ) . '/includes/';
 $wgAutoloadClasses['ListParameter']            = $incDir . 'ListParameter.php';
+$wgAutoloadClasses['ListParameterCriterion']= $incDir . 
'ListParameterCriterion.php';
 $wgAutoloadClasses['Parameter']                        = $incDir . 
'Parameter.php';
+$wgAutoloadClasses['ParameterCriterion']       = $incDir . 
'ParameterCriterion.php';
 $wgAutoloadClasses['ParserHook']                       = $incDir . 
'ParserHook.php';
 $wgAutoloadClasses['Validator']                        = $incDir . 
'Validator.php';
 $wgAutoloadClasses['TopologicalSort']          = $incDir . 
'TopologicalSort.php';
@@ -70,8 +72,10 @@
 $wgAutoloadClasses['CriterionIsFloat']         = $incDir . 
'criteria/CriterionIsFloat.php';
 $wgAutoloadClasses['CriterionIsInteger']       = $incDir . 
'criteria/CriterionIsInteger.php';
 $wgAutoloadClasses['CriterionIsNumeric']       = $incDir . 
'criteria/CriterionIsNumeric.php';
+$wgAutoloadClasses['CriterionItemCount']       = $incDir . 
'criteria/CriterionItemCount.php';
 $wgAutoloadClasses['CriterionMatchesRegex']    = $incDir . 
'criteria/CriterionMatchesRegex.php';
 $wgAutoloadClasses['CriterionNotEmpty']                = $incDir . 
'criteria/CriterionNotEmpty.php';
+$wgAutoloadClasses['CriterionUniqueItems']     = $incDir . 
'criteria/CriterionUniqueItems.php';
 
 $wgAutoloadClasses['ValidatorListErrors']      = $incDir . 
'parserHooks/Validator_ListErrors.php';
 unset( $incDir );

Added: trunk/extensions/Validator/includes/ListParameterCriterion.php
===================================================================
--- trunk/extensions/Validator/includes/ListParameterCriterion.php              
                (rev 0)
+++ trunk/extensions/Validator/includes/ListParameterCriterion.php      
2010-09-05 14:06:19 UTC (rev 72428)
@@ -0,0 +1,26 @@
+<?php
+
+/**
+ * List parameter criterion definition class. This is for criteria
+ * that apply to list parameters as a whole instead of to their
+ * individual items.
+ * 
+ * @since 0.4
+ * 
+ * @file ListParameterCriterion.php
+ * @ingroup Validator
+ * @ingroup Criteria
+ * 
+ * @author Jeroen De Dauw
+ */
+abstract class ListParameterCriterion extends ParameterCriterion {
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               parent::__construct();
+       }
+}
\ No newline at end of file


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

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-09-05 13:58:09 UTC 
(rev 72427)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-05 14:06:19 UTC 
(rev 72428)
@@ -273,7 +273,11 @@
        }
        
        protected function validateCriteria( $value ) {
-               
+               foreach ( $this->getCriteria() as $criterion ) {
+                       if ( !$criterion->validate( $value ) ) {
+                               
+                       }
+               }
        }
        
        /**
@@ -310,22 +314,6 @@
        }
        
        /**
-        * Returns the list delimeter.
-        * 
-        * @since 0.4
-        * 
-        * @return string
-        */
-       public function getListDelimeter() {
-               if ( $this->isList() ) {
-                       return count( $this->type ) > 1 ? $this->type[1] : 
self::$defaultListDelimeter;
-               }
-               else {
-                       return false;
-               }
-       }
-       
-       /**
         * Returns the parameter criteria.
         * 
         * @since 0.4

Modified: trunk/extensions/Validator/includes/ParameterCriterion.php
===================================================================
--- trunk/extensions/Validator/includes/ParameterCriterion.php  2010-09-05 
13:58:09 UTC (rev 72427)
+++ trunk/extensions/Validator/includes/ParameterCriterion.php  2010-09-05 
14:06:19 UTC (rev 72428)
@@ -7,6 +7,7 @@
  * 
  * @file ParameterCriterion.php
  * @ingroup Validator
+ * @ingroup Criteria
  * 
  * @author Jeroen De Dauw
  */
@@ -17,7 +18,7 @@
        /**
         * Validate a value against the criterion.
         * 
-        * @param string $value
+        * @param mixed $value
         * 
         * @since 0.4
         * 

Added: trunk/extensions/Validator/includes/criteria/CriterionItemCount.php
===================================================================
--- trunk/extensions/Validator/includes/criteria/CriterionItemCount.php         
                (rev 0)
+++ trunk/extensions/Validator/includes/criteria/CriterionItemCount.php 
2010-09-05 14:06:19 UTC (rev 72428)
@@ -0,0 +1,42 @@
+<?php
+
+/**
+ * Parameter criterion stating that the value must have a certain length.
+ * 
+ * @since 0.4
+ * 
+ * @file CriterionHasLength.php
+ * @ingroup Validator
+ * @ingroup Criteria
+ * 
+ * @author Jeroen De Dauw
+ */
+class CriterionHasLength extends ListParameterCriterion {
+       
+       protected $lowerBound;
+       protected $upperBound;
+       
+       /**
+        * Constructor.
+        * 
+        * @param integer $lowerBound
+        * @param mixed $upperBound
+        * 
+        * @since 0.4
+        */
+       public function __construct( $lowerBound, $upperBound = false ) {
+               parent::__construct();
+               
+               $this->lowerBound = $lowerBound;
+               $this->upperBound = $upperBound === false ? $lowerBound : 
$upperBound;
+       }
+       
+       /**
+        * @see ParameterCriterion::validate
+        */     
+       public function validate( array $values ) {
+               $count = count( $values );
+               return $count <= $this->upperBound && $count >= 
$this->lowerBound;
+       }
+       
+}
\ No newline at end of file


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

Added: trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php
===================================================================
--- trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php       
                        (rev 0)
+++ trunk/extensions/Validator/includes/criteria/CriterionUniqueItems.php       
2010-09-05 14:06:19 UTC (rev 72428)
@@ -0,0 +1,32 @@
+<?php
+
+/**
+ * Parameter criterion stating that the value must have a certain length.
+ * 
+ * @since 0.4
+ * 
+ * @file CriterionHasLength.php
+ * @ingroup Validator
+ * @ingroup Criteria
+ * 
+ * @author Jeroen De Dauw
+ */
+class CriterionHasLength extends ListParameterCriterion {
+       
+       /**
+        * Constructor.
+        * 
+        * @since 0.4
+        */
+       public function __construct() {
+               parent::__construct();
+       }
+       
+       /**
+        * @see ParameterCriterion::validate
+        */     
+       public function validate( array $values ) {
+               return count( $values ) == count( array_unique( $values ) ); 
+       }
+       
+}
\ No newline at end of file


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



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

Reply via email to