[Config] Numerical keys for prot. arrays if useAttributeAsKey is set #4276

Merged
merged 1 commit into from May 22, 2012

Conversation

Projects
None yet
4 participants
@asm89
Contributor

asm89 commented May 14, 2012

Bug fix: not sure
Feature addition: not sure
Backwards compatibility break: not sure
Symfony2 tests pass: Build Status
License of the code: MIT

When using an array node with children of prototype array and useAttributeAsKey, using numerical values for the keys throws an exception. For example (useAttributeAsKey('id')):

<?php
// works
array (
    'thing' => array(
        array('foo', 'bar', 'id' => 'a')
    )
);

// works and is the same as above
array (
    'thing' => array(
        'a' => array('foo', 'bar')
    )
);

// works
array(
    'thing' => array(
        array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
    ),  
);

// works with this patch and is the same as above
array(
    'thing' => array(
        42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
    ),  
);
@travisbot

This comment has been minimized.

Show comment Hide comment
@travisbot

travisbot May 14, 2012

This pull request passes (merged 42d252da into 46ffbd5).

This pull request passes (merged 42d252da into 46ffbd5).

@travisbot

This comment has been minimized.

Show comment Hide comment
@travisbot

travisbot May 14, 2012

This pull request passes (merged b7fc009 into 46ffbd5).

This pull request passes (merged b7fc009 into 46ffbd5).

@vicb

This comment has been minimized.

Show comment Hide comment
@vicb

vicb May 21, 2012

Contributor

Could this be fixed by changing PrototypedArrayNode
$k = $v[$this->keyAttribute]; -> $k = (string) $v[$this->keyAttribute];

Contributor

vicb commented May 21, 2012

Could this be fixed by changing PrototypedArrayNode
$k = $v[$this->keyAttribute]; -> $k = (string) $v[$this->keyAttribute];

@asm89

This comment has been minimized.

Show comment Hide comment
@asm89

asm89 May 22, 2012

Contributor

I tried it and the test I added still fails. The code change you propose doesn't execute because if (!isset($v[$this->keyAttribute]) && is_int($k)) will still evaluate to true.

Contributor

asm89 commented May 22, 2012

I tried it and the test I added still fails. The code change you propose doesn't execute because if (!isset($v[$this->keyAttribute]) && is_int($k)) will still evaluate to true.

fabpot added a commit that referenced this pull request May 22, 2012

merged branch asm89/numeric-keys-config (PR #4276)
Commits
-------

b7fc009 [Config] Numerical keys for prot. arrays if useAttributeAsKey is set

Discussion
----------

[Config] Numerical keys for prot. arrays if useAttributeAsKey is set

Bug fix: not sure
Feature addition: not sure
Backwards compatibility break: not sure
Symfony2 tests pass: [![Build Status](https://secure.travis-ci.org/asm89/symfony.png?branch=numeric-keys-config)](http://travis-ci.org/asm89/symfony)
License of the code: MIT

When using an array node with children of prototype array and `useAttributeAsKey`, using numerical values for the keys throws an exception. For example (`useAttributeAsKey('id')`):

``` php
<?php
// works
array (
    'thing' => array(
        array('foo', 'bar', 'id' => 'a')
    )
);

// works and is the same as above
array (
    'thing' => array(
        'a' => array('foo', 'bar')
    )
);

// works
array(
    'thing' => array(
        array('foo', 'bar', 'id' => 42), array('baz', 'qux', 'id' => 1337),
    ),
);

// works with this patch and is the same as above
array(
    'thing' => array(
        42 => array('foo', 'bar'), 1337 => array('baz', 'qux'),
    ),
);
```

---------------------------------------------------------------------------

by travisbot at 2012-05-14T14:26:32Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1327383) (merged 42d252da into 46ffbd5).

---------------------------------------------------------------------------

by travisbot at 2012-05-14T14:32:59Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1327430) (merged b7fc009 into 46ffbd5).

---------------------------------------------------------------------------

by vicb at 2012-05-21T15:16:24Z

Could this be fixed by changing [PrototypedArrayNode](https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Config/Definition/PrototypedArrayNode.php#L255)
`$k = $v[$this->keyAttribute];` -> `$k = (string) $v[$this->keyAttribute];`

---------------------------------------------------------------------------

by asm89 at 2012-05-22T07:01:53Z

I tried it and the test I added still fails. The code change you propose doesn't execute because `if (!isset($v[$this->keyAttribute]) && is_int($k))` will still evaluate to true.

@fabpot fabpot merged commit b7fc009 into symfony:master May 22, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment