From 36c58f84d80d861853365ec2b8a8e607f8edc644 Mon Sep 17 00:00:00 2001 From: Peter Mitchell Date: Mon, 9 Jun 2014 22:24:55 -0400 Subject: [PATCH] [HttpFoundation] Added ParameterBag::getBoolean Added the getBoolean method as a convenience on top of the pre-existing ParameterBag::filter method --- .../Component/HttpFoundation/ParameterBag.php | 14 ++++++++++++++ .../HttpFoundation/Tests/ParameterBagTest.php | 13 +++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Symfony/Component/HttpFoundation/ParameterBag.php b/src/Symfony/Component/HttpFoundation/ParameterBag.php index 8791275bafda..18ca3ea54700 100644 --- a/src/Symfony/Component/HttpFoundation/ParameterBag.php +++ b/src/Symfony/Component/HttpFoundation/ParameterBag.php @@ -253,6 +253,20 @@ public function getInt($key, $default = 0, $deep = false) return (int) $this->get($key, $default, $deep); } + /** + * Returns the parameter value converted to boolean. + * + * @param string $key The parameter key + * @param mixed $default The default value if the parameter key does not exist + * @param bool $deep If true, a path like foo[bar] will find deeper items + * + * @return bool The filtered value + */ + public function getBoolean($key, $default = false, $deep = false) + { + return $this->filter($key, $default, $deep, FILTER_VALIDATE_BOOLEAN); + } + /** * Filter key. * diff --git a/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php b/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php index 7f4f243b481b..0492e2de3168 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php @@ -251,4 +251,17 @@ public function testCount() $this->assertEquals(count($parameters), count($bag)); } + + /** + * @covers Symfony\Component\HttpFoundation\ParameterBag::getBoolean + */ + public function testGetBoolean() + { + $parameters = array('string_true' => 'true', 'string_false' => 'false'); + $bag = new ParameterBag($parameters); + + $this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true'); + $this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false'); + $this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined'); + } }