bug #39764 [Config]  fix handling float-like key attribute values (xabbuh)

This PR was merged into the 4.4 branch.

Discussion
----------

[Config]  fix handling float-like key attribute values

| Q             | A
| ------------- | ---
| Branch?       | 4.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #36001
| License       | MIT
| Doc PR        |

Commits
-------

a2ad4fa8b3 fix handling float-like key attribute values
This commit is contained in:
Nicolas Grekas 2021-01-12 12:28:52 +01:00
commit 0211b9afe8
2 changed files with 30 additions and 0 deletions

View File

@ -237,6 +237,10 @@ class PrototypedArrayNode extends ArrayNode
} elseif (isset($v[$this->keyAttribute])) {
$k = $v[$this->keyAttribute];
if (\is_float($k)) {
$k = var_export($k, true);
}
// remove the key attribute when required
if ($this->removeKeyAttribute) {
unset($v[$this->keyAttribute]);

View File

@ -201,6 +201,32 @@ class NormalizationTest extends TestCase
$this->assertNormalized($tree, $data, $data);
}
public function testFloatLikeValueAsMapKeyAttribute()
{
$tree = (new TreeBuilder('root'))
->getRootNode()
->useAttributeAsKey('number')
->arrayPrototype()
->children()
->scalarNode('foo')->end()
->end()
->end()
->end()
->buildTree()
;
$this->assertNormalized($tree, [
[
'number' => 3.0,
'foo' => 'bar',
],
], [
'3.0' => [
'foo' => 'bar',
],
]);
}
public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
{
self::assertSame($normalized, $tree->normalize($denormalized));