Skip to content

Commit

Permalink
Merge pull request #12297 from civicrm/5.3
Browse files Browse the repository at this point in the history
5.3
  • Loading branch information
eileenmcnaughton authored Jun 11, 2018
2 parents 0005b9a + 9eee39f commit 165bc6f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
16 changes: 14 additions & 2 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -1205,12 +1205,24 @@ public static function recursiveValue($array, $path, $default = NULL) {
*
* @param $path
* @param $value
* @param array $source
*
* @return array
*/
public static function recursiveBuild($path, $value) {
public static function recursiveBuild($path, $value, $source = []) {
$arrayKey = array_shift($path);
return [$arrayKey => (empty($path) ? $value : self::recursiveBuild($path, $value))];
// Recurse through array keys
if ($path) {
if (!isset($source[$arrayKey])) {
$source[$arrayKey] = [];
}
$source[$arrayKey] = self::recursiveBuild($path, $value, $source[$arrayKey]);
}
// Final iteration
else {
$source[$arrayKey] = $value;
}
return $source;
}

}
14 changes: 10 additions & 4 deletions tests/phpunit/CRM/Utils/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,14 @@ public function testRecursiveValue($array, $path, $default, $expected) {
public function getBuildValueExamples() {
return [
[
[0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]]
]
[], [0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]],
],
[
['foo', 'bar', [['donkey']]], [2, 0, 1], ['foo', 'bar', [['donkey', 'llama']]],
],
[
['a' => [1, 2, 3], 'b' => ['x' => [], 'y' => ['a' => 'donkey', 'b' => 'bear'], 'z' => [4, 5, 6]]], ['b', 'y', 'b'], ['a' => [1, 2, 3], 'b' => ['x' => [], 'y' => ['a' => 'donkey', 'b' => 'llama'], 'z' => [4, 5, 6]]],
],
];
}

Expand All @@ -317,8 +323,8 @@ public function getBuildValueExamples() {
*
* @dataProvider getBuildValueExamples
*/
public function testBuildRecursiveValue($path, $expected) {
$result = CRM_Utils_Array::recursiveBuild($path, 'llama');
public function testBuildRecursiveValue($source, $path, $expected) {
$result = CRM_Utils_Array::recursiveBuild($path, 'llama', $source);
$this->assertEquals($expected, $result);
}

Expand Down

0 comments on commit 165bc6f

Please sign in to comment.