Added variable filtering and validation

Validation logic was existing but it was reworked to abstract out checking for
required fields initially and then sanity checks after the fact. Filters are
applied before validation but after checking existence. No support for _PUT and
_DELETE at the moment as those do not exist as super globals natively in PHP.
This commit is contained in:
Josh Sherman 2014-09-27 08:13:05 -04:00
parent 0ad0754726
commit a749c80d93

View file

@ -36,6 +36,15 @@ class Resource extends Object
*/
public $secure = false;
/**
* Required
*
* Variables that are required.
*
* @var array
*/
public $required = [];
/**
* Filter
*
@ -61,6 +70,7 @@ class Resource extends Object
public $limit = false;
public $offset = false;
public $errors = [];
public $uids = [];
// @todo if $status != 200 && $message == 'OK' ...
@ -72,55 +82,72 @@ class Resource extends Object
* typically used when a module is called outside of the scope of the
* controller (the registration page calls the login page in this manner.
*/
public function __construct()
public function __construct($uids = false)
{
$this->uids = $uids;
parent::__construct(['cache', 'db']);
$method = $_SERVER['REQUEST_METHOD'];
$filter = isset($this->filter[$method]);
$validate = isset($this->validate[$method]);
if ($filter || $validate)
{
// Hack together some new globals
if (in_array($method, ['PUT', 'DELETE']))
{
$GLOBALS['_' . $method] = [];
// @todo Populate it
}
/**
* 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()
{
$errors = [];
$global =& $GLOBALS['_' . $method];
if ($this->validate)
// Checks that the required parameters are present
// @todo Add in support for uid:* variables
if ($validate)
{
if (is_array($this->method))
$variables = [];
foreach ($this->validate[$method] as $variable => $rules)
{
$this->method = $this->method[0];
}
switch (strtoupper($this->method))
{
case 'GET':
$global = &$_GET;
break;
case 'POST':
$global = &$_POST;
break;
default:
$global = &$_REQUEST;
break;
}
foreach ($this->validate as $variable => $rules)
{
if (!is_array($rules) && $rules !== true)
if (!is_array($rules))
{
$variable = $rules;
$rules = true;
}
$variables[] = $variable;
}
$missing_variables = array_diff($variables, array_keys($global));
if ($missing_variables !== array())
{
foreach ($missing_variables as $variable)
{
$this->errors[$variable] = 'The ' . $variable . ' parameter is required.';
}
}
}
foreach ($global as $variable => $value)
{
// Applies any filters
if ($filter && isset($this->filter[$method][$variable]))
{
// @todo Definitely could see the need to expand this out
// to allow for more robust filters to be applied
// similar to how the validation logic work.
$global[$variable] = $this->filter[$method][$variable]($value);
}
if ($validate && isset($this->validate[$method][$variable]))
{
$rules = $this->validate[$method][$variable];
if (is_array($rules))
{
if (isset($global[$variable]) && !String::isEmpty($global[$variable]))
{
if (is_array($rules))
@ -129,18 +156,20 @@ class Resource extends Object
if (is_array($rule_errors))
{
$errors = array_merge($errors, $rule_errors);
$this->errors[$variable] = $rule_errors[0];
}
}
}
else
{
$errors[] = 'The ' . $variable . ' field is required.';
}
}
}
return $errors == [] ? false : $errors;
// if PUT or DELETE, need to update the super globals directly as
// they do not stay in sync. Probably need to make them global in
// this class method
//
// $_PUT = $GLOBALS['_PUT'];
}
}
}