- Added array return type if key is incomplete

This commit is contained in:
mcndave committed 2020-10-06 16:00:27 +00:00
1 parent 026126be76
commit 86603044c0
3 files changed
+25 -57

No files matched your search

+17 -2
View File
@@ -49,12 +49,27 @@ class Iterate
return false;
}
public static function arrayGet(array $array, string $path, string $delimiter = '.') {
public static function arrayGet(iterable $array, string $path, string $delimiter = '.') {
$pathArr = explode($delimiter, $path);
if (isset($array[$pathArr[0]])) {
if ( isset($pathArr[1]) ) {
return static::arrayGet($array[array_shift($pathArr)], implode($delimiter, $pathArr));
$recurse = $array[array_shift($pathArr)];
if ( ! is_iterable($recurse) ) {
if ( is_object($recurse) ) {
if ( count($pathArr) > 1 ) {
throw new \Exception("An error occured trying to fetch a value from an object using path $path");
}
else {
# @TODO An object caller would be a WAY better idea then this line of code
# Calling $obj->varname
return $recurse->{$pathArr[0]} ?? "";
}
}
}
return static::arrayGet($recurse, implode($delimiter, $pathArr));
}
else {
return $array[$pathArr[0]];