Skip to content
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
47 changes: 44 additions & 3 deletions src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,31 @@ function array_except($array, $keys)
/**
* Return the first element in an array passing a given truth test.
*
* @deprecated A native function with the same name was introduced in PHP 8.5.
* Use Illuminate\Support\Arr::first to keep functionality
*
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @return mixed
*/
function array_first($array, ?callable $callback = null, $default = null)
{
return Arr::first($array, $callback, $default);
if (is_null($callback)) {
if (empty($array)) {
return value($default);
}

foreach ($array as $item) {
return $item;
}

return value($default);
}

$key = array_find_key($array, $callback);

return $key !== null ? $array[$key] : value($default);
}
}

Expand Down Expand Up @@ -148,14 +165,21 @@ function array_has($array, $keys)
/**
* Return the last element in an array passing a given truth test.
*
* @deprecated A native function with the same name was introduced in PHP 8.5.
* Use Illuminate\Support\Arr::last to keep functionality
*
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @return mixed
*/
function array_last($array, ?callable $callback = null, $default = null)
{
return Arr::last($array, $callback, $default);
if (is_null($callback)) {
return empty($array) ? value($default) : end($array);
}

return Arr::first(array_reverse($array, true), $callback, $default);
}
}

Expand Down Expand Up @@ -403,13 +427,30 @@ function str_before($subject, $search)
/**
* Determine if a given string contains a given substring.
*
* @deprecated A native function with the same name was introduced in PHP 8.0.
* Use Illuminate\Support\Str::contains to keep functionality
*
* @param string $haystack
* @param string|array $needles
* @return bool
*/
function str_contains($haystack, $needles)
{
return Str::contains($haystack, $needles);
if (is_null($haystack)) {
return false;
}

if (! is_iterable($needles)) {
$needles = (array) $needles;
}

foreach ($needles as $needle) {
if ($needle !== '' && strpos($haystack, $needle) !== false) {
return true;
}
}

return false;
}
}

Expand Down