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.4] Higher-order messages for the collection class #16267

Conversation

franzliedke
Copy link
Contributor

Howdy - long time, no see! :)

I've recently come across an article about a concept called Higher-Order Messages. That particular article was about Ruby, but it intrigued me to implement this for Laravel. And it turns out it's very easy!

This lets you turn some very common collection use-cases into concise one-liners.

filter example

// Instead of this:
$collection->filter(function($item) {
  return $item->field;
});

// Write this:
$collection->where->field;

It also works for other collection methods:

// Instead of this:
$collection->each(function($item) {
  return $item->printYourself();
});

// Write this:
$collection->do->printYourself();

This is just a proposal. If you're interested in merging this into Laravel, I'll gladly provide the unit tests and clean up the code. If not, I have saved some time and will probably publish a package.

Naming: I used the conventions from the aforementioned article, but this probably requires some input to make it consistent with the rest of the collection.

@franzliedke franzliedke changed the title Basic implementation of higher-order messages [5.4] Higher-oder messages for the collection class Nov 4, 2016
Copy link

@CristianLlanos CristianLlanos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks interesting :)

public function __get($name)
{
return $this->collection->{$this->method}(function($value) use ($name) {
return $value->$name;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we shouldn't assume the value will be an object. We also use collections for arrays.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good point. We could add an offsetGet method. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@franzliedke I fixed this in a separate PR to allow getting by object or array

@taylorotwell taylorotwell merged commit 8dd57f9 into laravel:master Nov 4, 2016
@taylorotwell
Copy link
Member

That's some clever stuff! 😈

@franzliedke
Copy link
Contributor Author

Woah, this wasn't ready yet. I wanted to add tests, and docs, and an offsetGet method.

'inReverseOrderOf' => 'sortByDesc',
];

if (isset($nameToMethod[$name])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

am I reading this wrong? shouldn't this be !isset()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, that's what I get for refactoring this method at the last moment.

'sum' => 'sum',
'inOrderOf' => 'sortBy',
'inReverseOrderOf' => 'sortByDesc',
];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that array we can't make the $invoices->each->pay(); announced in the LN article . Can we ? We have to do $invoices->do->pay(); instead. Maybe am I missing something somewhere else ? In this case, I think we should use the original methods too.
Thx.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was just the original implementation with the method names from the article that I linked in the original description. I didn't consider this finished, but Taylor merged it and changed it to what was explained in the Laravel News article.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've seen that afterwards. Too bad I liked the aliases you made...
But I've seen too that the merge and the update were fast and uncontrolled ! ^^"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would prefer $invoices->each()->pay(); format over $invoices->each->pay();

@tillkruss tillkruss changed the title [5.4] Higher-oder messages for the collection class [5.4] Higher-order messages for the collection class Nov 13, 2016
@franzliedke franzliedke deleted the fl/collections-higher-order-messages branch June 9, 2021 20:33
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.

7 participants