Skip to content

Instantly share code, notes, and snippets.

@omeid
Created March 10, 2012 14:15
Show Gist options
  • Save omeid/2011557 to your computer and use it in GitHub Desktop.
Save omeid/2011557 to your computer and use it in GitHub Desktop.
PHP Validator
<?php
class validiator {
/**
* !! The class name isn't misspled, it's just the package name. :) !!
*
* @var array $filters
* @var array $errorMsgs
*
* @var array $filtered
* @var array $errorFlags
*
*/
protected $filters = array(),
$errorMsgs = array(),
$filtered = array(),
$tainted = array(),
$errorFlags = array();
/**
*
* @param type $form
* @param type $filters
* @param type $errorMsgs
* @return type
*/
public function validate($form, $filters, $errorMsgs) {
$this->filters = $filters;
$this->errorMsgs = $errorMsgs;
foreach($form as $field => $data) {
if(isset($this->filters[$field])) {
//Filed Information
if(is_array($this->filters[$field])) {
$filter = "f_".$this->filters[$field][0];
$optional = (isset($this->filters[$field][1])) ? $this->filters[$field][1] : false;
} else {
$filter = $this->filters[$field];
$optional = false;
}
//End of Field Information
//Validation process
if(method_exists($this, $filter)) {
$filtered = call_user_func_array(array($this, $filter), array($data, $optional));
if($filtered === false) {
$this->tainted[$field] = $data;
$this->errorFlags[$field] = sprintf($this->errorMsgs[$field], (string) $data);
} else {
$this->filtered[$field] = $filtered;
}
}
//End of Validation
}
//returing the results.
return $this->filtered;
}
}
public function getFiltered() {
return $this->filtered;
}
public function getTainted() {
return $this->tainted;
}
public function getErrors() {
return $this->errorFlags;
}
public function raiseErrorFlag($error) {
$this->errorFlags[] = $error;
}
//Validation filters, all filtesr must be prefixed with f_
protected function f_URL($URL, $optional = false) {
if($optional and empty($URL))
return NULL;
else
return filter_var($URL, FILTER_VALIDATE_URL);
}
protected function f_email($email, $optional = false) {
if($optional and empty($email))
return NULL;
else
return filter_var(
filter_var($email, FILTER_SANITIZE_EMAIL),
FILTER_VALIDATE_EMAIL
);
}
protected function f_string($str, $optional = false) {
if($optional and empty($str))
return NULL;
else {
// TODO: string is pretty vague. Drop or well-define.
if($str)
return $str;
else
return false;
}
}
protected function f_alphaNum($str, $optional = false) {
if($optional and empty($str))
return NULL;
else {
$str = trim($str);
if(preg_match("/^[A-Za-z0-9\s]+[^ ]$/", $str))
return trim($str);
else
return false;
}
}
//none will return false on empty values which will cause raise error flag unless $optional is set to true.
protected function f_none($input, $optional = false) {
if($optional and empty($input))
return NULL;
else
return $input;
}
protected function f_text($input, $optional = false) {
if($optional and empty($input))
return NULL;
else
return trim($input);
}
protected function f_date($date, $optional = false) {
if($optional and empty($date))
return NULL;
else {
if(strtotime($is_date))
return strtotime($date);
else
return false;
}
}
protected function f_file($input, $optional = false) {
if($optional and empty($input))
return NULL;
else
return $input;
}
protected function f_boolen($input, $optional = false) {
if($optional and empty($input))
return NULL;
else {
if($input)
return true;
else
false;
}
}
protected function f_int($input, $optional = false) {
if($optional and empty($input))
return NULL;
else {
if(is_int($input))
return $input;
else
false;
}
}
}
?>
<?php
//Example File!!
include('validiator.php');
$filters = array(
// the "test" field type is "text" and is optional
"test" => array("text",true),
// the "test2" field type is "alphaNum" and NOT optional
"test2" => array("alphaNum",false),
// the "no" field type is "int" and NOT optional
"no" => array("int",false),
// the "email" field type is "email" and is optional
"email" => array("email",true)
);
$form = array(
"test" => "something here!",
"text2" => "ASDFA@#$@!!!ADS",
"no" => "34534" ,
"email" => "not@email."
);
$errors = array(
"test" => "%s isn't a valid name.",
"text2" => "%s isn't valid.",
"no" => "%s is not a proper int." ,
"email" => "%s is invalid email."
);
$validiator = new $validiator();
$validiator->validate($data,$filters,$erros);
if(empty($validator->getErrors())) {
echo "All is good";
var_dump($validiator->getFiltered);
}
else
{ echo "<br />Errors:"
var_dump($validiator->getErrors());
echo "<br/> Invalid data:"
var_dump($validiator->getTainted());
echo "<br />Valid data:"
var_dump($validiator->getFiltered);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment