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] collection pipe method + test. #14111

Closed
wants to merge 1 commit into from
Closed

[5.2] collection pipe method + test. #14111

wants to merge 1 commit into from

Conversation

gocanto
Copy link
Contributor

@gocanto gocanto commented Jun 22, 2016

Passes the collection into the callback, and returns the result.

When you are working into a collection chain, you might need keeping track of some result. Right now, this is possible dong something like so:

_Collection_

$data = new Collection([
            'a' => [
                'po_01' => 'value in pos 01',
                'po_02' => 'value in pos 01'
            ],

            'b' => [
                'po_01' => 'value in pos 01',
                'po_02' => 'value in pos 01',
                'sub' => [
                    'c' => [
                        'po_01' => 'value in pos 01',
                        'po_02' => 'value in pos 01',
                    ]
                ]
            ],
        ]);

_Operations_

$result = $data->filter(function ($item) {
     return collect($item)->has('sub');
})->map(function ($item, $key) {
     return $item['sub'];
});

// Now you will be able to operate using $result

_Using the pipe method_

You can do the operation within the chain, like so:

$data->filter(function ($item) {
     return collect($item)->has('sub');
})->map(function ($item, $key) {
     return $item['sub'];
})->pipe(function ($items) {
      //do something amazing within the chain.
})->pipe(function ($items) {
      //because it is fluent, it can add as much as it is needed.
 });

Note: I thought this was not necessary until I crossed to this situation 30 this morning.

@adamwathan calls this approach "something pretty neat" in his book!

I would like to know what you guys think about this. Please, take in account this is my first contribution!

Passes the collection into the callback, and returns the result.
@GrahamCampbell GrahamCampbell changed the title collection pipe method + test. [5.2] collection pipe method + test. Jun 22, 2016
@GrahamCampbell
Copy link
Member

Thanks. I think we already had a PR for this though?

@gocanto
Copy link
Contributor Author

gocanto commented Jun 22, 2016

you are right. I did not realize!

here it is: #13899

@gocanto gocanto closed this Jun 22, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants