From 54454ba4aa7264ce9b607d000f5635d0e664f199 Mon Sep 17 00:00:00 2001 From: Drak Date: Mon, 26 Sep 2011 04:05:08 +0545 Subject: [PATCH 1/3] Added generic filtering to ParameterBag. Adds filtering convenience using PHP's filter_var() e.g. `$request->get->filter($key, '', false, FITLER_SANITIZE_STRING);` See http://php.net/manual/en/filter.filters.php for capabilities. --- .../Component/HttpFoundation/ParameterBag.php | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 3a38b5fab334..17f2151cf7d2 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -242,4 +242,25 @@ 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 array $options Fitler options. + * + * @return mixed + */ + public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, array $options=array()) + { + $value = $this->get($key, $default, $deep); + if (is_array($value)) { + $options['flags'] = FILTER_REQUIRE_ARRAY; + } + + return filter_var($value, $filter, $options); + } } From 6aec7898e36820d2dd6ecab197bf7024ae9952dc Mon Sep 17 00:00:00 2001 From: Drak Date: Tue, 27 Sep 2011 15:20:51 +0545 Subject: [PATCH 2/3] Added tests. --- .../Component/HttpFoundation/ParameterBag.php | 2 +- .../HttpFoundation/ParameterBagTest.php | 35 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 17f2151cf7d2..53a255948646 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -250,7 +250,7 @@ public function getInt($key, $default = 0, $deep = false) * @param mixed $default Default = null. * @param boolean $deep Default = false. * @param integer $filter FILTER_* constant. - * @param array $options Fitler options. + * @param array $options Filter options - can be an array of options or FILTER_* constant. * * @return mixed */ diff --git a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php index d93f6a4d557b..331c1997299c 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php @@ -162,4 +162,39 @@ 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/', + 'dec' => '256', + 'hex' => '0x100', + '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->assertNotEquals('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->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->assertNotEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array'); + } } From c4a0f799af9d018279ea8e3701c06578acb9d7e2 Mon Sep 17 00:00:00 2001 From: Drak Date: Tue, 27 Sep 2011 20:14:32 +0545 Subject: [PATCH 3/3] Updates according to suggestions. - Simplified logic of tests. - Added more comments/docblocks. - Added more convenience. --- .../Component/HttpFoundation/ParameterBag.php | 17 +++++++++++++---- .../HttpFoundation/ParameterBagTest.php | 13 +++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 53a255948646..16e892f2f7f6 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -250,17 +250,26 @@ public function getInt($key, $default = 0, $deep = false) * @param mixed $default Default = null. * @param boolean $deep Default = false. * @param integer $filter FILTER_* constant. - * @param array $options Filter options - can be an array of options or 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, array $options=array()) + public function filter($key, $default = null, $deep = false, $filter=FILTER_DEFAULT, $options=array()) { $value = $this->get($key, $default, $deep); - if (is_array($value)) { + + // 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); } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php index 331c1997299c..da0eb4af4a01 100644 --- a/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php +++ b/tests/Symfony/Tests/Component/HttpFoundation/ParameterBagTest.php @@ -171,10 +171,10 @@ public function testFilter() $bag = new ParameterBag(array( 'digits' => '0123ab', 'email' => 'example@example.com', - 'url' => 'http://example.com/', + 'url' => 'http://example.com/foo', 'dec' => '256', 'hex' => '0x100', - 'array' => 'bang', + 'array' => array('bang'), )); $this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found'); @@ -183,7 +183,10 @@ public function testFilter() $this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email'); - $this->assertNotEquals('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->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, @@ -195,6 +198,8 @@ public function testFilter() 'options' => array('min_range' => 1, 'max_range' => 0xff)) ), '->filter() gets a value of parameter as integer between boundaries'); - $this->assertNotEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array'); + $this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array'); + + } }