User tests: Successful: Unsuccessful:
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
)
)
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.
Also tested good here good one @phproberto I will moving the Tracker to RTC
Merged into 3.4-dev.
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2014-06-30 18:31:14 |
Title |
|
Simple change and good feature.
Tested and couldn't break anything as expected. Thanks Roberto!