Skip to content

Commit

Permalink
Merge pull request #96 from endelwar/filter-duplicate-periods-in-coll…
Browse files Browse the repository at this point in the history
…ection

Filter duplicate periods in collection
  • Loading branch information
brendt authored Oct 13, 2021
2 parents 0dabfa4 + fd2abf8 commit 3991f28
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/PeriodCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,14 @@ private function overlap(PeriodCollection $others): PeriodCollection

return $overlaps;
}

public function unique(): PeriodCollection
{
$uniquePeriods = [];
foreach ($this->periods as $period) {
$uniquePeriods[$period->asString()] = $period;
}

return new static(...array_values($uniquePeriods));
}
}
20 changes: 20 additions & 0 deletions tests/PeriodCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,4 +315,24 @@ public function it_substracts_empty_period_collection()

$this->assertCount(4, $collection);
}

/** @test */
public function it_filters_duplicate_periods_in_collection()
{
$collection = new PeriodCollection(
Period::make('2018-01-01', '2018-01-02'),
Period::make('2018-01-01', '2018-01-02'),
Period::make('2018-01-01', '2018-01-02'),
Period::make('2018-01-01', '2018-01-02'),
Period::make('2018-01-01', '2018-01-02'),
Period::make('2018-01-30', '2018-01-31')
);

$unique = $collection->unique();

$this->assertCount(6, $collection);
$this->assertCount(2, $unique);
$this->assertTrue($unique[0]->equals($collection[0]));
$this->assertTrue($unique[1]->equals($collection[5]));
}
}

0 comments on commit 3991f28

Please sign in to comment.