staar2 Posted May 21, 2009 Share Posted May 21, 2009 As i have been trying to get the OO theory to practice so here is the first result. I am making simple validation class and maybe adding the filtering(trim, striptags, replace) support. So all ideas and critics is welcomed. Validation.php <?php /** * Validating the assoc array * * @package default * @author Risto **/ class Validation { private $data; private $rules = array(); private $errors = array(); private $curField = ''; /** * Accepting the array as raw data only associtive arrays * * @return void * @author Risto **/ public function __construct($data) { if (is_array($data)) { $this->data = $data; } } /** * Sets the pointer which field is currently manipulated * * @return this * @author Risto **/ public function setCurField($name) { $this->curField = $name; return $this; } /** * Main functions which will handle the rule setting and checking * * @return this * @author Risto **/ public function addRule(Rule $rule, Error $error) { $this->rules[] = $rule; if (array_key_exists($this->curField, $this->data)) { if ($rule->validate($this->data[$this->curField]) == false) { $this->errors[] = $error; } } return $this; } /** * Returns the Error objects as an array * * @return Array * @author Risto **/ public function getErrors() { return $this->errors; } /** * Checks if there is any errors * * @return boolean * @author Risto **/ public function isValid() { return is_array($this->errors); } } /** * For holding the error messages, can also add the priority **/ class Error { private $message; function __construct($message) { $this->message = $message; } public function getMessage() { return $this->message; } } interface Rule { function validate($field); } class Length implements Rule { private $req = array(); public function __construct($min, $max) { $this->req['min'] = $min; $this->req['max'] = $max; } function validate($field) { $len = strlen($field); // Pikkus x >= min ja x <= max if ($len >= $this->req['min'] && $len <= $this->req['max']) { return true; } else { return false; } } } class Regex implements Rule { private $req = array(); public function __construct($regex) { $this->req['regex'] = $regex; } function validate($field) { return preg_match($this->req['regex'], $field); } } class Email implements Rule { public function __construct() { } function validate($field) { return preg_match('/^[a-z0-9]((\.|_)?[a-z0-9]+)+@([a-z0-9]+(\.|-)?)+[a-z0-9]\.[a-z]{2,}$/', $field); } } class FieldEmpty implements Rule { public function __construct() { } function validate($field) { $len = strlen($field); return ($len > 0) ? true : false; } } ?> Usage is pretty simple require 'Validation.php'; $v = new Validation($data = array('name' => 'dsiada', 'age' => 'yu44')); $v->setCurField('name'); $v->addRule(new Length(2, 5), new Error('Pikkus pole sobiv')) ->addRule(new FieldEmpty(), new Error('Lahter tühi')); $v->setCurField('age'); $v->addRule(new FieldEmpty(), new Error('Age box is empty')) ->addRule(new Regex('/^[0-9]+/'), new Error('No characters allowed')); var_dump($v->getErrors()); [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/159143-oo-validation-class-design/ Share on other sites More sharing options...
Daniel0 Posted May 21, 2009 Share Posted May 21, 2009 Maybe see this post where I gave an example of a simple filtering class: http://www.phpfreaks.com/forums/index.php/topic,253279.msg1189813.html#msg1189813 And my comments on it in a later post: http://www.phpfreaks.com/forums/index.php/topic,253279.msg1189922.html#msg1189922 You could probably borrow some of the logic in that to make a validation class. Quote Link to comment https://forums.phpfreaks.com/topic/159143-oo-validation-class-design/#findComment-839295 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.