From 8c5beabe1f8ab463a9b0b36bb628177478da14f5 Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Thu, 4 Aug 2016 16:25:15 +0100 Subject: [PATCH 1/2] Allow collections to be created from objects that implement Traversable --- src/Illuminate/Support/Collection.php | 3 +++ tests/Support/SupportCollectionTest.php | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index a6a79271c940..5ce94840820c 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -4,6 +4,7 @@ use Countable; use ArrayAccess; +use Traversable; use ArrayIterator; use CachingIterator; use JsonSerializable; @@ -1241,6 +1242,8 @@ protected function getArrayableItems($items) return json_decode($items->toJson(), true); } elseif ($items instanceof JsonSerializable) { return $items->jsonSerialize(); + } elseif ($items instanceof Traversable) { + return iterator_to_array($items); } return (array) $items; diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index ec97ddde595e..de78f46ede39 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1390,6 +1390,18 @@ public function testSliceNegativeOffsetAndNegativeLength() $collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8]); $this->assertEquals([3, 4, 5, 6], $collection->slice(-6, -2)->values()->toArray()); } + + public function testCollectonFromTraversable() + { + $collection = new Collection(new \ArrayObject([1 , 2, 3])); + $this->assertEquals([1 , 2, 3], $collection->toArray()); + } + + public function testCollectonFromTraversableWithKeys() + { + $collection = new Collection(new \ArrayObject(['foo' => 1 , 'bar' => 2, 'baz' => 3])); + $this->assertEquals(['foo' => 1 , 'bar' => 2, 'baz' => 3], $collection->toArray()); + } } class TestAccessorEloquentTestStub From 8a272dbc295b12db4cbddee4abd9d5d55a0c362e Mon Sep 17 00:00:00 2001 From: Tim Williams Date: Fri, 5 Aug 2016 09:54:59 +0100 Subject: [PATCH 2/2] Style fix; remove exta space character --- tests/Support/SupportCollectionTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index de78f46ede39..6077046d0d42 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1393,14 +1393,14 @@ public function testSliceNegativeOffsetAndNegativeLength() public function testCollectonFromTraversable() { - $collection = new Collection(new \ArrayObject([1 , 2, 3])); - $this->assertEquals([1 , 2, 3], $collection->toArray()); + $collection = new Collection(new \ArrayObject([1, 2, 3])); + $this->assertEquals([1, 2, 3], $collection->toArray()); } public function testCollectonFromTraversableWithKeys() { - $collection = new Collection(new \ArrayObject(['foo' => 1 , 'bar' => 2, 'baz' => 3])); - $this->assertEquals(['foo' => 1 , 'bar' => 2, 'baz' => 3], $collection->toArray()); + $collection = new Collection(new \ArrayObject(['foo' => 1, 'bar' => 2, 'baz' => 3])); + $this->assertEquals(['foo' => 1, 'bar' => 2, 'baz' => 3], $collection->toArray()); } }