From 62a5afbcf4fd30830fc4f009f38add92ce9a1c55 Mon Sep 17 00:00:00 2001 From: Duilio Palacios Date: Fri, 14 Jul 2017 15:14:21 +0100 Subject: [PATCH 1/2] Add unwrap to the Collection class --- src/Illuminate/Support/Collection.php | 11 +++++++++++ tests/Support/SupportCollectionTest.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 4d48e29b92ca..ab90afaf6df7 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -72,6 +72,17 @@ public static function wrap($value) : new static(Arr::wrap($value)); } + /** + * If the given value is a collection return its items. + * + * @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. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index b99d7d9c4432..e25add367bfa 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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) { From 173137e4ae18c38ed1dda259af85e35716370451 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 14 Jul 2017 11:45:03 -0500 Subject: [PATCH 2/2] Update Collection.php --- src/Illuminate/Support/Collection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index ab90afaf6df7..8ebebf4678ca 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -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 @@ -73,7 +73,7 @@ public static function wrap($value) } /** - * If the given value is a collection return its items. + * Get the underlying items from the given collection if applicable. * * @param array|static $value * @return array