cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[NFR] Methods \Phalcon\Http\Request::setQueryFilters() and \Phalcon\Http\Request::getFilteredQuery()

Open ekmst opened this issue 8 years ago • 7 comments

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);
    }
}

ekmst avatar Sep 07 '18 17:09 ekmst

+1

wajdijurry avatar Sep 28 '18 14:09 wajdijurry

@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

niden avatar Jan 28 '19 03:01 niden

BTW this should also be implemented in the rest methods getPost, getPut etc.

niden avatar Jan 28 '19 03:01 niden

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?

niden avatar Jan 28 '19 04:01 niden

This has been implemented. Thank you @ekmst

niden avatar Feb 02 '19 14:02 niden

@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.

ekmst avatar Feb 15 '19 15:02 ekmst

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.

niden avatar Feb 15 '19 16:02 niden