From 8f57077ef6962a57f2382cbb992114eac2ce3010 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sun, 29 Sep 2013 15:51:45 -0400 Subject: [PATCH] Created a validation class to handle validation Instead of making the Module the keeper of validation, moved the actual sanity checks to another class. This makes it stupid easy to use one module's validation (some or all) to validate arbitrary values elsewhere in your system. --- classes/Module.php | 120 +++------------------------------ classes/Validate.php | 157 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 109 deletions(-) create mode 100644 classes/Validate.php diff --git a/classes/Module.php b/classes/Module.php index c661ee7..f0c931b 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -305,6 +305,13 @@ class Module extends Object /** * Validate + * + * Internal validation for data passed to a Module. Grabs the super global + * based on the Module's request method and loops through the data using the + * Module's validation array (if present) sanity checking each variable + * against the rules. + * + * @return mixed boolean false if everything is fine or an array or errors */ public function __validate() { @@ -329,118 +336,13 @@ class Module extends Object if (isset($global[$variable]) && !String::isEmpty($global[$variable])) { - $value = $global[$variable]; - if (is_array($rules)) { - foreach ($rules as $rule => $message) + $rule_errors = Validate::isValid($global[$variable], $rules); + + if (is_array($rule_errors)) { - $rule = explode(':', $rule); - - switch (strtolower($rule[0])) - { - // @todo case 'alpha': - // @todo case 'alphanumeric': - // @todo case 'date': - - // {{{ Checks using filter_var() - - case 'filter': - if (count($rule) < 2) - { - throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|regex|url".'); - } - else - { - switch (strtolower($rule[1])) - { - case 'boolean': - case 'email': - case 'float': - case 'int': - case 'ip': - case 'regex': - case 'url': - $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); - break; - - default: - throw new Exception('Invalid filter, expecting boolean, email, float, int, ip, regex or url.'); - break; - } - - if (!filter_var($value, $filter)) - { - $errors[] = $message; - } - } - - break; - - // }}} - // {{{ Checks using strlen() - - case 'length': - if (count($rule) < 3) - { - throw new Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".'); - } - else - { - if (!filter_var($rule[2], FILTER_VALIDATE_INT)) - { - throw new Exception('Invalid length value, expecting an integer.'); - } - else - { - $length = strlen($value); - - switch ($rule[1]) - { - case '<': $valid = $length < $rule[2]; break; - case '<=': $valid = $length <= $rule[2]; break; - case '==': $valid = $length == $rule[2]; break; - case '!=': $valid = $length != $rule[2]; break; - case '>=': $valid = $length >= $rule[2]; break; - case '>': $valid = $length > $rule[2]; break; - - default: - throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.'); - break; - } - - if ($valid === true) - { - $errors[] = $message; - } - } - } - - break; - - // }}} - - // @todo case 'range': - - // {{{ Checks using preg_match() - - case 'regex': - if (count($rule) < 3) - { - throw new Exception('Invalid validation rule, expected: "regex:is|not:string".'); - } - else - { - if ((strtolower($rule[1]) == 'not' && !preg_match($rule[2], $value)) || preg_match($rule[2], $value)) - { - $errors[] = $message; - } - } - break; - - // }}} - } - + $errors = array_merge($errors, $rule_errors); } } } diff --git a/classes/Validate.php b/classes/Validate.php new file mode 100644 index 0000000..f5f85ea --- /dev/null +++ b/classes/Validate.php @@ -0,0 +1,157 @@ + + * @copyright Copyright 2007-2012, Josh Sherman + * @license http://www.opensource.org/licenses/mit-license.html + * @package PICKLES + * @link https://github.com/joshtronic/pickles + */ + +/** + * Validate Class + * + * Validation layer that's used by the Modules to validate passed data. Handles + * single sanity checks against a variable so the validation itself can be used + * in other places in the system + */ +class Validate +{ + /** + * Is Valid? + * + * Checks if a variable is valid based on the passed rules. + * + * @param mixed $value the value to be validated + * @param array $rules an array of rules (and messages) to validate with + * @return mixed boolean true if valid, array of errors if invalid + */ + public static function isValid($value, $rules) + { + $errors = array(); + + if (is_array($rules)) + { + foreach ($rules as $rule => $message) + { + $rule = explode(':', $rule); + + switch (strtolower($rule[0])) + { + // @todo case 'alpha': + // @todo case 'alphanumeric': + // @todo case 'date': + + // {{{ Checks using filter_var() + + case 'filter': + if (count($rule) < 2) + { + throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|regex|url".'); + } + else + { + switch (strtolower($rule[1])) + { + case 'boolean': + case 'email': + case 'float': + case 'int': + case 'ip': + case 'regex': + case 'url': + $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); + break; + + default: + throw new Exception('Invalid filter, expecting boolean, email, float, int, ip, regex or url.'); + break; + } + + if (!filter_var($value, $filter)) + { + $errors[] = $message; + } + } + + break; + + // }}} + // {{{ Checks using strlen() + + case 'length': + if (count($rule) < 3) + { + throw new Exception('Invalid validation rule, expected: "length:<|<=|==|!=|>=|>:integer".'); + } + else + { + if (!filter_var($rule[2], FILTER_VALIDATE_INT)) + { + throw new Exception('Invalid length value, expecting an integer.'); + } + else + { + $length = strlen($value); + + switch ($rule[1]) + { + case '<': $valid = $length < $rule[2]; break; + case '<=': $valid = $length <= $rule[2]; break; + case '==': $valid = $length == $rule[2]; break; + case '!=': $valid = $length != $rule[2]; break; + case '>=': $valid = $length >= $rule[2]; break; + case '>': $valid = $length > $rule[2]; break; + + default: + throw new Exception('Invalid operator, expecting <, <=, ==, !=, >= or >.'); + break; + } + + if ($valid === true) + { + $errors[] = $message; + } + } + } + + break; + + // }}} + + // @todo case 'range': + + // {{{ Checks using preg_match() + + case 'regex': + if (count($rule) < 3) + { + throw new Exception('Invalid validation rule, expected: "regex:is|not:string".'); + } + else + { + if ((strtolower($rule[1]) == 'not' && !preg_match($rule[2], $value)) || preg_match($rule[2], $value)) + { + $errors[] = $message; + } + } + break; + + // }}} + } + + } + } + + return count($errors) ? $errors : true; + } +} + +?>