Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[HttpFoundation] Adds various return-typed getters to ParameterBag #49972

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 7 commits into from
Closed
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
21 changes: 21 additions & 0 deletions 21 src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,13 @@ public function getString(string $key, string $default = ''): string
return (string) $value;
}

public function getStringOrNull(string $key): ?string
{
return $this->has($key)
? $this->getString($key)
: null;
}

/**
* Returns the parameter value converted to integer.
*/
Expand All @@ -155,6 +162,13 @@ public function getInt(string $key, int $default = 0): int
return $this->filter($key, $default, \FILTER_VALIDATE_INT, ['flags' => \FILTER_REQUIRE_SCALAR]) ?: 0;
}

public function getIntOrNull(string $key): ?int
{
return $this->has($key)
Copy link
Member

Choose a reason for hiding this comment

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

Checking if the value is set is not necessary:
filter_var(null, \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE) returns null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to use getInt implementation instead.

? $this->getInt($key)
: null;
}

/**
* Returns the parameter value converted to boolean.
*/
Expand All @@ -163,6 +177,13 @@ public function getBoolean(string $key, bool $default = false): bool
return $this->filter($key, $default, \FILTER_VALIDATE_BOOL, ['flags' => \FILTER_REQUIRE_SCALAR]);
}

public function getBooleanOrNull(string $key): ?bool
{
return $this->has($key)
? $this->getBoolean($key)
: null;
}

/**
* Returns the parameter value converted to an enum.
*
Expand Down
24 changes: 24 additions & 0 deletions 24 src/Symfony/Component/HttpFoundation/Tests/ParameterBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,30 @@ public function testGetEnumThrowsExceptionWithInvalidValueType()

$this->assertNull($bag->getEnum('invalid-value', FooEnum::class));
}

public function testGetStringOrNull(): void
{
$bag = new ParameterBag(['valid-value' => 'foo']);

$this->assertSame('foo', $bag->getStringOrNull('valid-value'));
$this->assertNull($bag->getStringOrNull('missing-key'));
}

public function testGetIntOrNull(): void
{
$bag = new ParameterBag(['valid-value' => 1]);
Copy link
Member

Choose a reason for hiding this comment

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

An assertion with an invalid value is necessary, i.e. with a value that cannot be converted to int. Same for Boolean.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've changed it to using native getInt calls etc. so behaviour will be same in both cases.


$this->assertSame(1, $bag->getIntOrNull('valid-value'));
$this->assertNull($bag->getIntOrNull('missing-key'));
}

public function testGetBooleanOrNull(): void
{
$bag = new ParameterBag(['valid-value' => true]);

$this->assertTrue($bag->getBooleanOrNull('valid-value'));
$this->assertNull($bag->getBooleanOrNull('missing-key'));
}
}

class InputStringable
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.