Skip to content

Commit

Permalink
[Config] Fixed preserving keys in associative arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Sep 24, 2012
1 parent 237629a commit c812b9d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Expand Up @@ -242,11 +242,11 @@ protected function normalizeValue($value)

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

$isAssoc = array_keys($value) === range(0, count($value) -1);
$isAssoc = array_keys($value) !== range(0, count($value) -1);
$normalized = array();
foreach ($value as $k => $v) {
if (null !== $this->keyAttribute && is_array($v)) {
if (!isset($v[$this->keyAttribute]) && is_int($k) && $isAssoc) {
if (!isset($v[$this->keyAttribute]) && is_int($k) && !$isAssoc) {
$msg = sprintf('The attribute "%s" must be set for path "%s".', $this->keyAttribute, $this->getPath());
$ex = new InvalidConfigurationException($msg);
$ex->setPath($this->getPath());
Expand Down Expand Up @@ -276,7 +276,7 @@ protected function normalizeValue($value)
}

$this->prototype->setName($k);
if (null !== $this->keyAttribute) {
if (null !== $this->keyAttribute || $isAssoc) {
$normalized[$k] = $this->prototype->normalize($v);
} else {
$normalized[] = $this->prototype->normalize($v);
Expand Down
Expand Up @@ -183,6 +183,25 @@ public function testNonAssociativeArrayThrowsExceptionIfAttributeNotSet()
$this->assertNormalized($this->getNumericKeysTestTree(), $denormalized, array());
}

public function testAssociativeArrayPreserveKeys()
{
$tb = new TreeBuilder();
$tree = $tb
->root('root', 'array')
->prototype('array')
->children()
->node('foo', 'scalar')->end()
->end()
->end()
->end()
->buildTree()
;

$data = array('first' => array('foo' => 'bar'));

$this->assertNormalized($tree, $data, $data);
}

public static function assertNormalized(NodeInterface $tree, $denormalized, $normalized)
{
self::assertSame($normalized, $tree->normalize($denormalized));
Expand Down

0 comments on commit c812b9d

Please sign in to comment.