Skip to content

Implement hasAll(), hasAny(), doesNotHave() and doesNotHaveAny() #26543

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
wants to merge 1 commit into from
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
68 changes: 66 additions & 2 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -869,12 +869,12 @@ public function keyBy($keyBy)
}

/**
* Determine if an item exists in the collection by key.
* Determine if all items exist in the collection by their keys.
*
* @param mixed $key
* @return bool
*/
public function has($key)
public function hasAll($key)
{
$keys = is_array($key) ? $key : func_get_args();

Expand All @@ -887,6 +887,70 @@ public function has($key)
return true;
}

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

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

return false;
}

/**
* Determine if one items is missing in the collection by its key.
*
* @param mixed $key
* @return bool
*/
public function doesNotHave($key)
{
return ! $this->offsetExists($key);
}

/**
* Determine if none of the items exist in the collection by their keys.
*
* @param mixed $key
* @return bool
*/
public function doesNotHaveAny($key)
{
$keys = is_array($key) ? $key : func_get_args();

foreach ($keys as $value) {
if ($this->offsetExists($value)) {
return false;
}
}

return true;
}

/**
* Determine if an item exists in the collection by key.
*
* @param mixed $key
* @return bool
*
* @deprecated use hasAll() instead
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();

return $this->hasAll($keys);
}

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

public function testHasAll()
{
$data = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
$this->assertTrue($data->hasAll('first'));
$this->assertFalse($data->hasAll('third'));
$this->assertTrue($data->hasAll(['first', 'second']));
$this->assertTrue($data->hasAll('first', 'second'));
$this->assertFalse($data->hasAll(['third', 'first']));
$this->assertFalse($data->hasAll('third', 'first'));
}

public function testHasAny()
{
$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', 'second'));
$this->assertTrue($data->hasAny(['third', 'first']));
$this->assertTrue($data->hasAny('third', 'first'));
}

public function testDoesNotHave()
{
$data = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
$this->assertFalse($data->doesNotHave('first'));
$this->assertTrue($data->doesNotHave('third'));
}

public function testDoesNotHaveAny()
{
$data = new Collection(['id' => 1, 'first' => 'Hello', 'second' => 'World']);
$this->assertFalse($data->doesNotHaveAny('first'));
$this->assertTrue($data->doesNotHaveAny('third'));
$this->assertFalse($data->doesNotHaveAny(['first', 'third']));
$this->assertFalse($data->doesNotHaveAny('first', 'third'));
$this->assertTrue($data->doesNotHaveAny(['name', 'third']));
$this->assertTrue($data->doesNotHaveAny('name', 'third'));
}

public function testImplode()
Expand Down