Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add unwrap to collection #20068

Merged
merged 2 commits into from
Jul 14, 2017
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
13 changes: 12 additions & 1 deletion src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static function make($items = [])
}

/**
* If the given value is not a collection, wrap it in one.
* Wrap the given value in a collection if applicable.
*
* @param mixed $value
* @return static
Expand All @@ -72,6 +72,17 @@ public static function wrap($value)
: new static(Arr::wrap($value));
}

/**
* Get the underlying items from the given collection if applicable.
*
* @param array|static $value
* @return array
*/
public static function unwrap($value)
{
return $value instanceof self ? $value->all() : $value;
}

/**
* Create a new collection by invoking the callback a given amount of times.
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,22 @@ public function testWrapWithCollectionSubclass()
$this->assertInstanceOf(TestCollectionSubclass::class, $collection);
}

public function testUnwrapCollection()
{
$collection = new Collection(['foo']);
$this->assertEquals(['foo'], Collection::unwrap($collection));
}

public function testUnwrapCollectionWithArray()
{
$this->assertEquals(['foo'], Collection::unwrap(['foo']));
}

public function testUnwrapCollectionWithScalar()
{
$this->assertEquals('foo', Collection::unwrap('foo'));
}

public function testTimesMethod()
{
$two = Collection::times(2, function ($number) {
Expand Down