From 20aff31b94f106687151f129dd0bbc094ad21dbd Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Sat, 27 Sep 2014 12:06:33 -0400 Subject: [PATCH] Moved validate logic into resource I couldn't find a single scenario where I was using the Validate class in my site level code. Dropped the overhead of calling a static class method multiple times on a page by moving the logic in the Resource class. Also changed the response to always return the errors as parameter => array. This allows a developer to choose if they want to display one error or all of the errors. --- src/classes/Resource.php | 140 +++++++++++++++++++++++++++++-- src/classes/Validate.php | 175 --------------------------------------- 2 files changed, 135 insertions(+), 180 deletions(-) delete mode 100644 src/classes/Validate.php diff --git a/src/classes/Resource.php b/src/classes/Resource.php index 49cf0a4..dd3430e 100644 --- a/src/classes/Resource.php +++ b/src/classes/Resource.php @@ -124,7 +124,7 @@ class Resource extends Object { foreach ($missing_variables as $variable) { - $this->errors[$variable] = 'The ' . $variable . ' parameter is required.'; + $this->errors[$variable][] = 'The ' . $variable . ' parameter is required.'; } } } @@ -156,11 +156,129 @@ class Resource extends Object { if (is_array($rules)) { - $rule_errors = Validate::isValid($global[$variable], $rules); - - if (is_array($rule_errors)) + foreach ($rules as $rule => $message) { - $this->errors[$variable] = $rule_errors[0]; + $rule = explode(':', $rule); + + switch (strtolower($rule[0])) + { + // {{{ Checks using filter_var() + + case 'filter': + if (count($rule) < 2) + { + throw new Exception('Invalid validation rule, expected: "validate:boolean|email|float|int|ip|url".'); + } + else + { + switch (strtolower($rule[1])) + { + case 'boolean': + case 'email': + case 'float': + case 'int': + case 'ip': + case 'url': + $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); + break; + + default: + throw new Exception('Invalid filter, expecting boolean, email, float, int, ip or url.'); + break; + } + + if (!filter_var($value, $filter)) + { + $this->errors[$variable][] = $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) + { + $this->errors[$variable][] = $message; + } + } + } + + break; + + // }}} + // {{{ Checks using preg_match() + + case 'regex': + if (count($rule) < 3) + { + throw new Exception('Invalid validation rule, expected: "regex:is|not:string".'); + } + else + { + $rule[1] = strtolower($rule[1]); + + if (($rule[1] == 'is' && preg_match($rule[2], $value)) + || ($rule[1] == 'not' && !preg_match($rule[2], $value))) + { + $this->errors[$variable][] = $message; + } + } + break; + + // }}} + // @todo case 'alpha': + // @todo case 'alphanumeric': + // @todo case 'date': + // @todo case 'range': + } } } } @@ -229,6 +347,18 @@ class Resource extends Object 'message' => $this->message, ]; + // Forces errors to be an array of arrays + if ($this->errors) + { + foreach ($this->errors as $key => $error) + { + if (!is_array($error)) + { + $this->errors[$key] = [$error]; + } + } + } + foreach (['echo', 'limit', 'offset', 'errors'] as $variable) { if ($this->$variable) diff --git a/src/classes/Validate.php b/src/classes/Validate.php deleted file mode 100644 index 2f613a3..0000000 --- a/src/classes/Validate.php +++ /dev/null @@ -1,175 +0,0 @@ - - * @copyright Copyright 2007-2014, 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 = []; - - 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|url".'); - } - else - { - switch (strtolower($rule[1])) - { - case 'boolean': - case 'email': - case 'float': - case 'int': - case 'ip': - case 'url': - $filter = constant('FILTER_VALIDATE_' . strtoupper($rule[1])); - break; - - default: - throw new Exception('Invalid filter, expecting boolean, email, float, int, ip 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) - { - $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 - { - $rule[1] = strtolower($rule[1]); - - if (($rule[1] == 'is' && preg_match($rule[2], $value)) - || ($rule[1] == 'not' && !preg_match($rule[2], $value))) - { - $errors[] = $message; - } - } - break; - - // }}} - } - - } - } - - return count($errors) ? $errors : true; - } -} -