Skip to content

[config] added remove option to ignoreExtraKeys #14238

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
8 changes: 7 additions & 1 deletion src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
protected $addIfNotSet = false;
protected $performDeepMerging = true;
protected $ignoreExtraKeys = false;
protected $removeExtraKeys = true;
protected $normalizeKeys = true;

public function setNormalizeKeys($normalizeKeys)
Expand Down Expand Up @@ -140,10 +141,12 @@ public function setPerformDeepMerging($boolean)
* Whether extra keys should just be ignore without an exception.
*
* @param bool $boolean To allow extra keys
* @param bool $remove To remove extra keys
*/
public function setIgnoreExtraKeys($boolean)
public function setIgnoreExtraKeys($boolean, $remove = true)
{
$this->ignoreExtraKeys = (bool) $boolean;
$this->removeExtraKeys = $this->ignoreExtraKeys && $remove;
}

/**
Expand Down Expand Up @@ -300,6 +303,9 @@ protected function normalizeValue($value)
if (isset($this->children[$name])) {
$normalized[$name] = $this->children[$name]->normalize($val);
unset($value[$name]);
} elseif (false === $this->removeExtraKeys) {
$normalized[$name] = $val;
unset($value[$name]);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class ArrayNodeDefinition extends NodeDefinition implements ParentNodeDefinition
{
protected $performDeepMerging = true;
protected $ignoreExtraKeys = false;
protected $removeExtraKeys = true;
protected $children = array();
protected $prototype;
protected $atLeastOne = false;
Expand Down Expand Up @@ -284,11 +285,14 @@ public function performNoDeepMerging()
* you want to send an entire configuration array through a special
* tree that processes only part of the array.
*
* @param bool $remove Whether to remove the extra keys
*
* @return ArrayNodeDefinition
*/
public function ignoreExtraKeys()
public function ignoreExtraKeys($remove = true)
{
$this->ignoreExtraKeys = true;
$this->removeExtraKeys = $remove;

return $this;
}
Expand Down Expand Up @@ -393,7 +397,7 @@ protected function createNode()
$node->addEquivalentValue(false, $this->falseEquivalent);
$node->setPerformDeepMerging($this->performDeepMerging);
$node->setRequired($this->required);
$node->setIgnoreExtraKeys($this->ignoreExtraKeys);
$node->setIgnoreExtraKeys($this->ignoreExtraKeys, $this->removeExtraKeys);
$node->setNormalizeKeys($this->normalizeKeys);

if (null !== $this->normalization) {
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ public function testIgnoreExtraKeysNoException()
$this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
}

/**
* Tests that extra keys are not removed when
* ignoreExtraKeys second option is set to false.
*
* Related to testExceptionThrownOnUnrecognizedChild
*/
public function testIgnoreExtraKeysNotRemoved()
{
$node = new ArrayNode('roo');
$node->setIgnoreExtraKeys(true, false);

$data = array('foo' => 'bar');
$this->assertSame($data, $node->normalize($data));
}

/**
* @dataProvider getPreNormalizationTests
*/
Expand Down