Skip to content

Commit 3991f28

Browse files
authored
Merge pull request #96 from endelwar/filter-duplicate-periods-in-collection
Filter duplicate periods in collection
2 parents 0dabfa4 + fd2abf8 commit 3991f28

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/PeriodCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,14 @@ private function overlap(PeriodCollection $others): PeriodCollection
179179

180180
return $overlaps;
181181
}
182+
183+
public function unique(): PeriodCollection
184+
{
185+
$uniquePeriods = [];
186+
foreach ($this->periods as $period) {
187+
$uniquePeriods[$period->asString()] = $period;
188+
}
189+
190+
return new static(...array_values($uniquePeriods));
191+
}
182192
}

tests/PeriodCollectionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,24 @@ public function it_substracts_empty_period_collection()
315315

316316
$this->assertCount(4, $collection);
317317
}
318+
319+
/** @test */
320+
public function it_filters_duplicate_periods_in_collection()
321+
{
322+
$collection = new PeriodCollection(
323+
Period::make('2018-01-01', '2018-01-02'),
324+
Period::make('2018-01-01', '2018-01-02'),
325+
Period::make('2018-01-01', '2018-01-02'),
326+
Period::make('2018-01-01', '2018-01-02'),
327+
Period::make('2018-01-01', '2018-01-02'),
328+
Period::make('2018-01-30', '2018-01-31')
329+
);
330+
331+
$unique = $collection->unique();
332+
333+
$this->assertCount(6, $collection);
334+
$this->assertCount(2, $unique);
335+
$this->assertTrue($unique[0]->equals($collection[0]));
336+
$this->assertTrue($unique[1]->equals($collection[5]));
337+
}
318338
}

0 commit comments

Comments
 (0)