From c3219a630fbf9627538f37107c8427cdb60b949b Mon Sep 17 00:00:00 2001 From: Aaron Costello Date: Fri, 3 Feb 2017 17:16:22 +0000 Subject: [PATCH 1/2] Add tap method to collection --- src/Illuminate/Support/Collection.php | 12 ++++++++++++ tests/Support/SupportCollectionTest.php | 13 +++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 93bf117c67c3..3529f3ce420e 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -826,6 +826,18 @@ public function pipe(callable $callback) return $callback($this); } + /** + * Pass the collection to the given callback and return the current instance. + * + * @param callable $callback + * @return $this + */ + public function tap(callable $callback) + { + $callback(new static($this->items)); + return $this; + } + /** * Get and remove the last item from the collection. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 22ac4b3a2599..59be6b0d4290 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1882,6 +1882,19 @@ public function testHigherOrderPartition() $this->assertSame(['b' => ['free' => false]], $premium->toArray()); } + + public function testTap() + { + $collection = new Collection([1, 2, 3]); + + $fromTap = []; + $collection = $collection->tap(function ($collection) use (&$fromTap) { + $fromTap = $collection->slice(0, 1)->toArray(); + }); + + $this->assertSame([1], $fromTap); + $this->assertSame([1, 2, 3], $collection->toArray()); + } } class TestSupportCollectionHigherOrderItem From 5cd2eda8c2bd01e268c20c75d2a26cda3b0496f6 Mon Sep 17 00:00:00 2001 From: Aaron Costello Date: Fri, 3 Feb 2017 17:20:49 +0000 Subject: [PATCH 2/2] cs fix --- src/Illuminate/Support/Collection.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 3529f3ce420e..e9f9e4f9b51a 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -835,6 +835,7 @@ public function pipe(callable $callback) public function tap(callable $callback) { $callback(new static($this->items)); + return $this; }