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

Skip to content

Commit 21cd852

Browse files
Constantine Shtompelnicolas-grekas
Constantine Shtompel
authored andcommitted
[OptionsResolver] add ignoreUndefined() method to allow skip not interesting options
1 parent 21c8789 commit 21cd852

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

src/Symfony/Component/OptionsResolver/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.3
5+
---
6+
7+
* Add `OptionsResolver::ignoreUndefined()` to ignore not defined options while resolving
8+
49
6.0
510
---
611

src/Symfony/Component/OptionsResolver/OptionConfigurator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,16 @@ public function info(string $info): static
134134

135135
return $this;
136136
}
137+
138+
/**
139+
* Sets whether ignore undefined options.
140+
*
141+
* @return $this
142+
*/
143+
public function ignoreUndefined(bool $ignore = true): static
144+
{
145+
$this->resolver->setIgnoreUndefined($ignore);
146+
147+
return $this;
148+
}
137149
}

src/Symfony/Component/OptionsResolver/OptionsResolver.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ class OptionsResolver implements Options
140140
*/
141141
private $prototypeIndex;
142142

143+
/**
144+
* Whether to ignore undefined options.
145+
*/
146+
private bool $ignoreUndefined = false;
147+
143148
/**
144149
* Sets the default value of a given option.
145150
*
@@ -862,7 +867,7 @@ public function resolve(array $options = []): array
862867
$clone = clone $this;
863868

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

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

874879
// Override options set by the user
875880
foreach ($options as $option => $value) {
881+
if ($this->ignoreUndefined && !isset($clone->defined[$option])) {
882+
continue;
883+
}
884+
876885
$clone->given[$option] = true;
877886
$clone->defaults[$option] = $value;
878887
unset($clone->resolved[$option], $clone->lazy[$option]);
@@ -1210,6 +1219,18 @@ public function count(): int
12101219
return \count($this->defaults);
12111220
}
12121221

1222+
/**
1223+
* Sets whether ignore undefined options.
1224+
*
1225+
* @return $this
1226+
*/
1227+
public function setIgnoreUndefined(bool $ignore = true): static
1228+
{
1229+
$this->ignoreUndefined = $ignore;
1230+
1231+
return $this;
1232+
}
1233+
12131234
/**
12141235
* Returns a string representation of the value.
12151236
*

src/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ protected function setUp(): void
3636
$this->resolver = new OptionsResolver();
3737
}
3838

39+
/**
40+
* @dataProvider provideResolveWithIgnoreUndefined
41+
*/
42+
public function testResolveWithIgnoreUndefined(array $defaults, array $options, array $expected)
43+
{
44+
$this->resolver
45+
->setDefaults($defaults)
46+
->setIgnoreUndefined();
47+
48+
$this->assertSame($expected, $this->resolver->resolve($options));
49+
}
50+
51+
public static function provideResolveWithIgnoreUndefined(): array
52+
{
53+
return [
54+
'no defaults options, undefined resolves empty' => [[], ['c' => 4, 'd' => 5], []],
55+
'empty options resolves defaults' => [['a' => '1', 'b' => '2'], [], ['a' => '1', 'b' => '2']],
56+
'undefined options resolves defaults' => [['a' => '1', 'b' => '2'], ['c' => 3, 'd' => 4], ['a' => '1', 'b' => '2']],
57+
'resolves defined' => [['a' => '1', 'b' => '2'], ['a' => '10', 'c' => '3'], ['b' => '2', 'a' => '10']],
58+
];
59+
}
60+
3961
public function testResolveFailsIfNonExistingOption()
4062
{
4163
$this->expectException(UndefinedOptionsException::class);

0 commit comments

Comments
 (0)