Skip to content

Commit

Permalink
Use new array helpers in Support Collection functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoordsij committed Nov 6, 2024
1 parent a8488ec commit 66e1f23
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ public function contains($key, $operator = null, $value = null)
{
if (func_num_args() === 1) {
if ($this->useAsCallable($key)) {
if (function_exists('array_any')) {
return array_any($this->items, $key);
}

$placeholder = new stdClass;

return $this->first($key, $placeholder) !== $placeholder;
Expand Down Expand Up @@ -573,6 +577,10 @@ public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();

if (function_exists('array_all')) {
return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
}

foreach ($keys as $value) {
if (! array_key_exists($value, $this->items)) {
return false;
Expand All @@ -596,6 +604,10 @@ public function hasAny($key)

$keys = is_array($key) ? $key : func_get_args();

if (function_exists('array_any')) {
return array_any($keys, fn ($key) => array_key_exists($key, $this->items));
}

foreach ($keys as $value) {
if ($this->has($value)) {
return true;
Expand Down Expand Up @@ -1154,6 +1166,10 @@ public function search($value, $strict = false)
return array_search($value, $this->items, $strict);
}

if (function_exists('array_find_key')) {
return array_find_key($this->items, $value) ?? false;
}

foreach ($this->items as $key => $item) {
if ($value($item, $key)) {
return $key;
Expand Down

0 comments on commit 66e1f23

Please # to comment.