Skip to content

Commit

Permalink
Merge pull request #4606 from paulbalandan/array-validation
Browse files Browse the repository at this point in the history
Fix validation of array data
  • Loading branch information
paulbalandan authored Apr 24, 2021
2 parents 0198df1 + ace2698 commit d6bb32f
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 100 deletions.
34 changes: 22 additions & 12 deletions system/Helpers/array_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @param string $index
* @param array $array
*
* @return mixed|null
* @return mixed
*/
function dot_array_search(string $index, array $array)
{
Expand All @@ -39,20 +39,20 @@ function dot_array_search(string $index, array $array)
if (! function_exists('_array_search_dot'))
{
/**
* Used by dot_array_search to recursively search the
* Used by `dot_array_search` to recursively search the
* array with wildcards.
*
* @internal This should not be used on its own.
*
* @param array $indexes
* @param array $array
*
* @return mixed|null
* @return mixed
*/
function _array_search_dot(array $indexes, array $array)
{
// Grab the current index
$currentIndex = $indexes
? array_shift($indexes)
: null;
$currentIndex = $indexes ? array_shift($indexes) : null;

if ((empty($currentIndex) && (int) $currentIndex !== 0) || (! isset($array[$currentIndex]) && $currentIndex !== '*'))
{
Expand All @@ -62,18 +62,28 @@ function _array_search_dot(array $indexes, array $array)
// Handle Wildcard (*)
if ($currentIndex === '*')
{
// If $array has more than 1 item, we have to loop over each.
$answer = [];

foreach ($array as $value)
{
$answer = _array_search_dot($indexes, $value);
$answer[] = _array_search_dot($indexes, $value);
}

$answer = array_filter($answer, static function ($value) {
return $value !== null;
});

if ($answer !== null)
if ($answer !== [])
{
if (count($answer) === 1)
{
return $answer;
// If array only has one element, we return that element for BC.
return current($answer);
}

return $answer;
}

// Still here after searching all child nodes?
return null;
}

Expand All @@ -85,7 +95,7 @@ function _array_search_dot(array $indexes, array $array)
}

// Do we need to recursively search this value?
if (is_array($array[$currentIndex]) && $array[$currentIndex])
if (is_array($array[$currentIndex]) && $array[$currentIndex] !== [])
{
return _array_search_dot($indexes, $array[$currentIndex]);
}
Expand Down
Loading

0 comments on commit d6bb32f

Please sign in to comment.