Skip to content

Commit

Permalink
Backport Of PR civicrm#12187 And PR civicrm#12192
Browse files Browse the repository at this point in the history
  • Loading branch information
vinuvarshith committed Jul 9, 2018
1 parent 8eb0f3c commit 1cd4175
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
61 changes: 61 additions & 0 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -1011,4 +1011,65 @@ public static function convertCheckboxFormatToArray($input) {
return $input;
}

/**
* Check if a key isset which may be several layers deep.
*
* This is a helper for when the calling function does not know how many layers deep the
* path array is so cannot easily check.
*
* @param array $array
* @param array $path
* @return bool
* @throws \CRM_Core_Exception
*/
public static function recursiveIsset($array, $path) {
foreach ($path as $key) {
if (!is_array($array) || !isset($array[$key])) {
return FALSE;
}
$array = $array[$key];
}
return TRUE;
}

/**
* Check if a key isset which may be several layers deep.
*
* This is a helper for when the calling function does not know how many layers deep the
* path array is so cannot easily check.
*
* @param array $array
* @param array $path
* An array of keys - e.g [0, 'bob', 8] where we want to check if $array[0]['bob'][8]
* @param mixed $default
* Value to return if not found.
* @return bool
* @throws \CRM_Core_Exception
*/
public static function recursiveValue($array, $path, $default = NULL) {
foreach ($path as $key) {
if (!is_array($array) || !isset($array[$key])) {
return $default;
}
$array = $array[$key];
}
return $array;
}

/**
* Append the value to the array using the key provided.
*
* e.g if value is 'llama' & path is [0, 'email', 'location'] result will be
* [0 => ['email' => ['location' => 'llama']]
*
* @param $path
* @param $value
*
* @return array
*/
public static function recursiveBuild($path, $value) {
$arrayKey = array_shift($path);
return [$arrayKey => (empty($path) ? $value : self::recursiveBuild($path, $value))];
}

}
92 changes: 92 additions & 0 deletions tests/phpunit/CRM/Utils/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,96 @@ public function testGetSetPathParts() {
$this->assertEquals(3, $arr['zoo']['half']);
}

public function getRecursiveIssetExamples() {
return [
[
[[[], [0, 1, 2], []]], [0, 1, 2], TRUE,
],
[
[[[], [0, 1, 2], []]], [0, 1, 3], FALSE,
],
[
[], ['foo'], FALSE,
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], FALSE,
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], TRUE,
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], TRUE,
],
];
}

/**
* @param $array
* @param $path
* @param $expected
* @dataProvider getRecursiveIssetExamples
*/
public function testRecursiveIsset($array, $path, $expected) {
$result = CRM_Utils_Array::recursiveIsset($array, $path);
$this->assertEquals($expected, $result);
}

public function getRecursiveValueExamples() {
return [
[
[[[], [0, 1, 2], []]], [0, 1, 2], NULL, 2,
],
[
[[[], [0, 1, 2], []]], [0, 1, 3], NULL, NULL,
],
[
[], ['foo'], FALSE, FALSE,
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'wrong'], 'nada', 'nada',
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right'], NULL, ['foo' => 1, 'bar' => 2]
],
[
[NULL, ['wrong' => NULL, 'right' => ['foo' => 1, 'bar' => 2]]], [1, 'right', 'foo'], NULL, 1,
],
];
}

/**
* @param $array
* @param $path
* @param $expected
* @dataProvider getRecursiveValueExamples
*/
public function testRecursiveValue($array, $path, $default, $expected) {
$result = CRM_Utils_Array::recursiveValue($array, $path, $default);
$this->assertEquals($expected, $result);
}

/**
* Get values for build test.
*/
public function getBuildValueExamples() {
return [
[
[0, 'email', 2, 'location'], [0 => ['email' => [2 => ['location' => 'llama']]]]
]
];
}

/**
* Test the build recursive function.
*
* @param $path
* @param $expected
*
* @dataProvider getBuildValueExamples
*/
public function testBuildRecursiveValue($path, $expected) {
$result = CRM_Utils_Array::recursiveBuild($path, 'llama');
$this->assertEquals($expected, $result);
}

}

0 comments on commit 1cd4175

Please sign in to comment.