? Success

User tests: Successful: Unsuccessful:

avatar phproberto
phproberto
28 Jun 2014

Tracker item:
http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=33902&start=0

The default mode of the function JArrayHelper::toObject is to recursively convert any subarray in another object of the same specified class. I would say this makes no sense in 99% of the times.

This PR adds a way to use the same function but without subarray conversion.

Let's say that you have an array like:

$array = array(
    'prop1' => 'value1',
    'prop2' => 'value2',
    'prop3' => array(
        'subprop1' => 'value1',
        'subprop2' => 'value2'
    )
);

Current conversion is used like:

$converted = JArrayHelper::toObject($array, 'JObject');
echo '<pre>';
print_r($converted);
echo '</pre>';

Result:

JObject Object
(
    [_errors:protected] => Array
        (
        )

    [prop1] => value1
    [prop2] => value2
    [prop3] => JObject Object
        (
            [_errors:protected] => Array
                (
                )

            [subprop1] => value1
            [subprop2] => value2
        )

)

As you see prop3 is converted to JObject too. That's most of the times a non-desired behavior but now it's forced.

I added a new parameter to the function with default = true to keep BC but allowing us to convert only the main array to the desired object keeping the rest children subarrays with their original values. So a call like:

$convertedNonRecursive = JArrayHelper::toObject($array, 'JObject', false);

echo '<pre>';
print_r($convertedNonRecursive);
echo '</pre>';

Will show:

JObject Object
(
    [_errors:protected] => Array
        (
        )

    [prop1] => value1
    [prop2] => value2
    [prop3] => Array
        (
            [subprop1] => value1
            [subprop2] => value2
        )

)

Test instructions

JArrayHelper::toObject is mainly used in the models getItem() function so after applying the patch ensure that all is working. You can also use this sample code:

$array = array(
    'prop1' => 'value1',
    'prop2' => 'value2',
    'prop3' => array(
        'subprop1' => 'value1',
        'subprop2' => 'value2'
    )
);

$converted = JArrayHelper::toObject($array, 'JObject');
$convertedNonRecursive = JArrayHelper::toObject($array, 'JObject', false);

echo '<pre>';
print_r($converted);
print_r($convertedNonRecursive);
echo '</pre>';

That will show that the function is stil lBC and the new feature.

avatar phproberto phproberto - open - 28 Jun 2014
avatar Bakual
Bakual - comment - 28 Jun 2014

Simple change and good feature. :+1:
Tested and couldn't break anything as expected. Thanks Roberto!

avatar zero-24
zero-24 - comment - 28 Jun 2014

Also tested good here :smile: good one @phproberto I will moving the Tracker to RTC

avatar phproberto phproberto - reference | - 30 Jun 14
avatar Bakual
Bakual - comment - 30 Jun 2014

Merged into 3.4-dev.

avatar Bakual Bakual - change - 30 Jun 2014
Status New Closed
Closed_Date 0000-00-00 00:00:00 2014-06-30 18:31:14
avatar Bakual Bakual - change - 30 Jun 2014
Title
[imp] JArrayHelper::toObject allow non-recursive conversion
[#33902] [imp] JArrayHelper::toObject allow non-recursive conversion
avatar Bakual Bakual - close - 30 Jun 2014
avatar Bakual Bakual - close - 30 Jun 2014

Add a Comment

Login with GitHub to post a comment