Issues

ZF-2437: Zend_Config_Xml Does not properly support nested items

Issue Type: Bug Created: 2008-01-15T09:14:20.000+0000 Last Updated: 2008-02-26T12:56:00.000+0000 Status: Resolved Fix version(s): - 1.0.4 (26/Feb/08)

Reporter: Jon Whitcraft (sidhighwind) Assignee: Rob Allen (rob) Tags: - Zend_Config

Related issues: - ZF-2285

Attachments: - patch.txt

Description

This is an example config file that i have.

<pre class="highlight">
<?xml version="1.0" encoding="UTF-8"?>
News/news/Schedule/schedule/

When i run it though the the Zend_Config_Xml it doesnt return it right. I have narrowed the problem down to the _toArray() function.

it returns an array like this:

<pre class="highlight">
Array
(
    [default] => Array
        (
            [navigation] => Array
                (
                    [nav_item] => Array
                        (
                            [nav_text] => News
                            [nav_link] => /news/
                            [0] => Array
                                (
                                    [nav_text] => Schedule
                                    [nav_link] => /schedule/
                                )
                        )
                )
        )
)

which is not correct as it should be like this one.

<pre class="highlight">
Array
(
    [default] => Array
        (
            [navigation] => Array
                (
                    [nav_item] => Array
                        (
                            [0] => Array
                                (
                                    [nav_text] => News
                                    [nav_link] => /news/
                                )
                            [1] => Array
                                (
                                    [nav_text] => Schedule
                                    [nav_link] => /schedule/
                                )
                        )
                )
        )
)

Comments

Posted by Inash Zubair (inash) on 2008-01-30T16:18:46.000+0000

Hi Jon.

Could you see if the below patch works for revision 7191 of Zend_Config_Xml in head. You might need to change the file paths in the attached patch file.

Posted by Rob Allen (rob) on 2008-01-31T02:20:07.000+0000

Unfortunately, your patch breaks for this case:

<pre class="highlight">
<?xml version="1.0"?>
123

as you end up with a $config->toArray() of this:

<pre class="highlight">
array(1) {
  ["six"] => array(1) {
    ["seven"] => array(2) {
      [0] => array(2) {
        [0] => array(1) {
          ["eight"] => string(1) "1"
        }
        [1] => array(1) {
          ["eight"] => string(1) "2"
        }
      }
      [1] => array(1) {
        ["eight"] => string(1) "3"
      }
    }
  }
}

I've fixed appropriately and committed to trunk in svn r7724.

Thanks for the heads up.

Posted by Jon Whitcraft (sidhighwind) on 2008-01-31T07:00:42.000+0000

Guys. I'll test it tomorrow as i dont have time today.

Thanks providing a patch! I'll let you know.

Posted by Inash Zubair (inash) on 2008-01-31T08:06:16.000+0000

Hi Rob. Your patch breaks the previous example of Jon. I've managed a patch that work for both the examples. We also need to create more test case scenarios for this. Hope it works this time. Cheers.

Posted by Rob Allen (rob) on 2008-01-31T09:01:38.000+0000

Inash,

You are right - my new unit test wasn't a true copy of Jon's scenario :(

I've updated the unit test and fixed it using

<pre class="highlight">
if (array_key_exists($key, $config)) {
    if (!is_array($config[$key]) || !array_key_exists(0, $config[$key])) {
        $config[$key] = array($config[$key]);
    }
    $config[$key][] = $value;
} else {
    $config[$key] = $value;
}

So fixed in svn r7727 hopefully!

Posted by Jon Whitcraft (sidhighwind) on 2008-01-31T09:06:10.000+0000

ok i've freed up some time to test so i'll get this once i finish with another bug.

Posted by Jon Whitcraft (sidhighwind) on 2008-01-31T09:19:17.000+0000

Hey guys it works great! Bug has been fixed!

Posted by Rob Allen (rob) on 2008-01-31T09:26:50.000+0000

Note that this breaks BC with 1.0.3's unit tests, so may have to be re-thought.

Posted by Rob Allen (rob) on 2008-01-31T09:33:24.000+0000

For reference:

Given this XML file:

<pre class="highlight">
<?xml version="1.0"?>
2a2b54

1.0.3 will produce this $config->toArray():

<pre class="highlight">
array(2) {
  ["one"] => array(1) {
    ["two"] => array(2) {
      [0] => string(2) "2a"
      [1] => string(2) "2b"
    }
  }
  ["three"] => array(1) {
    ["four"] => array(2) {
      ["five"] => string(1) "5"
      [0] => string(1) "4"
    }
  }
}

r 7727 of trunk will give:

<pre class="highlight">
array(2) {
  ["one"] => array(1) {
    ["two"] => array(2) {
      [0] => string(2) "2a"
      [1] => string(2) "2b"
    }
  }
  ["three"] => array(1) {
    ["four"] => array(2) {
      [0] => array(1) {
        ["five"] => string(1) "5"
      }
      [1] => string(1) "4"
    }
  }
}

Posted by Inash Zubair (inash) on 2008-01-31T09:42:05.000+0000

Like you said, it does break that. I've checked the tests as well. But I think now it's working properly like it should. I've linked a similar issue to this which you've commented as well. I could now access the fields in your reference as mentioned in that issue.

If this should break the 1.0 branch, the fix could wait until 1.1!? What do you think!?

Posted by Darby Felton (darby) on 2008-01-31T09:45:59.000+0000

My opinion is that the former behavior (i.e., from 1.0.3) was broken, and that r7727 fixes the broken behavior. Based on this, the BC issue appears to be that people depending on the broken behavior should simply update to not depend on the broken and (IIRC) undocumented behavior.

Posted by Darby Felton (darby) on 2008-01-31T09:47:02.000+0000

Sorry, I also meant to say that I think the fix should be in 1.0.4 (merged to release-1.0 branch), too. :)

Posted by Inash Zubair (inash) on 2008-01-31T09:54:54.000+0000

I agree too.

Posted by Jon Whitcraft (sidhighwind) on 2008-01-31T10:00:05.000+0000

I also agree!

Posted by Rob Allen (rob) on 2008-01-31T10:04:38.000+0000

Fixed on release-1.0 branch in svn r7732.

Thanks very much for the help guys!

Posted by Jon Whitcraft (sidhighwind) on 2008-01-31T10:08:02.000+0000

Not a problem. Glad to be helping!

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