Skip to content

Commit bc2a113

Browse files
committed
feature #11086 [HttpFoundation] Added ParameterBag::getBoolean (peterjmit)
This PR was merged into the 2.3-dev branch. Discussion ---------- [HttpFoundation] Added ParameterBag::getBoolean | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a At the moment to pull a boolean value from a request the user has to use `ParameterBag::filter` or use `filter_var` directly ```php // GET /products?hide_archived=true $request->query->filter('hide_archived', false, false, FILTER_VALIDATE_BOOLEAN); // or filter_var($request->query->get('hide_archived'), FILTER_VALIDATE_BOOLEAN); ``` This is a pure convenience addition, adding a nice way of pulling a boolean value from a query string or request body (especially if http form/url encoded). Example usage: ```php // GET /products?hide_archived=true $request->query->getBoolean('hide_archived'); // (boolean) true $request->query->get('hide_archived'); // (string) "true" // GET /products?hide_archived=1 $request->query->getBoolean('hide_archived'); // (boolean) true // GET /products?hide_archived=false $request->query->getBoolean('hide_archived'); // (boolean) false // GET /products?hide_archived=banana $request->query->getBoolean('hide_archived'); // (boolean) false ``` Commits ------- 36c58f8 [HttpFoundation] Added ParameterBag::getBoolean
2 parents ba40ab5 + 9c2dfce commit bc2a113

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

ParameterBag.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,20 @@ public function getInt($key, $default = 0, $deep = false)
253253
return (int) $this->get($key, $default, $deep);
254254
}
255255

256+
/**
257+
* Returns the parameter value converted to boolean.
258+
*
259+
* @param string $key The parameter key
260+
* @param mixed $default The default value if the parameter key does not exist
261+
* @param bool $deep If true, a path like foo[bar] will find deeper items
262+
*
263+
* @return bool The filtered value
264+
*/
265+
public function getBoolean($key, $default = false, $deep = false)
266+
{
267+
return $this->filter($key, $default, $deep, FILTER_VALIDATE_BOOLEAN);
268+
}
269+
256270
/**
257271
* Filter key.
258272
*

Tests/ParameterBagTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,4 +251,17 @@ public function testCount()
251251

252252
$this->assertEquals(count($parameters), count($bag));
253253
}
254+
255+
/**
256+
* @covers Symfony\Component\HttpFoundation\ParameterBag::getBoolean
257+
*/
258+
public function testGetBoolean()
259+
{
260+
$parameters = array('string_true' => 'true', 'string_false' => 'false');
261+
$bag = new ParameterBag($parameters);
262+
263+
$this->assertTrue($bag->getBoolean('string_true'), '->getBoolean() gets the string true as boolean true');
264+
$this->assertFalse($bag->getBoolean('string_false'), '->getBoolean() gets the string false as boolean false');
265+
$this->assertFalse($bag->getBoolean('unknown'), '->getBoolean() returns false if a parameter is not defined');
266+
}
254267
}

0 commit comments

Comments
 (0)