Hi.
What do you think about this filter_input handler ?
(credits to Shrike for the validator class)
<?php
class ValidatorException extends Exception {}
class Validator{
private $filters= array();
private $errors= array();
public $values= array();
public function __construct(){}
public function add(AbstractFilter $filter){
$this->filters[]= $filter;
}
public function isValid(){
$isValid= true;
foreach($this->filters as $filter){
if(!$filter->isValid()){
$isValid= false;
$this->errors[] = $filter->getError();
}
$this->values[$filter->name]= $filter->value;
}
return $isValid;
}
public function getErrors(){
return $this->errors;
}
public function getFirstError(){
return $this->errors[0];
}
}
abstract class AbstractFilter{
protected $type= 0;
protected $error= '';
public $name= '';
public $value= false;
public function getError(){
return $this->error;
}
abstract protected function isValid();
abstract protected function setError($msg);
}//
class IsNumeric extends AbstractFilter{
public function __construct($type,$name,$error,$options=null){
if(!is_int($this->type=$type)){
throw new ValidatorException('Type must be an integer');
}
if(!is_string($name) || empty($name)){
throw new ValidatorException('Name must be a not empty string');
}
$this->name= $name;
$this->setError($error);
$this->options= $options;
}
public function isValid(){
$isValid= true;
$options= array('options'=>$this->options);
if(is_array($this->options) && (count($this->options)==2)){
if(!array_key_exists('min_range', $this->options) && !array_key_exists('max_range',$this->options)){
throw new ValidatorException('Invalid options');
}
}
$this->value= filter_input($this->type,$this->name,FILTER_VALIDATE_INT,$options);
if($this->value === false){
$isValid= false;
}
return $isValid;
}
protected function setError($error){
$this->error= $error;
}
}//
class IsValidRegEx extends AbstractFilter{
public function __construct($type,$name,$error,$options){
if(!is_int($this->type=$type)){
throw new ValidatorException('Type must be an integer');
}
if(!is_string($name) || empty($name)){
throw new ValidatorException('Name must be a not empty string');
}
$this->name= $name;
$this->setError($error);
$this->options= (array)$options;
}
public function isValid(){
$isValid= true;
$options= array('options'=>$this->options);
if((count($this->options)!=1) && !array_key_exists('regexp', $this->options)){
throw new ValidatorException('Invalid options');
}
$this->value= filter_input($this->type,$this->name,FILTER_VALIDATE_REGEXP,$options);
if($this->value === false){
$isValid= false;
}
return $isValid;
}
protected function setError($error){
$this->error= $error;
}
}//
if (!filter_has_var(INPUT_POST, 'submit')) {
$xhtml=<<<EOD
<form action="{$_SERVER['PHP_SELF']}" method="post" >
Enter your name: <input name="name" maxlength="15"><br>
Enter your age: <input name="age" maxlength="2">
<input type="submit" name="submit" value="Go">
</form>
EOD;
echo $xhtml;
return;
}
$validator= new Validator();
$name= new IsValidRegEx(INPUT_POST,'name','Invalid name',array('regexp'=>'/^[A-Za-z0-9]{3,15}$/'));
$age= new IsNumeric(INPUT_POST,'age','Age must be an number between 1 and 99',array('min_range'=>1,'max_range'=>99));
$validator->add($name);
$validator->add($age);
if(!$validator->isValid()){
print_r($validator->getErrors());
exit();
}
echo $validator->values['name'];
echo $validator->values['age'];
?>
As usual any comments on how I should be doing this better would be gratefully appreciated 😉
Bye and best wishes for the christmas holidays.