Skip to content

Commit ab29010

Browse files
[8.x] Add pipeThrough collection method (#40253)
* Add `pipeThrough` collection method * Spacing * Update EnumeratesValues.php Co-authored-by: Taylor Otwell <[email protected]>
1 parent 8f55fff commit ab29010

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/Illuminate/Collections/Traits/EnumeratesValues.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,22 @@ public function pipeInto($class)
721721
return new $class($this);
722722
}
723723

724+
/**
725+
* Pass the collection through a series of callable pipes and return the result.
726+
*
727+
* @param array<callable> $pipes
728+
* @return mixed
729+
*/
730+
public function pipeThrough($pipes)
731+
{
732+
return static::make($pipes)->reduce(
733+
function ($carry, $pipe) {
734+
return $pipe($carry);
735+
},
736+
$this,
737+
);
738+
}
739+
724740
/**
725741
* Pass the collection to the given callback and then return it.
726742
*

tests/Support/SupportCollectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,6 +4007,25 @@ public function testPipeInto($collection)
40074007
$this->assertSame($data, $instance->value);
40084008
}
40094009

4010+
/**
4011+
* @dataProvider collectionClassProvider
4012+
*/
4013+
public function testPipeThrough($collection)
4014+
{
4015+
$data = new $collection([1, 2, 3]);
4016+
4017+
$result = $data->pipeThrough([
4018+
function ($data) {
4019+
return $data->merge([4, 5]);
4020+
},
4021+
function ($data) {
4022+
return $data->sum();
4023+
},
4024+
]);
4025+
4026+
$this->assertEquals(15, $result);
4027+
}
4028+
40104029
/**
40114030
* @dataProvider collectionClassProvider
40124031
*/

0 commit comments

Comments
 (0)