File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments