joomla/joomla-cms — pull request #3836 by phproberto (2014-06-28 06:32:11+00:00)
Title: [imp] JArrayHelper::toObject allow non-recursive conversion

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:

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

Current conversion is used like:

```php
$converted = JArrayHelper::toObject($array, 'JObject');
echo '

';
print_r($converted);
echo '

';
```

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:

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

echo '

';
print_r($convertedNonRecursive);
echo '

';
```

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:

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

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

echo '

';
print_r($converted);
print_r($convertedNonRecursive);
echo '

';
```

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