8000 [Config] Remap XML before normalization by ro0NL · Pull Request #21052 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] Remap XML before normalization #21052

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 2 commits 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
10 changes: 6 additions & 4 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,14 @@ public function setNormalizeKeys($normalizeKeys)
*/
protected function preNormalize($value)
{
if (!$this->normalizeKeys || !is_array($value)) {
if (!is_array($value)) {
return $value;
}

if (!$this->normalizeKeys) {
return $this->remapXml($value);
}

$normalized = array();

foreach ($value as $k => $v) {
Expand All @@ -66,7 +70,7 @@ protected function preNormalize($value)
}
}

return $normalized;
8000 return $this->remapXml($normalized);
}

/**
Expand Down Expand Up @@ -299,8 +303,6 @@ protected function normalizeValue($value)
return $value;
}

$value = $this->remapXml($value);

$normalized = array();
foreach ($value as $name => $val) {
if (isset($this->children[$name])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ protected function normalizeValue($value)
return $value;
}

$value = $this->remapXml($value);

$isAssoc = array_keys($value) !== range(0, count($value) - 1);
$normalized = array();
foreach ($value as $k => $v) {
Expand Down
33 changes: 30 additions & 3 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\PrototypedArrayNode;
use Symfony\Component\Config\Definition\ScalarNode;

class ArrayNodeTest extends \PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -68,11 +69,33 @@ public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $messa
public function testPreNormalize($denormalized, $normalized)
{
$node = new ArrayNode('foo');
$node->addChild(new ScalarNode('foo_bar'));
$node->addChild(new ScalarNode('foo-bar'));
$node->addChild(new ScalarNode('foo-bar_moo'));
$node->addChild(new ScalarNode('anything_with_dash_and_no_underscore'));
$node->addChild(new ScalarNode('no_dash'));

$r = new \Reflection 9DB8 Method($node, 'preNormalize');
$r->setAccessible(true);
$prototypeNode = new ArrayNode('subfoo');
$prototypeNode->addChild(new ScalarNode('bar'));
$prototype = new PrototypedArrayNode('plural');
$prototype->setPrototype($prototypeNode);
$prototype->setKeyAttribute('x');

$node->setXmlRemappings(array(
array('singular', 'plural'),
));
$node->addChild($prototype);

$node->setNormalizationClosures(array(
function ($v) use ($normalized) {
$this->assertSame($normalized, $v);

return $v;
},
));

$this->assertSame($normalized, $node->normalize($denormalized));

$this->assertSame($normalized, $r->invoke($node, $denormalized));
}

public function getPreNormalizationTests()
Expand All @@ -94,6 +117,10 @@ public function getPreNormalizationTests()
array('foo-bar' => null, 'foo_bar' => 'foo'),
array('foo-bar' => null, 'foo_bar' => 'foo'),
),
array(
array('singular' => array(array('x' => 'foo', 'bar' => 'foo'), array('x' => 'bar', 'bar' => 'bar'))),
array('plural' => array('foo' => array('bar' => 'foo'), 'bar' => array('bar' => 'bar'))),
)
);
}

Expand Down
0