[limb-svn] r7084 - in 3.x/trunk/limb/validation: src tests/cases

svn at limb-project.com svn at limb-project.com
Fri Jun 27 11:17:33 MSD 2008


Author: conf
Date: 2008-06-27 11:17:32 +0400 (Fri, 27 Jun 2008)
New Revision: 7084
URL: http://fisheye.limb-project.com/changelog/limb/?cs=7084

Modified:
   3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php
   3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php
Log:
-- an extended format of the rules is added into lmbValidatorBuilder
-- updating and fixing errors in tests

Modified: 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php
===================================================================
--- 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php	2008-06-26 14:33:13 UTC (rev 7083)
+++ 3.x/trunk/limb/validation/src/lmbValidatorBuilder.class.php	2008-06-27 07:17:32 UTC (rev 7084)
@@ -81,15 +81,15 @@
 
       foreach($list as $rule_name => $rule) // by default $rule has simple format
       {
-        $error = '';
+        $args = '';
 
         if(is_string($rule_name)) // extended format
         {
-          $error = $rule;
+          $args = $rule;
           $rule = $rule_name;
         }
 
-        if($object_rule = self :: parseRule($field, $rule, $error))
+        if($object_rule = self :: parseRule($field, $rule, $args))
         {
           $validator->addRule($object_rule);
         }
@@ -112,10 +112,10 @@
    *
    * @param string $field
    * @param string $rule
-   * @param string $error
+   * @param mixed $args
    * @return object 
    */  
-  protected static function parseRule($field, $rule, $error = '') 
+  protected static function parseRule($field, $rule, $args = '') 
   {
 
     $params = array();
@@ -126,18 +126,23 @@
     }
 
     $rule_name = $matches[1];
-    if(isset($matches[3])) 
+    
+    if (is_array($args) && $args) // args in array overlay args in square brackets
     {
+      $params = array_values($args);
+    }
+    elseif(isset($matches[3]))
+    {
       $params = explode(',', $matches[3]);
     }
-
+    
     array_unshift($params, $field); // field must be the first in params
 
-    if(!empty($error)) 
+    if (is_string($args) && $args) // if $args is a string, then it's a custom error message
     {
-      array_push($params, $error); // but error the last
+      array_push($params, $args); // and must be the last in the $params 
     }
-
+    
     $params = self :: trim($params);
 
     $path_to_rule = self :: getPathByRuleName($rule_name);
@@ -157,7 +162,7 @@
     } 
     elseif(strpos($rule_name, 'lmb') === 0) // $rule_name is exactly limb filename
     { 
-      return LIMB_RULES_INCLUDE_PATH . '/' . $rule_name;
+      return LIMB_RULES_INCLUDE_PATH . '/' . $rule_name . '.class.php';
     } 
     else 
     {

Modified: 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php
===================================================================
--- 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php	2008-06-26 14:33:13 UTC (rev 7083)
+++ 3.x/trunk/limb/validation/tests/cases/lmbValidatorBuilderTest.class.php	2008-06-27 07:17:32 UTC (rev 7084)
@@ -23,7 +23,7 @@
   function testAddRulesFromSimpleString()
   {
     $rules = array();
-    $rules['login'] = "required|matches[bbb]|size_range[5, 8]|lmbIdentifierRule.class.php";
+    $rules['login'] = "required|matches[bbb]|size_range[5, 8]|lmbIdentifierRule";
     
     $calls_counter = 0;
     
@@ -74,11 +74,12 @@
     lmbValidatorBuilder :: addRules($rules, $this->validator);
   }
     
-  function testAddRulesFromArrayWithCustomError()
+  function testAddRulesFromArrayWithCustomArguments()
   {
     $errors = array(
       'email' => 'Email error',
-      'pattern' => 'Not a digit'    
+      'pattern' => 'Not a digit',
+      'size_range' => 'Size range error!'  
     );
         
     $rules = array();
@@ -88,6 +89,11 @@
       'size_range[5, 8]',
       'email' => $errors['email'],
       'pattern[/\d+/]' => $errors['pattern'],
+      'lmbSizeRangeRule[5, 8]' => array( // params [5, 8] will be ignored because of args have array type
+        'min' => 10,
+        'max' => 15,
+        'error' => $errors['size_range']  // keys (min, max, error) are ignored, the order of args is still important
+      )
     );
     
     $calls_counter = 0;
@@ -133,6 +139,17 @@
       )
     );
         
+   $this->validator->expectAt(
+      $calls_counter++,
+      "addRule", 
+      array(
+        new EqualExpectation(
+          new lmbHandle('limb/validation/src/rule/lmbSizeRangeRule.class.php', array('login', 10, 15, $errors['size_range']))
+        )
+      )
+    );
+    
+        
     lmbValidatorBuilder :: addRules($rules, $this->validator);
   }
 }



More information about the limb-svn mailing list