Skip to content

Commit

Permalink
bug #29152 [Config] Unset key during normalization (ro0NL)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 2.8 branch (closes #29152).

Discussion
----------

[Config] Unset key during normalization

| Q             | A
| ------------- | ---
| Branch?       | 2.8
| Bug fix?      | yes-ish
| New feature?  | yes
| BC breaks?    | no     <!-- see https://symfony.com/bc -->
| Deprecations? | no
| Tests pass?   | yes    <!-- please add some, will be required by reviewers -->
| Fixed tickets | #29142
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

2.8 vs. 4.x :) let me know.

Commits
-------

e1402d4 [Config] Unset key during normalization
  • Loading branch information
fabpot committed Nov 12, 2018
2 parents 131a42b + e1402d4 commit 26f321c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,10 @@ protected function normalizeValue($value)
$normalized = array();
foreach ($value as $name => $val) {
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]);
} elseif (!$this->removeExtraKeys) {
$normalized[$name] = $val;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function thenEmptyArray()
}

/**
* 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.
*
Expand All @@ -167,7 +167,7 @@ public function thenInvalid($message)
}

/**
* 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
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,25 @@ public function testNormalizeKeys()
$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()
{
return array(
Expand Down

0 comments on commit 26f321c

Please sign in to comment.