[HttpFoundation] Added ParameterBag::getBoolean

Added the getBoolean method as a convenience on top of the pre-existing
ParameterBag::filter method
This commit is contained in:
Peter Mitchell 2014-06-09 22:24:55 -04:00
parent 8f0b984d76
commit 36c58f84d8
2 changed files with 27 additions and 0 deletions

View File

@ -253,6 +253,20 @@ class ParameterBag implements \IteratorAggregate, \Countable
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.
*

View File

@ -251,4 +251,17 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
$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');
}
}