ZF-5800: toArray buggy when used with lots + 1

Description

I'm working with a XML config file like this one:


user1@company.comUser Name

using this code:


<?$cc = $config->mailler->cc->receiver->toArray()?>

$cc is not really what I expected it to be:

array
  'receiver' => 
    array
      'mail' => string 'user1@company.com' (length=17)
      'name' => string 'User Name' (length=9)
      0 => 
        array
          'mail' => string 'nice.guy@company.com' (length=20)
          'name' => string 'Nice Guy' (length=8)
      1 => 
        array
          'mail' => string 'fred@company.com' (length=16)

I though I would get something like that:

array
  'receiver' => 
    array
      0 =>
        array
          'mail' => string 'user1@company.com' (length=17)
          'name' => string 'User Name' (length=9)
      1 => 
        array
          'mail' => string 'nice.guy@company.com' (length=20)
          'name' => string 'Nice Guy' (length=8)
      2 => 
        array
          'mail' => string 'fred@company.com' (length=16)

When I think of this, I can't see why it doesn't work as one can expect it.

might it be a bug?

Comments

In fact that looks like a bug in the _processExtends() method.

I think that this is expected behaviour as per the current parsing rules.

In you are specifying as an object with two properties. You then create an array of s in , but as $receiver->mail already exists, the best it can do is create an array for you as it doesn't convert the previously created receiver object into an array.

Now, we could change it so that it does convert the in into an array before merging the in . If we did that though, then as extends , the resultant array would be:


array
  'receiver' => 
    array
      0 => 
        array
          'mail' => string 'nice.guy@company.com' (length=20)
          'name' => string 'Nice Guy' (length=8)
      1 => 
        array
          'mail' => string 'fred@company.com' (length=16)

as would completely override . Your expected result would never happen as that's a merge, not an override.

Thoughts?

If we choose to fix it, this is the patch :


Index: tests/Zend/Config/XmlTest.php
===================================================================
--- tests/Zend/Config/XmlTest.php   (revision 14409)
+++ tests/Zend/Config/XmlTest.php   (working copy)
@@ -271,4 +271,31 @@
         $this->assertEquals('staging', $config->hostname);
 
     }
+
+    /*
+     * @group ZF-5800
+     * 
+     */
+    public function testArraysOfKeysCreatedUsingAttributesAndKeys()
+    {
+        $string = <<
+
+
+        
+                user1@company.com
+                User Name
+        
+
+
+        
+        
+
+
+EOT;
+
+        $config = new Zend_Config_Xml($string, 'dev');
+        $this->assertEquals('nice.guy@company.com', $config->receiver->{0}->mail);
+        $this->assertNull($config->receiver->mail);
+    }
 }
Index: library/Zend/Config/Xml.php
===================================================================
--- library/Zend/Config/Xml.php (revision 14409)
+++ library/Zend/Config/Xml.php (working copy)
@@ -222,7 +222,11 @@
                 if (isset($firstArray[$key])) {
                     $firstArray[$key] = $this->_arrayMergeRecursive($firstArray[$key], $value);
                 } else {
-                    $firstArray[$key] = $value;
+                    if($key === 0) {
+                        $firstArray= array(0=>$this->_arrayMergeRecursive($firstArray, $value));
+                    } else {
+                        $firstArray[$key] = $value;
+                    }
                 }
             }
         } else {

in fact, thinking about it, <?$cc = $config->mailler->cc->toArray()?> might have the behaviour I wanted, according to that other non-bug (ZF-5973) I reported.

Try it, but I suspect that you'll have the same issue.

Fixed in SVN r14665 (for XML) and r14667 (for INI).