Skip to content

Commit

Permalink
[HttpFoundation] Added ParameterBag::getBoolean
Browse files Browse the repository at this point in the history
Added the getBoolean method as a convenience on top of the pre-existing
ParameterBag::filter method
  • Loading branch information
peterjmit committed Jun 10, 2014
1 parent 8f0b984 commit 36c58f8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Expand Up @@ -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.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
Expand Up @@ -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');
}
}

0 comments on commit 36c58f8

Please sign in to comment.