Hej, chce poddać ocenie swoją klasę walidującą dane. Powiedzcie, co jest dobrze, a co źle. Co do robić, co przerobić. Proszę o opinie

<?php
/**
* Class data validation (can be used to form validation)
*
* Class data validation (can be used to form validation).
* Here is an example usage and available parameters.
* Example of use: $formValidation->validate($pass, "required|matches[$pass_c]|min_length[6]");
* Possible parameters to use:: required, numeric, matches[comparison], min_length[number], max_length[number], exact_length[number], valid_email
*
* @version 1.0
* @author Jakub Kubera <jakubkubera1992@gmail.com>
* @link www.jakubkubera.pl
* @license New BSD License
* @access public
*/
class ValidationData
{
private $name;
private $rules = array();
/**
* Assigning parameters to the class variables and run validation
*
* @param string $name Field name
* @param string $rules Field validation rules
*
* @return bool Returns whether validation was successful
*/
public function validate($name, $rules)
{
$this->name = $name;
$this->explodeRules($rules);
return $this->run();
}
/**
* Returns whether validation was successful
*
* @return bool Returns whether validation was successful
*/
private function run()
{
if ($this->checkRequired() === false)
$errors[] = 'required';
if ($this->checkNumeric() === false)
$errors[] = 'numeric';
if ($key = $this->pregArrayKey('/matches\[(.*)]/'))
{
preg_match('/\[(.*)]/', $this->rules[$key], $confirm); if ($this->checkMatches($confirm[1]) === false)
$errors[] = $this->rules[$key];
}
if ($key = $this->pregArrayKey('/min_length\[(.*)]/'))
{
preg_match('/\[(.*)]/', $this->rules[$key], $value); if ($this->checkMinLength($value[1]) === false)
$errors[] = $this->rules[$key];
}
if ($key = $this->pregArrayKey('/max_length\[(.*)]/'))
{
preg_match('/\[(.*)]/', $this->rules[$key], $value); if ($this->checkMaxLength($value[1]) === false)
$errors[] = $this->rules[$key];
}
if ($key = $this->pregArrayKey('/exact_length\[(.*)]/'))
{
preg_match('/\[(.*)]/', $this->rules[$key], $value); if ($this->checkLength($value[1]) === false)
$errors[] = $this->rules[$key];
}
if (in_array('valid_email', $this->rules)) if ($this->checkEmail() === false)
$errors[] = 'valid_email';
return (empty($errors)) ?
true : $errors; }
/**
* Separation of validation rules
*
* @param $rules Field validation rules
*/
private function explodeRules($rules)
{
$this->rules = explode('|', $rules); }
/**
* Finds that there is a pattern in the rules,
* like in_array with using regular expressions
*
* @param $pattern Search pattern in the table
*/
private function pregArrayKey($pattern)
{
}
/**
* Checks and returns whether the value is empty
*
* @return bool Returns whether the value is empty
*/
private function checkRequired()
{
return (!empty($this->name)) ?
true : false; }
/**
* Checks and returns whether the value is numeric
*
* @return bool Returns whether the value is numeric
*/
private function checkNumeric()
{
}
/**
* Checks and returns whether the specified phrases are the same
*
* @return bool Returns whether the specified phrases are the same
*/
private function checkMatches($confirm)
{
return ($this->name == $confirm) ? true : false;
}
/**
* Checks and returns whether the value of a specified minimum length
*
* @return bool Returns whether the value of a specified minimum length
*/
private function checkMinLength($value)
{
return (strlen($this->name) > $value) ?
true : false; }
/**
* Checks and returns whether the value of a specified maximum length
*
* @return bool Returns whether the value of a specified maximum length
*/
private function checkMaxLength($value)
{
return (strlen($this->name) < $value) ?
true : false; }
/**
* Checks and returns whether the value of a specified length
*
* @return bool Returns whether the value of a specified length
*/
private function checkLength($value)
{
return (strlen($this->name) == $value) ?
true : false; }
/**
* Checks and returns whether the given value is a valid email address
*
* @return bool Returns whether the given value is a valid email address
*/
private function checkEmail()
{
return (preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $this->name)) ?
true : false; }
}
Przykład użycia:
<?php
$formValidation = new ValidationData;
$age = '123';
var_dump($formValidation->validate($age, "required|numeric|max_length[4]"));
$name = 'Kuba';
var_dump($formValidation->validate($name, "required|min_length[5]"));
$pass = 'test';
$pass_c = 'test';
var_dump($formValidation->validate($pass, "required|matches[$pass_c]|exact_length[4]"));
$email = 'test1test.pl';
if ($formValidation->validate($email, "required|valid_email") === true)
echo 'Adres e-mail '.$email.' poprawnie zostal zwalidowany'; else {
echo 'Blad, adres e-mail '.$email.' nie zostal poprawnie zwalidowany, ponizej bledy w tablicy $errors:<br />'; var_dump($formValidation->validate($email, "required|valid_email")); }
Ten post edytował in5ane 29.10.2013, 17:07:57