Skip to content

Commit ed3a8db

Browse files
authored
[8.x] Added the whereInstanceOfAny() method for collections (#36328)
* Added the whereInstanceOfAny() method for collections. * Moved whereInstanceOfAny logic into whereInstanceOf method. * Added tests for using whereInstanceOf with an array.
1 parent bb9078a commit ed3a8db

File tree

3 files changed

+17
-5
lines changed

3 files changed

+17
-5
lines changed

src/Illuminate/Collections/Enumerable.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ public function whereNotIn($key, $values, $strict = false);
415415
public function whereNotInStrict($key, $values);
416416

417417
/**
418-
* Filter the items, removing any items that don't match the given type.
418+
* Filter the items, removing any items that don't match the given type(s).
419419
*
420-
* @param string $type
420+
* @param string|string[] $type
421421
* @return static
422422
*/
423423
public function whereInstanceOf($type);

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,14 +669,24 @@ public function whereNotInStrict($key, $values)
669669
}
670670

671671
/**
672-
* Filter the items, removing any items that don't match the given type.
672+
* Filter the items, removing any items that don't match the given type(s).
673673
*
674-
* @param string $type
674+
* @param string|string[] $type
675675
* @return static
676676
*/
677677
public function whereInstanceOf($type)
678678
{
679679
return $this->filter(function ($value) use ($type) {
680+
if (is_array($type)) {
681+
foreach ($type as $classType) {
682+
if ($value instanceof $classType) {
683+
return true;
684+
}
685+
}
686+
687+
return false;
688+
}
689+
680690
return $value instanceof $type;
681691
});
682692
}

tests/Support/SupportCollectionTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,10 @@ public function testWhereStrict($collection)
766766
*/
767767
public function testWhereInstanceOf($collection)
768768
{
769-
$c = new $collection([new stdClass, new stdClass, new $collection, new stdClass]);
769+
$c = new $collection([new stdClass, new stdClass, new $collection, new stdClass, new Str]);
770770
$this->assertCount(3, $c->whereInstanceOf(stdClass::class));
771+
772+
$this->assertCount(4, $c->whereInstanceOf([stdClass::class, Str::class]));
771773
}
772774

773775
/**

0 commit comments

Comments
 (0)