mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 15:06:06 +00:00
fix util array to object to preserve lists
This commit is contained in:
@@ -305,8 +305,17 @@ class Util
|
||||
*/
|
||||
private static function arrayToObjectInternal($value)
|
||||
{
|
||||
if (is_array($value)) {
|
||||
return (object) array_map(fn($v) => self::arrayToObjectInternal($v), $value);
|
||||
if (!is_array($value)) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// @todo Change to `array_is_list` when PHP 8.1 is the min supported.
|
||||
$isList = $value === array_values($value);
|
||||
|
||||
$value = array_map(fn($v) => self::arrayToObjectInternal($v), $value);
|
||||
|
||||
if (!$isList) {
|
||||
$value = (object) $value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
||||
@@ -1072,27 +1072,35 @@ class UtilTest extends \PHPUnit\Framework\TestCase
|
||||
$this->assertEquals($result, Util::concatPath($input));
|
||||
}
|
||||
|
||||
public function testArrayToObject()
|
||||
public function testArrayToObject(): void
|
||||
{
|
||||
$testArr= array(
|
||||
$testArr= [
|
||||
'useCache' => true,
|
||||
'sub' => array (
|
||||
'sub' => [
|
||||
'subV' => '125',
|
||||
'subO' => array(
|
||||
'subO' => [
|
||||
'subOV' => '125',
|
||||
),
|
||||
),
|
||||
);
|
||||
],
|
||||
'subList' => [
|
||||
'0',
|
||||
'1'
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$testResult= (object) array(
|
||||
$testResult= (object) [
|
||||
'useCache' => true,
|
||||
);
|
||||
$testResult->sub = (object) array (
|
||||
'subV' => '125',
|
||||
);
|
||||
$testResult->sub->subO = (object) array (
|
||||
'subOV' => '125',
|
||||
);
|
||||
];
|
||||
|
||||
$testResult->sub = (object) [
|
||||
'subV' => '125',
|
||||
];
|
||||
|
||||
$testResult->sub->subO = (object) [
|
||||
'subOV' => '125',
|
||||
];
|
||||
|
||||
$testResult->sub->subList = ['0', '1'];
|
||||
|
||||
$this->assertEquals($testResult, Util::arrayToObject($testArr));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user