Skip to content

[OptionsResolver] add ignoreUndefined() method to allow skip not interesting options #49608

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
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/OptionsResolver/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.3
---

* Add `OptionsResolver::ignoreUndefined()` to ignore not defined options while resolving

6.0
---

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/OptionsResolver/OptionConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,16 @@ public function info(string $info): static

return $this;
}

/**
* Sets whether ignore undefined options.
*
* @return $this
*/
public function ignoreUndefined(bool $ignore = true): static
{
$this->resolver->setIgnoreUndefined($ignore);

return $this;
}
}
23 changes: 22 additions & 1 deletion src/Symfony/Component/OptionsResolver/OptionsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ class OptionsResolver implements Options
*/
private $prototypeIndex;

/**
* Whether to ignore undefined options.
*/
private bool $ignoreUndefined = false;

/**
* Sets the default value of a given option.
*
Expand Down Expand Up @@ -862,7 +867,7 @@ public function resolve(array $options = []): array
$clone = clone $this;

// Make sure that no unknown options are passed
$diff = array_diff_key($options, $clone->defined);
$diff = $this->ignoreUndefined ? [] : array_diff_key($options, $clone->defined);

if (\count($diff) > 0) {
ksort($clone->defined);
Expand All @@ -873,6 +878,10 @@ public function resolve(array $options = []): array

// Override options set by the user
foreach ($options as $option => $value) {
if ($this->ignoreUndefined && !isset($clone->defined[$option])) {
continue;
}

$clone->given[$option] = true;
$clone->defaults[$option] = $value;
unset($clone->resolved[$option], $clone->lazy[$option]);
Expand Down Expand Up @@ -1210,6 +1219,18 @@ public function count(): int
return \count($this->defaults);
}

/**
* Sets whether ignore undefined options.
*
* @return $this
*/
public function setIgnoreUndefined(bool $ignore = true): static
{
$this->ignoreUndefined = $ignore;

return $this;
}

/**
* Returns a string representation of the value.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ protected function setUp(): void
$this->resolver = new OptionsResolver();
}

/**
* @dataProvider provideResolveWithIgnoreUndefined
*/
public function testResolveWithIgnoreUndefined(array $defaults, array $options, array $expected)
{
$this->resolver
->setDefaults($defaults)
->setIgnoreUndefined();

$this->assertSame($expected, $this->resolver->resolve($options));
}

public static function provideResolveWithIgnoreUndefined(): array
{
return [
'no defaults options, undefined resolves empty' => [[], ['c' => 4, 'd' => 5], []],
'empty options resolves defaults' => [['a' => '1', 'b' => '2'], [], ['a' => '1', 'b' => '2']],
'undefined options resolves defaults' => [['a' => '1', 'b' => '2'], ['c' => 3, 'd' => 4], ['a' => '1', 'b' => '2']],
'resolves defined' => [['a' => '1', 'b' => '2'], ['a' => '10', 'c' => '3'], ['b' => '2', 'a' => '10']],
];
}

public function testResolveFailsIfNonExistingOption()
{
$this->expectException(UndefinedOptionsException::class);
Expand Down