Skip to content

Commit

Permalink
Add operator support to the "contains" method (#16791)
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephSilber authored and taylorotwell committed Dec 14, 2016
1 parent 800012d commit ed02967
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
11 changes: 4 additions & 7 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,14 @@ public function add($item)
* Determine if a key exists in the collection.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function contains($key, $value = null)
public function contains($key, $operator = null, $value = null)
{
if (func_num_args() == 2) {
return parent::contains($key, $value);
}

if ($this->useAsCallable($key)) {
return parent::contains($key);
if (func_num_args() > 1 || $this->useAsCallable($key)) {
return parent::contains(...func_get_args());
}

$key = $key instanceof Model ? $key->getKey() : $key;
Expand Down
21 changes: 13 additions & 8 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,22 +165,27 @@ public function collapse()
* Determine if an item exists in the collection.
*
* @param mixed $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function contains($key, $value = null)
public function contains($key, $operator = null, $value = null)
{
if (func_num_args() == 2) {
return $this->contains(function ($item) use ($key, $value) {
return data_get($item, $key) == $value;
});
if (func_num_args() == 1) {
if ($this->useAsCallable($key)) {
return ! is_null($this->first($key));
}

return in_array($key, $this->items);
}

if ($this->useAsCallable($key)) {
return ! is_null($this->first($key));
if (func_num_args() == 2) {
$value = $operator;

$operator = '=';
}

return in_array($key, $this->items);
return $this->contains($this->operatorForWhere($key, $operator, $value));
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Database/DatabaseEloquentCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public function testGettingMinItemsFromCollection()
$this->assertEquals(10, $c->min('foo'));
}

public function testContainsWithMultipleArguments()
{
$c = new Collection([['id' => 1], ['id' => 2]]);

$this->assertTrue($c->contains('id', 1));
$this->assertTrue($c->contains('id', '>=', 2));
$this->assertFalse($c->contains('id', '>', 2));
}

public function testContainsIndicatesIfModelInArray()
{
$mockModel = m::mock('Illuminate\Database\Eloquent\Model');
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1244,6 +1244,16 @@ public function testContainsStrict()
$this->assertTrue($c->containsStrict(''));
}

public function testContainsWithOperator()
{
$c = new Collection([['v' => 1], ['v' => 3], ['v' => '4'], ['v' => 5]]);

$this->assertTrue($c->contains('v', '=', 4));
$this->assertTrue($c->contains('v', '==', 4));
$this->assertFalse($c->contains('v', '===', 4));
$this->assertTrue($c->contains('v', '>', 4));
}

public function testGettingSumFromCollection()
{
$c = new Collection([(object) ['foo' => 50], (object) ['foo' => 50]]);
Expand Down

0 comments on commit ed02967

Please sign in to comment.