diff --git a/src/PeriodCollection.php b/src/PeriodCollection.php index eded01a..21922ec 100644 --- a/src/PeriodCollection.php +++ b/src/PeriodCollection.php @@ -134,7 +134,7 @@ public function filter(Closure $closure): static { $collection = clone $this; - $collection->periods = array_filter($collection->periods, $closure); + $collection->periods = array_values(array_filter($collection->periods, $closure)); return $collection; } diff --git a/tests/PeriodCollectionTest.php b/tests/PeriodCollectionTest.php index b742599..33f396a 100644 --- a/tests/PeriodCollectionTest.php +++ b/tests/PeriodCollectionTest.php @@ -277,4 +277,25 @@ public function filter() $this->assertCount(1, $filtered); $this->assertTrue($filtered[0]->equals($collection[0])); } + + /** @test */ + public function it_loops_after_filter() + { + $collection = new PeriodCollection( + Period::make('2018-01-01', '2018-01-02'), + Period::make('2018-01-10', '2018-01-15'), + Period::make('2018-01-20', '2018-01-25'), + Period::make('2018-01-30', '2018-01-31') + ); + + $filtered = $collection->filter(function (Period $period) { + return $period->length() > 2; + }); + + $items = []; + foreach ($filtered as $item) { + $items[] = $item; + } + $this->assertEquals($filtered->count(), count($items)); + } }