Issues

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

Issue Type: Bug Created: 2009-02-16T08:12:11.000+0000 Last Updated: 2009-04-05T02:19:51.000+0000 Status: Resolved Fix version(s): - 1.8.0 (30/Apr/09)

Reporter: Brice Laurencin (brisssou) Assignee: Rob Allen (rob) Tags: - Zend_Config

Related issues: Attachments:

Description

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

<pre class="highlight">
user1@company.comUser Name

using this code:

<pre class="highlight">
<?$cc = $config->mailler->cc->receiver->toArray()?>

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

<pre class="literal">
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:

<pre class="literal">
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

Posted by Ben Scholzen (dasprid) on 2009-02-19T01:03:56.000+0000

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

Posted by Rob Allen (rob) on 2009-03-21T11:07:43.000+0000

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:

<pre class="highlight">
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?

Posted by Rob Allen (rob) on 2009-03-21T11:22:09.000+0000

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

<pre class="highlight">
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 {

Posted by Brice Laurencin (brisssou) on 2009-03-29T09:10:06.000+0000

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

Posted by Rob Allen (rob) on 2009-03-29T09:15:07.000+0000

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

Posted by Rob Allen (rob) on 2009-04-05T02:19:51.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts