8000 Add `getAny` in InputBag by BafS · Pull Request #36725 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Add getAny in InputBag #36725

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 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function handleRequest(FormInterface $form, $request = null)
return;
}

$data = $request->query->all()[$name];
$data = $request->query->getAny($name);
}
} else {
// Mark the form with an error if the uploaded size was too large
Expand All @@ -87,7 +87,7 @@ public function handleRequest(FormInterface $form, $request = null)
$files = $request->files->all();
} elseif ($request->request->has($name) || $request->files->has($name)) {
$default = $form->getConfig()->getCompound() ? [] : null;
$params = $request->request->all()[$name] ?? $default;
$params = $request->request->getAny($name, $default);
$files = $request->files->get($name, $default);
} else {
// Don't submit the form if it is not present in the request
Expand Down
10000
14 changes: 13 additions & 1 deletion src/Symfony/Component/HttpFoundation/InputBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ public function get(string $key, $default = null)
return $this === $value ? $default : $value;
}

/**
* Returns any input value by name.
*
* @param mixed $default The default value if the input key does not exist
*
* @return mixed
*/
public function getAny(string $key, $default = null)
{
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
}

/**
* Returns the inputs.
*
Expand Down Expand Up @@ -99,7 +111,7 @@ public function set(string $key, $value)
*/
public function filter(string $key, $default = null, int $filter = FILTER_DEFAULT, $options = [])
{
$value = $this->has($key) ? $this->all()[$key] : $default;
$value = $this->getAny($key, $default);

// Always turn $options into an array - this allows filter_var option shortcuts.
if (!\is_array($options) && $options) {
Expand Down
11 changes: 11 additions & 0 deletions src/Symfony/Component/HttpFoundation/Tests/InputBagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ public function testGetDoesNotUseDeepByDefault()
$this->assertNull($bag->get('foo[bar]'));
}

public function testGetAny()
{
$bag = new InputBag(['foo' => 'bar', 'null' => null, 'arr' => [1], 'num' => 1]);

$this->assertSame('bar', $bag->getAny('foo'), '->getAny() gets the value of a parameter (string)');
$this->assertSame([1], $bag->getAny('arr'), '->getAny() gets the value of a parameter (array)');
$this->assertSame(1, $bag->getAny('num'), '->getAny() gets the value of a parameter (int)');
$this->assertSame([], $bag->getAny('unknown', []), '->getAny() returns second argument as default if a parameter is not defined');
$this->assertNull($bag->getAny('null'), '->getAny() returns null if null is set');
}

public function testAllWithInputKey()
{
$bag = new InputBag(['foo' => ['bar', 'baz'], 'null' => null]);
Expand Down
0