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

[7.x] Implement HigherOrderWhenProxy for Collections #32148

Merged
merged 1 commit into from
Mar 30, 2020
Merged

[7.x] Implement HigherOrderWhenProxy for Collections #32148

merged 1 commit into from
Mar 30, 2020

Conversation

lorisleiva
Copy link
Contributor

Hi there, 👋

This PR allows us to use a higher-order proxy for the Collection::when method.

// With this PR, this:
$collection->when($condition, function ($collection) use ($item) {
    $collection->push($item);
});

// ... can be refactored as this:
$collection->when($condition)->push($item);

I have also added the __get() magic method so that we can even chain other higher-order proxy methods like so:

// This:
$collection->when($condition, function ($collection) {
    $collection->map->parseIntoSomething();
});

// ... can be refactored as this:
$collection->when($condition)->map->parseIntoSomething();

If this is something you'd like to include in the Laravel framework, I will add some tests and update the documentation accordingly.

Cheers. 🍀

@taylorotwell taylorotwell merged commit eb0c397 into laravel:7.x Mar 30, 2020
imacrayon pushed a commit to imacrayon/framework that referenced this pull request Mar 31, 2020
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