Skip to content

Commit

Permalink
add pipe function (#13899)
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze authored and taylorotwell committed Jul 2, 2016
1 parent cffcd34 commit 0b4f798
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0b4f798

Please sign in to comment.