Skip to content
Merged
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
10 changes: 10 additions & 0 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,16 @@ public function isEmpty()
return empty($this->items);
}

/**
* Determine if the collection contains a single element.
*
* @return bool
*/
public function isSingle()
{
return $this->count() === 1;
}

/**
* Join all items from the collection using a string. The final items can use a separate glue string.
*
Expand Down
10 changes: 10 additions & 0 deletions src/Illuminate/Collections/LazyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,16 @@ public function isEmpty()
return ! $this->getIterator()->valid();
}

/**
* Determine if the collection contains a single element.
*
* @return bool
*/
public function isSingle()
{
return $this->take(2)->count() === 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is take(2) necessary? :)

Copy link
Contributor Author

@JosephSilber JosephSilber Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gocanto without it, the whole collection would have to be enumerated, which is a waste.

As soon as we know that there are 2, we know that it's not single!


See this test, which ensures we don't enumerate more than 2 items:

public function testIsSingleIsLazy()
{
$this->assertEnumerates(2, function ($collection) {
$collection->isSingle();
});
}

Without take(2), this test would fail.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool stuff.

}

/**
* Join all items from the collection using a string. The final items can use a separate glue string.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ public function testCountableByWithCallback($collection)
})->all());
}

/**
* @dataProvider collectionClassProvider
*/
public function testIsSingle($collection)
{
$this->assertFalse((new $collection([]))->isSingle());
$this->assertTrue((new $collection([1]))->isSingle());
$this->assertFalse((new $collection([1, 2]))->isSingle());
}

public function testIterable()
{
$c = new Collection(['foo']);
Expand Down
7 changes: 7 additions & 0 deletions tests/Support/SupportLazyCollectionIsLazyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,13 @@ public function testIsNotEmptyIsLazy()
});
}

public function testIsSingleIsLazy()
{
$this->assertEnumerates(2, function ($collection) {
$collection->isSingle();
});
}

public function testJoinIsLazy()
{
$this->assertEnumeratesOnce(function ($collection) {
Expand Down