Skip to content

Commit

Permalink
add whereInstanceOfMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Mar 2, 2018
1 parent 85247ee commit 78b5b92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,19 @@ public function whereNotInStrict($key, $values)
return $this->whereNotIn($key, $values, true);
}

/**
* Filter the items, removing any items that don't match the given type.
*
* @param string $type
* @return static
*/
public function whereInstanceOf($type)
{
return $this->filter(function ($value) use ($type) {
return $value instanceof $type;
});
}

/**
* Get the first item from the collection.
*
Expand Down
6 changes: 6 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,12 @@ public function testWhereStrict()
);
}

public function testWhereInstanceOf()
{
$c = new Collection([new stdClass, new stdClass, new Collection, new stdClass]);
$this->assertCount(3, $c->whereInstanceOf(stdClass::class));
}

public function testWhereIn()
{
$c = new Collection([['v' => 1], ['v' => 2], ['v' => 3], ['v' => '3'], ['v' => 4]]);
Expand Down

0 comments on commit 78b5b92

Please sign in to comment.