[HttpFoundation] Deprecate $deep parameter on ParameterBag

This commit is contained in:
Nicolas Grekas 2015-11-27 11:11:22 +01:00
parent b29691067d
commit f4f082ee6d
3 changed files with 28 additions and 13 deletions

View File

@ -90,7 +90,7 @@ class ParameterBag implements \IteratorAggregate, \Countable
*/ */
public function get($key, $default = null, $deep = false) 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); @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);
} }
@ -214,7 +214,7 @@ class ParameterBag implements \IteratorAggregate, \Countable
public function getDigits($key, $default = '', $deep = false) public function getDigits($key, $default = '', $deep = false)
{ {
// we need to remove - and + because they're allowed in the filter // 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));
} }
/** /**
@ -242,7 +242,7 @@ class ParameterBag implements \IteratorAggregate, \Countable
*/ */
public function getBoolean($key, $default = false, $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);
} }
/** /**
@ -250,16 +250,31 @@ class ParameterBag implements \IteratorAggregate, \Countable
* *
* @param string $key Key. * @param string $key Key.
* @param mixed $default Default = null. * @param mixed $default Default = null.
* @param bool $deep Default = false.
* @param int $filter FILTER_* constant. * @param int $filter FILTER_* constant.
* @param mixed $options Filter options. * @param mixed $options Filter options.
* @param bool $deep Default = false.
* *
* @see http://php.net/manual/en/function.filter-var.php * @see http://php.net/manual/en/function.filter-var.php
* *
* @return mixed * @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); $value = $this->get($key, $default, $deep);
// Always turn $options into an array - this allows filter_var option shortcuts. // Always turn $options into an array - this allows filter_var option shortcuts.

View File

@ -724,7 +724,7 @@ class Request
*/ */
public function get($key, $default = null, $deep = false) 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); @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);
} }

View File

@ -172,26 +172,26 @@ class ParameterBagTest extends \PHPUnit_Framework_TestCase
$this->assertEmpty($bag->filter('nokey'), '->filter() should return empty by default if no key is found'); $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 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, 'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff), 'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries'); )), '->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, 'flags' => FILTER_FLAG_ALLOW_HEX,
'options' => array('min_range' => 1, 'max_range' => 0xff), 'options' => array('min_range' => 1, 'max_range' => 0xff),
)), '->filter() gets a value of parameter as integer between boundaries'); )), '->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() public function testGetIterator()