diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 2dfbea6b1bcc..a52fa5e66e70 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -735,6 +735,33 @@ public function containsOneItem(?callable $callback = null): bool return $this->count() === 1; } + /** + * Determine if the collection contains multiple items. If a callback is provided, determine if multiple items match the condition. + * + * @param (callable(TValue, TKey): bool)|null $callback + * @return bool + */ + public function containsManyItems(?callable $callback = null): bool + { + if (! $callback) { + return $this->count() > 1; + } + + $count = 0; + + foreach ($this as $key => $item) { + if ($callback($item, $key)) { + $count++; + } + + if ($count > 1) { + return true; + } + } + + return false; + } + /** * Join all items from the collection using a string. The final items can use a separate glue string. * diff --git a/src/Illuminate/Collections/Enumerable.php b/src/Illuminate/Collections/Enumerable.php index b59af1892dc6..c4e599b0d049 100644 --- a/src/Illuminate/Collections/Enumerable.php +++ b/src/Illuminate/Collections/Enumerable.php @@ -625,6 +625,13 @@ public function isNotEmpty(); */ public function containsOneItem(); + /** + * Determine if the collection contains multiple items. + * + * @return bool + */ + public function containsManyItems(); + /** * Join all items from the collection using a string. The final items can use a separate glue string. * diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 025f87ea10fd..31afc8157148 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -731,6 +731,16 @@ public function containsOneItem() return $this->take(2)->count() === 1; } + /** + * Determine if the collection contains multiple items. + * + * @return bool + */ + public function containsManyItems(): bool + { + return $this->take(2)->count() > 1; + } + /** * Join all items from the collection using a string. The final items can use a separate glue string. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index e02322db80f2..b723b8d92a05 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -900,6 +900,20 @@ public function testContainsOneItem($collection) $this->assertFalse(collect(['ant', 'bear', 'cat'])->containsOneItem(fn ($word) => strlen($word) > 4)); } + #[DataProvider('collectionClassProvider')] + public function testContainsManyItems($collection) + { + $this->assertFalse((new $collection([]))->containsManyItems()); + $this->assertFalse((new $collection([1]))->containsManyItems()); + $this->assertTrue((new $collection([1, 2]))->containsManyItems()); + $this->assertTrue((new $collection([1, 2, 3]))->containsManyItems()); + + $this->assertTrue(collect([1, 2, 2])->containsManyItems(fn ($number) => $number === 2)); + $this->assertFalse(collect(['ant', 'bear', 'cat'])->containsManyItems(fn ($word) => strlen($word) === 4)); + $this->assertFalse(collect(['ant', 'bear', 'cat'])->containsManyItems(fn ($word) => strlen($word) > 4)); + $this->assertTrue(collect(['ant', 'bear', 'cat'])->containsManyItems(fn ($word) => strlen($word) === 3)); + } + public function testIterable() { $c = new Collection(['foo']); diff --git a/tests/Support/SupportLazyCollectionTest.php b/tests/Support/SupportLazyCollectionTest.php index 7c0f2a87093e..b73034450750 100644 --- a/tests/Support/SupportLazyCollectionTest.php +++ b/tests/Support/SupportLazyCollectionTest.php @@ -392,6 +392,21 @@ public function testContainsOneItem() $this->assertFalse($multipleCollection->containsOneItem()); } + public function testContainsManyItems() + { + $emptyCollection = new LazyCollection([]); + $this->assertFalse($emptyCollection->containsManyItems()); + + $singleCollection = new LazyCollection([1]); + $this->assertFalse($singleCollection->containsManyItems()); + + $multipleCollection = new LazyCollection([1, 2]); + $this->assertTrue($multipleCollection->containsManyItems()); + + $manyCollection = new LazyCollection([1, 2, 3]); + $this->assertTrue($manyCollection->containsManyItems()); + } + public function testDoesntContain() { $collection = new LazyCollection([1, 2, 3, 4, 5]);