Skip to content

Commit

Permalink
add method to data_fetch get data using dot notation data_get() repro…
Browse files Browse the repository at this point in the history
…duce issue in some case
  • Loading branch information
aqibmehmmod committed Aug 6, 2024
1 parent 7e69645 commit 8d7c2a3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Illuminate/Collections/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,37 @@ function data_get($target, $key, $default = null)
return $target;
}
}
/**
* Get an item from an array or object using "dot" notation.
*
* @param mixed $target
* @param string|array|int|null $key
* @param mixed $default
* @return mixed
*/
function data_fetch(mixed $target, string|array|int|null $key, mixed $default = null, bool $trim = false): mixed
{
if (is_null($key)) {
return $default;
}

$key = is_array($key) ? $key : [$key];
$segment = array_shift($key);

if (Arr::accessible($target) && Arr::exists($target, $segment)) {
$target = $target[$segment];
} elseif (is_object($target) && isset($target->{$segment})) {
$target = $target->{$segment};
} else {
return $default;
}

if ($key) {
return data_fetch($target, $key, $default); // recursive call
} else {
return ($trim) ? trim($target) : $target;
}
}
if (! function_exists('data_set')) {
/**
* Set an item on an array or object using dot notation.
Expand Down

0 comments on commit 8d7c2a3

Please sign in to comment.