Hallo!
Ich habe seit längerem das Problem, dass ich des öfteren Daten aus Formularen überprüfen muss. Dies möchte ich aber möglichst ohne tausende if()-Abfragen und mit einer ausreichenden Fehlermeldung machen. Also habe ich mir überlegt eine Klasse zu schreiben, die diese Aufgabe für mich übernimmt. Leider bin ich mit dem Ergebnis nicht wirklich zufrieden, weiß aber leider auch nicht, wie ich es besser machen soll. Deshalb würde ich mich über Anregungen und Tips freuen.
MfG, Andy
Ich habe seit längerem das Problem, dass ich des öfteren Daten aus Formularen überprüfen muss. Dies möchte ich aber möglichst ohne tausende if()-Abfragen und mit einer ausreichenden Fehlermeldung machen. Also habe ich mir überlegt eine Klasse zu schreiben, die diese Aufgabe für mich übernimmt. Leider bin ich mit dem Ergebnis nicht wirklich zufrieden, weiß aber leider auch nicht, wie ich es besser machen soll. Deshalb würde ich mich über Anregungen und Tips freuen.
MfG, Andy
PHP-Code:
<?
error_reporting(E_ALL);
/***
* Class Validator
*
* Validator class allows checking
* variables for the right content rather
* the type. If a given variable is not
* of the right type the class will store
* an error message into the private class
* variable.
*
* @package Validator
* @version 0.1
* @author Andreas Wilhelm <xxxxxxxxxxx@xxxxxx>
* @copyright Andreas Wilhelm
**/
class Validator
{
// private class variables
private $errors = array();
/**
* clear() - Clears the errors array
*
* @access public
* @return NONE
*/
public function clear()
{
$errors = array();
}
/**
* isNum() - Checks if the given parameter is numeric
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isNum($param, $name)
{
if( !is_numeric($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isInt() - Checks if the given parameter is an integer
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isInt($param, $name)
{
if( !is_int($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isFloat() - Checks if the given parameter is a float
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isFloat($param, $name)
{
if( !is_float($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isString() - Checks if the given parameter is a string
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isString($param, $name)
{
if( !is_string($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isBool() - Checks if the given parameter is a boolean
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isBool($param, $name)
{
if( !is_bool($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isArray() - Checks if the given parameter is an array
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isArray($param, $name)
{
if( !is_array($param) )
{
$this->setError("Invaild {$name}.");
}
}
/**
* isObject() - Checks if the given parameter is an object
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isObject($param, $name)
{
if( !is_object($param) )
{
$this->setError("Invaild {$name}.");
}
}
/***
* isMail() - Checks for valid email addresses
*
* @access public
* @param Mix $param
* @return NONE
*/
public function isMail($param)
{
// set pattern for mail address
$pattern = '/^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/';
// check for mail address
if( preg_match($pattern, $param) )
{
$this->setError("Invaild email-address.");
}
}
/***
* isMail() - Checks for valid uri
*
* @access public
* @param Mix $param
* @param Str $name
* @return NONE
*/
public function isUri($param, $name)
{
// set pattern for uri
$pattern = "(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?";
// check for mail address
if( preg_match($pattern, $param) )
{
$this->setError("Invaild {$name}.");
}
}
/***
* isPlz() - Checks for valid postcode
*
* @access public
* @param Mix $param
* @return NONE
*/
public function isPlz($param)
{
// set pattern for uri
$pattern = "^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$";
// check for mail address
if( preg_match($pattern, $param) )
{
$this->setError("Invaild postcode.");
}
}
/**
* equals() - Checks if one entry equals the other
*
* @access public
* @param Mix $param_1
* @param Mix $param_2
* @param Str $name
* @return NONE
*/
public function equals($param_1, $param_2, $name)
{
if( $param_1 !== $param_2 )
{
$this->setError("{$name} must be correct two times.");
}
}
/**
* length() - Checks the length of a parameter
*
* @access public
* @param Mix $param
* @param Str $name
* @param Int $max
* @param Int $min
* @return NONE
*/
public function length($param, $name, $max, $min = 0)
{
if( !(strlen($param) < $min) )
{
$this -> setError("To long {$name}.");
}
if( !(strlen($param) > $max))
{
$this -> setError("To short {$name}.");
}
}
/**
* isValid() - Checks if there are any errors
*
* @access public
* @return Boolean
*/
public function isValid()
{
if( empty($this->errors) )
{
return true;
}
return false;
}
/**
* setError() - Adds an error message to the array
*
* @access private
* @param Str $msg
* @return NONE
*/
private function setError($msg)
{
$this->errors[] = $msg;
}
/**
* __toString() - Returns the errors
*
* @access public
* @return String
*/
public function __toString()
{
// instance variable that holds return
$return = '';
foreach( $this->errors as $error)
{
$return .= $error . "<br />";
}
return $return;
}
}
?>
Kommentar