Skip to content

Commit

Permalink
Fix behavior of ::filter() operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jan 6, 2020
1 parent 03a53fa commit bc2d733
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
15 changes: 11 additions & 4 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ public function it_can_explode(): void
);
}

public function it_can_filter_its_element(): void
public function it_can_filter(): void
{
$input = array_merge([0, false], range(1, 10));

Expand All @@ -488,10 +488,17 @@ public function it_can_filter_its_element(): void
->normalize()
->shouldIterateAs([1, 3, 5, 7, 9]);

$this
->filter()
$this::with(['afooe', 'fooe', 'allo', 'llo'])
->filter(
static function ($value) {
return 0 === mb_strpos($value, 'a');
},
static function ($value) {
return mb_strlen($value) - 1 === mb_strpos($value, 'o');
}
)
->normalize()
->shouldIterateAs([]);
->shouldIterateAs(['allo']);
}

public function it_can_flatten(): void
Expand Down
6 changes: 4 additions & 2 deletions src/Operation/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public function on(iterable $collection): Closure
$callbacks = $this->callbacks;

return static function () use ($callbacks, $collection): Generator {
$iterator = $collection;
$iterator = new IterableIterator($collection);

foreach ($callbacks as $callback) {
yield from $iterator = new CallbackFilterIterator(new IterableIterator($iterator), $callback);
$iterator = new CallbackFilterIterator($iterator, $callback);
}

yield from $iterator;
};
}
}

0 comments on commit bc2d733

Please sign in to comment.