Skip to content

Commit

Permalink
[HttpFoundation] Deprecate $deep parameter on ParameterBag
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 27, 2015
1 parent a149609 commit 5ed0ec3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
25 changes: 20 additions & 5 deletions ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function add(array $parameters = array())
*/
public function get($key, $default = null, $deep = false)
{
if (true === $deep) {
if ($deep) {
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public function getAlnum($key, $default = '', $deep = false)
public function getDigits($key, $default = '', $deep = false)
{
// we need to remove - and + because they're allowed in the filter
return str_replace(array('-', '+'), '', $this->filter($key, $default, $deep, FILTER_SANITIZE_NUMBER_INT));
return str_replace(array('-', '+'), '', $this->filter($key, $default, FILTER_SANITIZE_NUMBER_INT, array(), $deep));
}

/**
Expand Down Expand Up @@ -242,24 +242,39 @@ public function getInt($key, $default = 0, $deep = false)
*/
public function getBoolean($key, $default = false, $deep = false)
{
return $this->filter($key, $default, $deep, FILTER_VALIDATE_BOOLEAN);
return $this->filter($key, $default, FILTER_VALIDATE_BOOLEAN, array(), $deep);
}

/**
* Filter key.
*
* @param string $key Key.
* @param mixed $default Default = null.
* @param bool $deep Default = false.
* @param int $filter FILTER_* constant.
* @param mixed $options Filter options.
* @param bool $deep Default = false.
*
* @see http://php.net/manual/en/function.filter-var.php
*
* @return mixed
*/
public function filter($key, $default = null, $deep = false, $filter = FILTER_DEFAULT, $options = array())
public function filter($key, $default = null, $filter = FILTER_DEFAULT, $options = array(), $deep = false)
{
static $filters = null;

if (null === $filters) {
foreach (filter_list() as $tmp) {
$filters[filter_id($tmp)] = 1;
}
}
if (is_bool($filter) || !isset($filters[$filter]) || is_array($deep)) {
@trigger_error('Passing the $deep boolean as 3rd argument to the '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0. Remove it altogether as the $deep argument will be removed in 3.0.', E_USER_ERROR);
$tmp = $deep;
$deep = $filter;
$filter = $options;
$options = $tmp;
}

$value = $this->get($key, $default, $deep);

// Always turn $options into an array - this allows filter_var option shortcuts.
Expand Down
2 changes: 1 addition & 1 deletion Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ public static function getHttpMethodParameterOverride()
*/
public function get($key, $default = null, $deep = false)
{
if (true === $deep) {
if ($deep) {
@trigger_error('Using paths to find deeper items in '.__METHOD__.' is deprecated since version 2.8 and will be removed in 3.0. Filter the returned value in your own code instead.', E_USER_DEPRECATED);
}

Expand Down
14 changes: 7 additions & 7 deletions Tests/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,26 +172,26 @@ public function testFilter()

$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found');

$this->assertEquals('0123', $bag->filter('digits', '', false, FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');
$this->assertEquals('0123', $bag->filter('digits', '', FILTER_SANITIZE_NUMBER_INT), '->filter() gets a value of parameter as integer filtering out invalid characters');

$this->assertEquals('example@example.com', $bag->filter('email', '', false, FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');
$this->assertEquals('example@example.com', $bag->filter('email', '', FILTER_VALIDATE_EMAIL), '->filter() gets a value of parameter as email');

$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, array('flags' => FILTER_FLAG_PATH_REQUIRED)), '->filter() gets a value of parameter as URL with a path');

// This test is repeated for code-coverage
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', false, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');
$this->assertEquals('http://example.com/foo', $bag->filter('url', '', FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED), '->filter() gets a value of parameter as URL with a path');

$this->assertFalse($bag->filter('dec', '', false, FILTER_VALIDATE_INT, array(
$this->assertFalse($bag->filter('dec', '', FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries');

$this->assertFalse($bag->filter('hex', '', false, FILTER_VALIDATE_INT, array(
$this->assertFalse($bag->filter('hex', '', FILTER_VALIDATE_INT, array(
'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries');

$this->assertEquals(array('bang'), $bag->filter('array', '', false), '->filter() gets a value of parameter as an array');
$this->assertEquals(array('bang'), $bag->filter('array', ''), '->filter() gets a value of parameter as an array');
}

public function testGetIterator()
Expand Down

0 comments on commit 5ed0ec3

Please sign in to comment.