From 3558f3db6a89687371447fd6ce7464307499fbad Mon Sep 17 00:00:00 2001 From: freek Date: Mon, 6 Jun 2016 22:27:04 +0200 Subject: [PATCH] add pipe function --- src/Illuminate/Support/Collection.php | 11 +++++++++++ tests/Support/SupportCollectionTest.php | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Illuminate/Support/Collection.php b/src/Illuminate/Support/Collection.php index 5aa615675fee..bcced62919a5 100644 --- a/src/Illuminate/Support/Collection.php +++ b/src/Illuminate/Support/Collection.php @@ -604,6 +604,17 @@ public function forPage($page, $perPage) return $this->slice(($page - 1) * $perPage, $perPage); } + /** + * Pass the collection to the given callback and return the result. + * + * @param callable $callback + * @return mixed + */ + public function pipe(callable $callback) + { + return $callback($this); + } + /** * Get and remove the last item from the collection. * diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index 870a817bd0bf..2b12ae21e1df 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -1273,6 +1273,15 @@ public function testRandomThrowsAnExceptionUsingAmountBiggerThanCollectionSize() $data = new Collection([1, 2, 3]); $data->random(4); } + + public function testPipe() + { + $collection = new Collection([1, 2, 3]); + + $this->assertEquals(6, $collection->pipe(function ($collection) { + return $collection->sum(); + })); + } } class TestAccessorEloquentTestStub