Skip to content

[12.x] Introducing Collection hasAll Method #55770

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
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
23 changes: 23 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,29 @@ public function hasAny($key)
return false;
}

/**
* Determine if all keys exist in the collection.
*
* @param TKey|array<array-key, TKey> $keys
* @return bool
*
* @throws \InvalidArgumentException
*/
public function hasAll($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

if (empty($keys)) {
throw new InvalidArgumentException('Keys must be provided.');
}

if ($this->isEmpty()) {
return false;
}

return empty(array_diff($keys, array_keys($this->all())));
}

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

/**
* Determine if all keys exist in the collection.
*
* @param TKey|array<array-key, TKey> $keys
* @return bool
*
* @throws \InvalidArgumentException
*/
public function hasAll($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();

if (empty($keys)) {
throw new InvalidArgumentException('Keys must be provided.');
}

if ($this->isEmpty()) {
return false;
}

return empty(array_diff($keys, array_keys($this->all())));
}

/**
* Concatenate values of a given key as a string.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2357,6 +2357,25 @@ public function testHasAny($collection)
$this->assertFalse($data->hasAny([]));
}

#[DataProvider('collectionClassProvider')]
public function testHasAll($collection)
{
$data = new $collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);

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

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Keys must be provided.');

$data->hasAll([]);
}

#[DataProvider('collectionClassProvider')]
public function testImplode($collection)
{
Expand Down