-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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.3] Allow collection partition by key and add preserving keys option #16644
[5.3] Allow collection partition by key and add preserving keys option #16644
Conversation
5eec2c8
to
539d044
Compare
* @return array | ||
*/ | ||
public function partition(callable $callback) | ||
public function partition($callback, $preserveKeys = false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest preserving keys by default. It'll be more natural IMO.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, groupBy doesn't preverse keys by default 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Collections operations generally should preserve keys by default and there should be no option not to as you can call array_values or ->values() if you don't want to.
* @return array | ||
*/ | ||
public function partition(callable $callback) | ||
public function partition($callback, $preserveKeys = false) | ||
{ | ||
$partitions = [new static(), new static()]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, while you're at it you should probably change this to return a collection, for easy chaining if necessary.
A collection method should really never return an array.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JosephSilber, I agree. However, Taylor Otwell made a point about returning an array instead of a Collection object.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree since this is a simplify version of the groupBy method and it's meant to be used with list
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can destructure a collection with list
no problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool!
Remove preserveKeys option and make preserving keys the default please. |
539d044
to
6cc3068
Compare
@taylorotwell I'm returning a collection because as @JosephSilber pointed out it works with |
{ | ||
$partitions = [new static(), new static()]; | ||
$partitions = new static([new static(), new static()]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- We usually leave off the
()
for constructors with no arguments. - It'd be better to keep
$partitions
as a simple array, and only wrap it in a collection before returning. It's a minute performance improvement, but hey, why not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll update the PR
8c3c3b6
to
6fb8796
Compare
Can we make this work with the higher order stuff? |
@taylorotwell example? |
$collection->partition->free ... I'll look into it. |
Some small improvements to the partition method.
You can partition a collection by key (typically a column with a boolean value like: published/draft, premium/free, featured/not featured, etc.). i.e.
$posts->partition('featured')
.I also added the preserve keys option similar to the
groupBy
method.