Skip to content

Commit

Permalink
feature #11086 [HttpFoundation] Added ParameterBag::getBoolean (peter…
Browse files Browse the repository at this point in the history
…jmit)

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
  • Loading branch information
fabpot committed Jun 16, 2014
2 parents f4a087b + 36c58f8 commit 1eed20a
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 1eed20a

Please sign in to comment.