All pastes #827510 Raw Edit

Someone

public text v1 · immutable
#827510 ·published 2007-12-21 20:40 UTC
rendered paste body
<?php

class Validator {

   private $data = array();
   private $errors = array();
   private $validation;
   
   public function __construct($data = '')
   {
      if (isset($data)) {
         $this->data = $data;
      }
   }

   private function setError($error)
   {
      $this->errors[] = $error;
   }
   
   public function getErrors()
   {
      return $this->errors;
   }
   
   public function getUniqueErrors()
   {
      return array_unique($this->errors);
   }
   
   public function isValid()
   {
      return sizeof($this->errors) < 1 ? true : false;
   }
   
   public function addRule($fields, $rule, $message)
   {
      if (is_array($fields)) {
         /*
          * Check array if they both are same
          */
         $array = array();
         
         foreach ($this->data as $key => $value) {
            if (in_array($key, $fields)) {
               $array[$key] = $value;
               //If rules is array then check the other fields also
               if (is_array($rule) && (sizeof($rule) == sizeof($message))) {
                  //$rules size and message must be equal
                  for ($i = 0; $i < sizeof($rule); $i++) {
                     $this->ruleCheck($array[$key], $rule[$i], $message[$i]);
                  }
               } else {
                  //if there is only one rule
                  $this->ruleCheck($array[$key], $rule, $message);
               }
               
            }
         }
      } else {
         /*
          * One element check
          */
         if (array_key_exists($fields, $this->data)) {
            if (is_array($rule) && (sizeof($rule) == sizeof($message))) {
               //$rules size and message must be equal
               for ($i = 0; $i < sizeof($rule); $i++) {
                  $this->ruleCheck($this->data[$fields], $rule[$i], $message[$i]);
               }
            } else {
               //if there is only one rule
               $this->ruleCheck($this->data[$fields], $rule, $message);
            }
         }
      }
   }
   
   private function ruleCheck($field, $rule, $message)
   {

      $this->validation = $rule;
      /*
      * Could add check if it is instance of iRUle
      */
                  
      if (!$this->validation->check($field)) {

         $this->setError($message);
      }
   
   }
}

interface iRule {
   public function check($word);
}

class RuleLength implements iRule {
   
   private $rules = array();
   
   public function __construct($minimum)
   {
      $this->rules['min'] = $minimum;
   }
   
   public function check($word)
   {
      $size = strlen($word);
      return ($size >= $this->rules['min']) ? true : false; 
   }
}

/*
 * Checks if the word is set or not
 */

class RuleRequired implements iRule {
   
   public function __construct() {}
   
   public function check($word)
   {
      $size = strlen($word);
      return ($size > 0) ? true : false; 
   }
}

class RuleRegex implements iRule {
   
   private $rules = array();
   
   public function __construct($pattern)
   {
      $this->rules['pattern'] = $pattern;
   }
   
   public function check($word)
   {
      return (preg_match($this->rules['pattern'], $word) != 0);
   }
} 

if (isset($_POST['send'])) {
   $val = new Validator($_POST);

   $val->addRule(array('username', 'age', 'school'), new RuleRequired(), 'Check some fields!');
   //test with two rules
   $val->addRule(array('username', 'age', 'school'), array(new RuleRequired(), new RuleLength(2)), array('Check the fields!', 'Minimum length is 2 chars!'));
   
   $val->addRule('username', array(new RuleRequired(), new RuleLength(2), new RuleRegex('([A-Z]+)')), array('You havent set the username!!', 'Username is too short !', 'Only caps!'));
   
   if ($val->isValid()) {
      echo 'It is OK';
   } else {  
      foreach ($val->getUniqueErrors() as $value) {
         echo "<b>$value</b><br />";
      }

   }
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
   username :<input type="text" name="username"><br>
   age: <input type="text" name="age"><br>
   school: <input type="text" name="school"><br>
   <input type="submit" name="send" value="Send Data" />
</form>