[NFR] Methods \Phalcon\Http\Request::setQueryFilters() and \Phalcon\Http\Request::getFilteredQuery()
First I want to thank all participants of this wonderful project. Thank you for this project.
Type: new feature
I suggest adding methods : \Phalcon\Http\Request::setQueryFilters(string $name, array $filters) and \Phalcon\Http\Request::getFilteredQuery(string $name, $defaultValue = null, bool $notAllowEmpty = false, bool $noRecursive = false).
How to use:
// service registration
$di->setShared('request', function () {
$request = new \Phalcon\Http\Request();
// Global filters for the entire application
$request->setQueryFilters('id', ['absint']);
$request->setQueryFilters('role', ['trim', 'string', 'lower']);
$request->setQueryFilters('param', ['absint']);
return $request;
});
and in controllers:
class IndexController extends ControllerBase
{
public function indexAction()
{
$param = $this->request->getFilteredQuery('param');
// ....
}
}
How did I realize this in my project:
<?php
namespace Demo\Components\Http;
class Request extends \Phalcon\Http\Request
{
/**
* @var array
*/
protected $_queryFilters = [];
/**
* @param string $param
* @param array $filsters
*/
public function setQueryFilters(string $name, array $filters)
{
$this->_queryFilters[$name] = $filters;
}
/**
* @param string $name
* @param null $defaultValue
* @param bool $notAllowEmpty
* @param bool $noRecursive
*
* @return mixed
*/
public function getFilteredQuery(string $name, $defaultValue = null, bool $notAllowEmpty = false, bool $noRecursive = false)
{
$filters = null;
if (isset($this->_queryFilters[$name])) {
$filters = $this->_queryFilters[$name];
}
return parent::getQuery($name, $filters, $defaultValue, $notAllowEmpty, $noRecursive);
}
}
+1
@ekmst What would happen when the filter does not exist?
For instance consider this typo:
$di->setShared('request', function () {
$request = new \Phalcon\Http\Request();
// Global filters for the entire application
$request->setQueryFilters('id', ['abbbbsint']);
return $request;
});
Should we just return the value as is because the filter is not found or throw an exception? At the moment the filter locator throws an exception if it cannot locate the relevant sanitizer
I am in favor of leaving the exception because it can help with typos from the developer. Also if we are to check the sanitizers, then that will introduce another loop in finding if a sanitizer exists or not
BTW this should also be implemented in the rest methods getPost, getPut etc.
Also what happens if you try to call getFilteredQuery for a parameter that does not have filters defined? You get back the passed variable pretty much?
This has been implemented. Thank you @ekmst
@ekmst What would happen when the filter does not exist?
For instance consider this typo:
$di->setShared('request', function () { $request = new \Phalcon\Http\Request(); // Global filters for the entire application $request->setQueryFilters('id', ['abbbbsint']); return $request; });Should we just return the value as is because the filter is not found or throw an exception? At the moment the filter locator throws an exception if it cannot locate the relevant sanitizer
I am in favor of leaving the exception because it can help with typos from the developer. Also if we are to check the sanitizers, then that will introduce another loop in finding if a sanitizer exists or not
I like the exception option. I think it will be more correct.
I ended up throwing the exception when the sanitizer does not exist when you register them. This way you have an early warning that you made a typo rather than getting errors when the sanitization happens.