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

[11.x] Add containsAny method to Collection #52373

Closed

Conversation

DeanWunder
Copy link
Contributor

What this PR contains

This PR adds the containsAny method to Collections which returns true if any of the supplied items are contained within the collection.

Some tests have also been added.

Why this PR should be merged

There currently exists a gap in the available methods for collections. Users can check if multiple items all exist in a collection with contains, however there is no similar containsAny method to provide OR functionality. The current workaround for users is to use intersect which I believe is not sufficiently elegant and expressive for laravel.

For example:

collect([1, 2, 3])->intersect([3, 4])->isNotEmpty() // true

Readers of this code must remind themselves of the definition of an intersection, and consider the meaning of both intersect and isNotEmpty together when understanding the functionality here. Following this PR, this code becomes more terse and easily understandable, and reads like an english sentence.

collect([1, 2, 3])->containsAny([3, 4]) // true

Furthermore, the method hasAny already exists for collections as a similar OR-analogue for has, which may lead users to expect there to exist a containsAny method as a OR-analogue for contains, which this PR provides.

@DeanWunder DeanWunder marked this pull request as draft August 4, 2024 02:21
@DeanWunder DeanWunder changed the title Add containsAny method to Collection [11.x] Add containsAny method to Collection Aug 4, 2024
@DeanWunder DeanWunder marked this pull request as ready for review November 1, 2024 07:38
@DeanWunder DeanWunder changed the title [11.x] Add containsAny method to Collection [11.x] Add containsAny method to Collection Nov 1, 2024
@DeanWunder DeanWunder marked this pull request as draft November 1, 2024 07:49
@DeanWunder DeanWunder marked this pull request as ready for review November 1, 2024 08:30
@DeanWunder
Copy link
Contributor Author

Happy to hear any feedback.
No hard feelings if closure.

@taylorotwell
Copy link
Member

Thanks for your pull request to Laravel!

Unfortunately, I'm going to delay merging this code for now. To preserve our ability to adequately maintain the framework, we need to be very careful regarding the amount of code we include.

If applicable, please consider releasing your code as a package so that the community can still take advantage of your contributions!

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