Last active
April 30, 2019 21:24
-
-
Save volter9/2d0bf88fceb8bdbfd201 to your computer and use it in GitHub Desktop.
Cool validation module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'validation.php'; | |
/** An example, validate the given data with given rules */ | |
$data = [ | |
'username' => 'voteforpedro', | |
'password' => '123456', | |
'confirm' => '123456', | |
'name' => 'Pedro', | |
'lastname' => 'Dynamite', | |
'status' => 'President', | |
'phone' => '1(800)-800-8000', | |
'mail' => 'voteforpedro@gmail.com', | |
'day' => '2', | |
'month' => '1992', | |
'year' => '3', | |
'education' => 'Something', | |
'experience' => 'Hello', | |
'occupation' => 'Russia', | |
'gender' => '1', | |
'extra' => '' | |
]; | |
$rules = [ | |
'username' => 'required|is_alpha_dash', | |
'password' => 'required|confirm:confirm', | |
'name' => 'required', | |
'lastname' => 'required', | |
'status' => 'required', | |
'phone' => 'required|is_phone_number', | |
'mail' => 'required|is_email', | |
'day' => 'required|is_number', | |
'month' => 'required|is_number', | |
'year' => 'required|is_number', | |
'education' => 'required', | |
'experience' => 'required', | |
'occupation' => 'required', | |
'gender' => 'required|is_boolean', | |
'extra' => '' | |
]; | |
$errors = validate($data, $rules); | |
if (!$errors) { | |
echo 'Valid'; | |
} | |
else { | |
var_dump($errors); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** Validators */ | |
function required ($value) { | |
return !!$value; | |
} | |
function confirm ($value, $array, $field) { | |
return (string)$value === (string)$array[$field]; | |
} | |
function is_phone_number ($value) { | |
return strlen(preg_replace('/[^\d]/', '', $value)) <= 11; | |
} | |
function is_number ($value) { | |
return is_numeric($value); | |
} | |
function is_email ($value) { | |
return strpos($value, '@') !== false; | |
} | |
function is_alpha_dash ($value) { | |
return !!preg_match('/^[\w\d\-\_]+$/', $value); | |
} | |
function is_boolean ($value) { | |
return $value == 1 || $value == 0; | |
} | |
/** | |
* Functions | |
*/ | |
/** | |
* @param array $data | |
* @param array $rules | |
* @param array $validators | |
* @return array | |
*/ | |
function validate (array $data, array $rules) { | |
$errors = []; | |
foreach ($rules as $key => $rule) { | |
$error = validate_field($data[$key], $rule, $data); | |
if ($error) { | |
$errors[$key] = $error; | |
} | |
} | |
return $errors; | |
} | |
/** | |
* @param mixed $value | |
* @param array|string $rule | |
* @param arary $data | |
* @return array | |
*/ | |
function validate_field ($value, $rule, array $data = []) { | |
$rule = parse_rule($rule); | |
$errors = []; | |
foreach ($rule as $validator => $args) { | |
$vargs = array_merge([$value, $data], $args); | |
if (!call_user_func_array($validator, $vargs)) { | |
$errors[$validator] = $args; | |
} | |
} | |
return $errors; | |
} | |
/** | |
* @param string $rule | |
* @return array | |
*/ | |
function parse_rule ($rule) { | |
$rule = !$rule ? [] : $rule; | |
if (is_array($rule)) { | |
return $rule; | |
} | |
$result = []; | |
$rules = explode('|', $rule); | |
foreach ($rules as $rule) { | |
$frags = explode(':', $rule); | |
$name = $frags[0]; | |
$args = isset($frags[1]) ? explode(',', $frags[1]) : []; | |
$result[$name] = $args; | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome