8000 [HttpFoundation] removed the ParameterBag::get() deep argument by fabpot · Pull Request #16019 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpFoundation] removed the ParameterBag::get() deep argument #16019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 2 additions & 53 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,67 +78,16 @@ public function add(array $parameters = array())
/**
* Returns a parameter by name.
*
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
*
* @param string $key The 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 mixed
*
* @throws \InvalidArgumentException
*/
public function get($key, $default = null, $deep = false)
public function get($key, $default = null)
{
if (true === $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);
}

if (!$deep || false === $pos = strpos($key, '[')) {
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

$root = substr($key, 0, $pos);
if (!array_key_exists($root, $this->parameters)) {
return $default;
}

$value = $this->parameters[$root];
$currentKey = null;
for ($i = $pos, $c = strlen($key); $i < $c; ++$i) {
$char = $key[$i];

if ('[' === $char) {
if (null !== $currentKey) {
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
}

$currentKey = '';
} elseif (']' === $char) {
if (null === $currentKey) {
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
}

if (!is_array($value) || !array_key_exists($currentKey, $value)) {
return $default;
}

$value = $value[$currentKey];
$currentKey = null;
} else {
if (null === $currentKey) {
throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
}

$currentKey .= $char;
}
}

if (null !== $currentKey) {
throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
}

return $value;
return array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

/**
Expand Down
15 changes: 4 additions & 11 deletions src/Symfony/Component/HttpFoundation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,29 +714,22 @@ public static function getHttpMethodParameterOverride()
* It is better to explicitly get request parameters from the appropriate
* public property instead (query, attributes, request).
*
* Note: Finding deep items is deprecated since version 2.8, to be removed in 3.0.
*
* @param string $key the key
* @param mixed $default the default value
* @param bool $deep is parameter deep in multidimensional array
*
* @return mixed
*/
public function get($key, $default = null, $deep = false)
public function get($key, $default = null)
{
if (true === $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);
}

if ($this !== $result = $this->query->get($key, $this, $deep)) {
if ($this !== $result = $this->query->get($key, $this)) {
return $result;
}

if ($this !== $result = $this->attributes->get($key, $this, $deep)) {
if ($this !== $result = $this->attributes->get($key, $this)) {
return $result;
}

if ($this !== $result = $this->request->get($key, $this, $deep)) {
if ($this !== $result = $this->request->get($key, $this)) {
return $result;
}

Expand Down
35 changes: 0 additions & 35 deletions src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,41 +85,6 @@ public function testGetDoesNotUseDeepByDefault()
$this->assertNull($bag->get('foo[bar]'));
}

/**
* @group legacy
* @dataProvider getInvalidPaths
* @expectedException \InvalidArgumentException
*/
public function testGetDeepWithInvalidPaths($path)
{
$bag = new ParameterBag(array('foo' => array('bar' => 'moo')));

$bag->get($path, null, true);
}

public function getInvalidPaths()
{
return array(
array('foo[['),
array('foo[d'),
array('foo[bar]]'),
array('foo[bar]d'),
);
}

/**
* @group legacy
*/
public function testGetDeep()
{
$bag = new ParameterBag(array('foo' => array('bar' => array('moo' => 'boo'))));

$this->assertEquals(array('moo' => 'boo'), $bag->get('foo[bar]', null, true));
$this->assertEquals('boo', $bag->get('foo[bar][moo]', null, true));
$this->assertEquals('default', $bag->get('foo[bar][foo]', 'default', true));
$this->assertEquals('default', $bag->get('bar[moo][foo]', 'default', true));
}

/**
* @covers Symfony\Component\HttpFoundation\ParameterBag::set
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testFailurePathCanBeOverwritten()
public function testFailurePathCanBeOverwrittenWithRequest()
{
$this->request->expects($this->once())
->method('get')->with('_failure_path', null, false)
->method('get')->with('_failure_path')
->will($this->returnValue('/auth/login'));

$this->httpUtils->expects($this->once())
Expand All @@ -158,7 +158,7 @@ public function testFailurePathCanBeOverwrittenWithRequest()
public function testFailurePathCanBeOverwrittenWithNestedAttributeInRequest()
{
$this->request->expects($this->once())
->method('get')->with('_failure_path', null, false)
->method('get')->with('_failure_path')
->will($this->returnValue(array('value' => '/auth/login')));

$this->httpUtils->expects($this->once())
Expand All @@ -173,7 +173,7 @@ public function testFailurePathParameterCanBeOverwritten()
$options = array('failure_path_parameter' => '_my_failure_path');

$this->request->expects($this->once())
->method('get')->with('_my_failure_path', null, false)
->method('get')->with('_my_failure_path')
->will($this->returnValue('/auth/login'));

$this->httpUtils->expects($this->once())
Expand Down
0