Skip to content

[HttpFoundation] Add method getString to ParameterBag #46245

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

Closed
wants to merge 13 commits into from
6 changes: 6 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

6.2
---

* Add method `convertString` to `ParameterBag`
* Rename method `getInt` to `convertInt` in `ParameterBag`

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add deprecate convertInt ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but convertInt is not deprecated...?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant getInt()

6.1
---

Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,34 @@ public function getDigits(string $key, string $default = ''): string

/**
* Returns the parameter value converted to integer.
*
* @deprecated 6.2
*/
public function getInt(string $key, int $default = 0): int
{
trigger_deprecation('symfony/http-foundation', '6.1', 'Method "getInt" is deprecated because renamed to "convertInt".');

return (int) $this->get($key, $default);
}

/**
* Returns the parameter value converted to integer.
*/
public function convertInt(string $key, int $default = 0): int
{
return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['default' => $default, 'flags' => \FILTER_VALIDATE_INT]);
}

/**
* Returns the parameter value converted to string.
*/
public function convertString(string $key, string $default = ''): string
{
$output = $this->get($key, $default);

return \is_array($output) ? $default : (string) $output;
}

/**
* Returns the parameter value converted to boolean.
*/
Expand Down
23 changes: 23 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ public function testGetDigits()
$this->assertEquals('', $bag->getDigits('unknown'), '->getDigits() returns empty string if a parameter is not defined');
}

/**
* @deprecated 6.2
*/
public function testGetInt()
{
$bag = new ParameterBag(['digits' => '0123']);
Expand All @@ -141,6 +144,26 @@ public function testGetInt()
$this->assertEquals(0, $bag->getInt('unknown'), '->getInt() returns zero if a parameter is not defined');
}

public function testConvertInt()
{
$bag = new ParameterBag(['first' => '123', 'second' => 1.2, 'third' => [123, '0123']]);

$this->assertEquals(123, $bag->convertInt('first'), '->convertInt() gets a value of parameter as integer');
$this->assertEquals(0, $bag->convertInt('second'), '->convertInt() gets a value of parameter as integer');
$this->assertEquals(0, $bag->convertInt('third'), '->convertInt() gets a value of parameter as integer');
$this->assertEquals(0, $bag->convertInt('unknown'), '->convertInt() returns zero if a parameter is not defined');
}

public function testConvertString()
{
$bag = new ParameterBag(['first' => 'shtikov', 'second' => 123, 'third' => [123, '0123']]);

$this->assertSame('shtikov', $bag->convertString('first'), '->convertString() gets a value of parameter as string');
$this->assertSame('123', $bag->convertString('second'), '->convertString() gets a value of parameter as string');
$this->assertSame('', $bag->convertString('third'), '->convertString() gets a value of parameter as string');
$this->assertSame('', $bag->convertString('unknown'), '->convertString() returns empty string if a parameter is not defined');
}

public function testFilter()
{
$bag = new ParameterBag([
Expand Down