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

Revision: 72507
Author:   jeroendedauw
Date:     2010-09-06 20:04:50 +0000 (Mon, 06 Sep 2010)

Log Message:
-----------
Work on ListParameter class

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

Modified: trunk/extensions/Validator/includes/ListParameter.php
===================================================================
--- trunk/extensions/Validator/includes/ListParameter.php       2010-09-06 
20:04:27 UTC (rev 72506)
+++ trunk/extensions/Validator/includes/ListParameter.php       2010-09-06 
20:04:50 UTC (rev 72507)
@@ -41,6 +41,15 @@
        protected $delimiter;
        
        /**
+        * List of criteria the parameter value as a whole needs to hold 
against.
+        * 
+        * @since 0.4
+        * 
+        * @var array of ListParameterCriterion
+        */             
+       protected $listCriteria;        
+       
+       /**
         * Constructor.
         * 
         * @since 0.4
@@ -54,8 +63,24 @@
         */
        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 
);
+               $itemCriteria = array();
+               $listCriteria = array();
+                                                                       
+               foreach ( $criteria as $criterion ) {
+                       if ( $criterion instanceof ListParameterCriterion ) {
+                               $listCriteria[] = $criterion;
+                       }
+                       else {
+                               $itemCriteria[] = $criterion;
+                       }               
+               }
+
+               parent::construct( $name, $type, $default, $aliases, 
$itemCriteria );
+               
                $this->delimiter = $delimiter;
+               
+               $this->cleanCriteria( $listCriteria );
+               $this->listCriteria = $listCriteria;
        }
        
        /**
@@ -67,6 +92,73 @@
         */             
        public function isList() {
                return true;
-       }       
+       }
        
+       /**
+        * Validates all items in the list.
+        * 
+        * @since 0.4
+        * 
+        * @return boolean
+        */
+       public function validate() {
+               if ( $this->setCount == 0 ) {
+                       if ( $this->isRequired() ) {
+                               // TODO: fatal error
+                               $success = false;
+                       }
+                       else {
+                               $success = true;
+                               $this->value = $this->default;
+                       }
+               }
+               else {
+                       $this->validateListCriteria( $this->value );
+                       
+                       // TODO
+                       
+                       foreach ( $this->value as $item ) {
+                               list( $itemSuccess, $itemHasError ) = 
$this->validateCriteria( $item );
+                               
+                               // TODO
+                               
+                               $success = $success && $itemSuccess;
+                       }
+               }
+               
+               return $success;
+       }
+       
+       /**
+        * 
+        * 
+        * @since 0.4
+        * 
+        * @param array $values
+        */
+       protected function validateListCriteria( array $values ) {
+               foreach ( $this->getListCriteria() as $listCriterion ) {
+                       if ( !$listCriterion->validate( $value ) ) {
+                               $hasError = true;
+                               
+                               if ( !self::$accumulateParameterErrors ) {
+                                       break;
+                               }
+                       }                       
+               }
+               
+               // TODO
+       }
+       
+       /**
+        * Returns the parameter list criteria.
+        * 
+        * @since 0.4
+        * 
+        * @return array of ListParameterCriterion
+        */     
+       public function getListCriteria() {
+               return $this->listCriteria; 
+       }
+       
 }
\ No newline at end of file

Modified: trunk/extensions/Validator/includes/Parameter.php
===================================================================
--- trunk/extensions/Validator/includes/Parameter.php   2010-09-06 20:04:27 UTC 
(rev 72506)
+++ trunk/extensions/Validator/includes/Parameter.php   2010-09-06 20:04:50 UTC 
(rev 72507)
@@ -357,7 +357,11 @@
                        }
                }
                else {
-                       $success = $this->validateCriteria( 
$this->originalValue );
+                       list( $success, $hasError ) = $this->validateCriteria( 
$this->originalValue );
+                       
+                       if ( $hasError ) {
+                               $this->value = $this->default;
+                       }                       
                }
                
                return $success;
@@ -370,7 +374,7 @@
         * 
         * @param string $value
         * 
-        * @return boolean If there where no fatal errors
+        * @return array Containing a boolean indicating if there where no 
fatal errors and one if there where any errors
         */
        protected function validateCriteria( $value ) {
                $success = true;
@@ -386,12 +390,7 @@
                        }
                }
                
-               // TODO: move this to a nicer place
-               if ( $hasError ) {
-                       $this->value = $this->default;
-               }
-               
-               return $success;
+               return array( $success, $hasError );
        }
        
        /**



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

Reply via email to