Skip to content

Commit

Permalink
[8.x] Add hasAny() Method to Collections (#39155)
Browse files Browse the repository at this point in the history
* WIP

* Test to verify hasAny() method is actually lazy

* Additional test to verify hasAny() method is actually lazy
  • Loading branch information
intrepidws authored Oct 11, 2021
1 parent d41ff7e commit 758565c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,29 @@ public function has($key)
return true;
}

/**
* Determine if any of the keys exist in the collection.
*
* @param mixed $key
* @return bool
*/
public function hasAny($key)
{
if ($this->isEmpty()) {
return false;
}

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

foreach ($keys as $value) {
if ($this->has($value)) {
return true;
}
}

return false;
}

/**
* Concatenate values of a given key as a string.
*
Expand Down
19 changes: 19 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,25 @@ public function has($key)
return false;
}

/**
* Determine if any of the keys exist in the collection.
*
* @param mixed $key
* @return bool
*/
public function hasAny($key)
{
$keys = array_flip(is_array($key) ? $key : func_get_args());

foreach ($this as $key => $value) {
if (array_key_exists($key, $keys)) {
return true;
}
}

return false;
}

/**
* Concatenate values of a given key as a string.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2078,6 +2078,20 @@ public function testHas($collection)
$this->assertFalse($data->has(['third', 'first']));
}

/**
* @dataProvider collectionClassProvider
*/
public function testHasAny($collection)
{
$data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);

$this->assertTrue($data->hasAny('first'));
$this->assertFalse($data->hasAny('third'));
$this->assertTrue($data->hasAny(['first', 'second']));
$this->assertTrue($data->hasAny(['first', 'fourth']));
$this->assertFalse($data->hasAny(['third', 'fourth']));
}

/**
* @dataProvider collectionClassProvider
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,25 @@ public function testHasIsLazy()
$this->assertEnumerates(5, function ($collection) {
$collection->has(4);
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->has('non-existent');
});
}

public function testHasAnyIsLazy()
{
$this->assertEnumerates(5, function ($collection) {
$collection->hasAny(4);
});

$this->assertEnumerates(2, function ($collection) {
$collection->hasAny([1, 4]);
});

$this->assertEnumeratesOnce(function ($collection) {
$collection->hasAny(['non', 'existent']);
});
}

public function testImplodeEnumeratesOnce()
Expand Down

0 comments on commit 758565c

Please sign in to comment.