Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup redundant array functions #12452

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 44 additions & 46 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -965,38 +965,58 @@ public static function extend(&$array, $other) {
}

/**
* Get a single value from an array-tre.
* Get a single value from an array-tree.
*
* @param array $arr
* Ex: array('foo'=>array('bar'=>123)).
* @param array $pathParts
* Ex: array('foo',bar').
* @return mixed|NULL
* @param array $values
* Ex: ['foo' => ['bar' => 123]].
* @param array $path
* Ex: ['foo', 'bar'].
* @param mixed $default
* @return mixed
* Ex 123.
*/
public static function pathGet($arr, $pathParts) {
$r = $arr;
foreach ($pathParts as $part) {
if (!isset($r[$part])) {
return NULL;
public static function pathGet($values, $path, $default = NULL) {
foreach ($path as $key) {
if (!is_array($values) || !isset($values[$key])) {
return $default;
}
$r = $r[$part];
$values = $values[$key];
}
return $r;
return $values;
}

/**
* 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 $values
* @param array $path
* @return bool
*/
public static function pathIsset($values, $path) {
foreach ($path as $key) {
if (!is_array($values) || !isset($values[$key])) {
return FALSE;
}
$values = $values[$key];
}
return TRUE;
}

/**
* Set a single value in an array tree.
*
* @param array $arr
* Ex: array('foo'=>array('bar'=>123)).
* @param array $values
* Ex: ['foo' => ['bar' => 123]].
* @param array $pathParts
* Ex: array('foo',bar').
* Ex: ['foo', 'bar'].
* @param $value
* Ex: 456.
*/
public static function pathSet(&$arr, $pathParts, $value) {
$r = &$arr;
public static function pathSet(&$values, $pathParts, $value) {
$r = &$values;
$last = array_pop($pathParts);
foreach ($pathParts as $part) {
if (!isset($r[$part])) {
Expand Down Expand Up @@ -1161,16 +1181,10 @@ public static function findInTree($search, $tree, $field = 'id') {
* @param array $array
* @param array $path
* @return bool
* @throws \CRM_Core_Exception
* @deprecated
*/
public static function recursiveIsset($array, $path) {
foreach ($path as $key) {
if (!is_array($array) || !isset($array[$key])) {
return FALSE;
}
$array = $array[$key];
}
return TRUE;
return self::pathIsset($array, $path);
}

/**
Expand All @@ -1185,16 +1199,10 @@ public static function recursiveIsset($array, $path) {
* @param mixed $default
* Value to return if not found.
* @return bool
* @throws \CRM_Core_Exception
* @deprecated
*/
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;
return self::pathGet($array, $path, $default);
}

/**
Expand All @@ -1208,20 +1216,10 @@ public static function recursiveValue($array, $path, $default = NULL) {
* @param array $source
*
* @return array
* @deprecated
*/
public static function recursiveBuild($path, $value, $source = []) {
$arrayKey = array_shift($path);
// 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;
}
self::pathSet($source, $path, $value);
return $source;
}

Expand Down