From fbb30b15b9f1fed41401bf93030cfaece9c7f9cd Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 8 Jan 2019 13:55:43 +0100 Subject: [PATCH] No overlap returns empty collection --- CHANGELOG.md | 4 ++++ src/Period.php | 8 +++++++- tests/PeriodTest.php | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ebbc4aa..d072153 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ All notable changes to `period` will be documented in this file - initial release +## 0.4.1 - 2019-01-08 + +- No overlap returns empty collection + ## 0.4.0 - 2018-12-19 - Add visualizer diff --git a/src/Period.php b/src/Period.php index 3d577cd..a1a980a 100644 --- a/src/Period.php +++ b/src/Period.php @@ -263,7 +263,13 @@ public function overlap(Period ...$periods): PeriodCollection $overlapCollection = new PeriodCollection(); foreach ($periods as $period) { - $overlapCollection[] = $this->overlapSingle($period); + $overlap = $this->overlapSingle($period); + + if ($overlap === null) { + continue; + } + + $overlapCollection[] = $overlap; } return $overlapCollection; diff --git a/tests/PeriodTest.php b/tests/PeriodTest.php index a04df74..f364d8e 100644 --- a/tests/PeriodTest.php +++ b/tests/PeriodTest.php @@ -176,6 +176,15 @@ public function it_can_determine_the_overlap_between_multiple_periods() $this->assertTrue($overlap->equals(Period::make('2018-01-10', '2018-01-15'))); } + /** @test */ + public function non_overlapping_dates_return_an_empty_collection() + { + $a = Period::make('2019-01-01', '2019-01-31'); + $b = Period::make('2019-02-01', '2019-02-28'); + + $this->assertTrue($a->overlap($b)->isEmpty()); + } + /** @test */ public function it_can_determine_that_two_periods_do_not_overlap() {