-
Notifications
You must be signed in to change notification settings - Fork 4
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
Feat: null filter #25
Conversation
Codecov Report
@@ Coverage Diff @@
## 2.0.x-dev #25 +/- ##
===========================================
Coverage 100.00% 100.00%
===========================================
Files 2 2
Lines 43 48 +5
===========================================
+ Hits 43 48 +5
Continue to review full report at Codecov.
|
4052d03
to
fd05360
Compare
Today I stumbled upon this But first, I don't get the readme
Why should it matter whether I pass an |
I think there's a mistake in the doc indeed ^^ I thought about iterable output, array|Traversable input. Example: interface StuffProviderInterface
{
public function getStuff(): iterable;
}
function ingestStuff(Traversable $stuff): void
{
// do something
}
// ...
ingestStuff(iterable($stuffProvider->getStuff())); // bec. we're not supposed to know if the implementation returns an array or a Traversable Similarly: interface StuffProviderInterface
{
public function getStuff(): iterable;
}
function ingestStuff(array $stuff): void
{
// do something
}
// ...
ingestStuff(iterable($stuffProvider->getStuff())->asArray()); But that's not really relevant since we provide functions for that. |
But basically we should think about |
|
||
public function filter(?callable $filter = null): self | ||
{ | ||
$filter = $filter ?? function ($value): bool { |
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.
When I did the benchmarking last time on 7.4, using intval
or boolval
in this case was faster than anon. fcn.
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.
Good idea 🙂 but I just tested it, and it throws warnings when used within CallbackFilterIterator
because it expects 1 parameter, and the CallbackFilterIterator
injects 3.
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.
That reminds me that we currently don't support the $mode
argument of the array_filter() function, I'll try to address that in a separate PR.
@bpolaszek no, let's 🚢 |
Since we can omit the 2nd argument of the native
array_filter
function, resulting in an output array is filtered on truthy values, we should be able to invokeIterableObject::filter()
similarly.This PR allows
IterableObject::filter()
to accept a null callback (or no args at all) to enable this behavior.To prevent this default filter from being applied when
IterableObject::$filter
is null (with the "no filter" intention),IterableObject
's constructor is now private, and theIterableObject::filter()
method MUST be used to ensure user's intention. Hence$filter
and$map
arguments from theiterable()
function have been removed to enforce using the fluent interface.@simPod would you mind reviewing this?