Skip to content

Added generic filtering to ParameterBag. #2261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Sep 27, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,34 @@ public function getInt($key, $default = 0, $deep = false)
{
return (int) $this->get($key, $default, $deep);
}

/**
* Filter key.
*
* @param string $key Key.
* @param mixed $default Default = null.
* @param boolean $deep Default = false.
* @param integer $filter FILTER_* constant.
* @param mixed $options Filter options.
*
* @see http://php.net/manual/en/function.filter-var.php
*
* @return mixed
*/
public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
{
$value = $this->get($key, $default, $deep);

// Always turn $options into an array - this allows filter_var option shortcuts.
if (!is_array($options) && $options) {
$options = array('flags' => $options);
}

// Add a convenience check for arrays.
if (is_array($value) && !isset($options['flags'])) {
$options['flags'] = FILTER_REQUIRE_ARRAY;
}

return filter_var($value, $filter, $options);
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was mulling over that earlier today and came up with the following:

public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
{
    $value = $this->get($key, $default, $deep);

    // Always turn $options into an array - this allows filter_var option shortcuts.
    if (!is_array($options) && $options) {
        $options = array('flags' => $options);
    }

    // Add a convenience check for arrays.
    if (is_array($value) && !isset($options['flags'])) {
        $options['flags'] = FILTER_REQUIRE_ARRAY;
    }

    return filter_var($value, $filter, $options);
}

This keeps the convenience of being able to handle arrays simply but the other alternative would be simply this:

public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array())
{
    return filter_var($this->get($key, $default, $deep), $filter, $options);
}

Let me know your preferences.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the convenience check of the first proposal.

}
40 changes: 40 additions & 0 deletions tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,44 @@ public function testGetInt()
$this->assertEquals(123, $bag->getInt('digits'), '->getInt() gets a value of parameter as integer');
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
}

/**
* @covers Symfony\Component\HttpFoundation\ParameterBag::filter
*/
public function testFilter()
{
$bag = new ParameterBag(array(
'digits' => '0123ab',
'email' => 'example@example.com',
'url' => 'http://example.com/foo',
'dec' => '256',
'hex' => '0x100',
'array' => array('bang'),
));

$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');

$this->assertEquals('0123', $bag->filter('digits', '', false, FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');

$this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');

$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as url with a path');

// This test is repeated for code-coverage
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as url with a path');

$this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff))
), '->filter() gets a value of parameter as integer between boundaries');

$this->assertFalse($bag->filter('hex', '', false, FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff))
), '->filter() gets a value of parameter as integer between boundaries');

$this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');


}
}