[Config] Unset key during normalization

This commit is contained in:
Roland Franssen 2018-11-09 11:22:20 +01:00 committed by Fabien Potencier
parent 46e3745a03
commit e1402d495e
3 changed files with 25 additions and 3 deletions

View File

@ -288,7 +288,10 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
$normalized = array(); $normalized = array();
foreach ($value as $name => $val) { foreach ($value as $name => $val) {
if (isset($this->children[$name])) { if (isset($this->children[$name])) {
$normalized[$name] = $this->children[$name]->normalize($val); try {
$normalized[$name] = $this->children[$name]->normalize($val);
} catch (UnsetKeyException $e) {
}
unset($value[$name]); unset($value[$name]);
} elseif (!$this->removeExtraKeys) { } elseif (!$this->removeExtraKeys) {
$normalized[$name] = $val; $normalized[$name] = $val;

View File

@ -149,7 +149,7 @@ class ExprBuilder
} }
/** /**
* Sets a closure marking the value as invalid at validation time. * Sets a closure marking the value as invalid at processing time.
* *
* if you want to add the value of the node in your message just use a %s placeholder. * if you want to add the value of the node in your message just use a %s placeholder.
* *
@ -167,7 +167,7 @@ class ExprBuilder
} }
/** /**
* Sets a closure unsetting this key of the array at validation time. * Sets a closure unsetting this key of the array at processing time.
* *
* @return $this * @return $this
* *

View File

@ -231,6 +231,25 @@ class ArrayNodeDefinitionTest extends TestCase
$this->assertFalse($this->getField($node, 'normalizeKeys')); $this->assertFalse($this->getField($node, 'normalizeKeys'));
} }
public function testUnsetChild()
{
$node = new ArrayNodeDefinition('root');
$node
->children()
->scalarNode('value')
->beforeNormalization()
->ifTrue(function ($value) {
return empty($value);
})
->thenUnset()
->end()
->end()
->end()
;
$this->assertSame(array(), $node->getNode()->normalize(array('value' => null)));
}
public function getEnableableNodeFixtures() public function getEnableableNodeFixtures()
{ {
return array( return array(