Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] Add pipe function to collection #13899

Merged
merged 1 commit into from
Jul 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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