Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add nullable parameters handling
  • Loading branch information
mleczakm committed Apr 7, 2023
commit 8843e8ba964ce36261b36de5ec00917af1ca0c35
15 changes: 15 additions & 0 deletions src/Symfony/Component/HttpFoundation/ParameterBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public function getString(string $key, string $default = ''): string
return (string) $value;
}

public function getStringOrNull(string $key, string $default = null): ?string
{
return $this->filter($key, $default, \FILTER_SANITIZE_STRING, \FILTER_NULL_ON_FAILURE);
}

/**
* Returns the parameter value converted to integer.
*/
Expand All @@ -155,6 +160,11 @@ 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 $default = null): ?int
{
return $this->filter($key, $default, \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE);
}

/**
* Returns the parameter value converted to boolean.
*/
Expand All @@ -163,6 +173,11 @@ 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 $default = null): ?bool
{
return $this->filter($key, $default, \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE);
}

/**
* Returns the parameter value converted to an enum.
*
Expand Down
24 changes: 24 additions & 0 deletions 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()
{
$bag = new ParameterBag(['valid-value' => 'foo']);

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

public function testGetIntegerOrNull()
{
$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->getIntegerOrNull('valid-value'));
$this->assertNull($bag->getIntegerOrNull('invalid-key'));
}

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

$this->assertSame(true, $bag->getBooleanOrNull('valid-value'));
$this->assertNull($bag->getBooleanOrNull('invalid-key'));
}
}

class InputStringable
Expand Down