From b33927cec73f07e66f3166cfd5db7e662efd1c28 Mon Sep 17 00:00:00 2001 From: Brent Roose Date: Tue, 18 Dec 2018 08:47:07 +0100 Subject: [PATCH] Support edge case for two period diffs --- CHANGELOG.md | 4 ++++ src/Period.php | 6 ++---- tests/PeriodTest.php | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d91d6d..94de611 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.3.3 - 2018-12-18 + +- Support edge case for two period diffs + ## 0.3.2 - 2018-12-11 - Add better return types to support inherited periods diff --git a/src/Period.php b/src/Period.php index ed429c6..7726647 100644 --- a/src/Period.php +++ b/src/Period.php @@ -318,12 +318,10 @@ public function diffSingle(Period $period): PeriodCollection public function diff(Period ...$periods): PeriodCollection { - if (count($periods) === 1) { + if (count($periods) === 1 && ! $this->overlapsWith($periods[0])) { $collection = new PeriodCollection(); - if (! $this->overlapsWith($periods[0])) { - $collection[] = $this->gap($periods[0]); - } + $collection[] = $this->gap($periods[0]); return $collection; } diff --git a/tests/PeriodTest.php b/tests/PeriodTest.php index cb0f8f5..a04df74 100644 --- a/tests/PeriodTest.php +++ b/tests/PeriodTest.php @@ -476,4 +476,22 @@ public function if_will_use_the_start_of_day_when_passing_strings_to_a_period() ) )); } + + /** + * @test + * + * A [=============================] + * B [========] + * + * DIFF [==] [===============] + */ + public function diff_with_one_period_within() + { + $a = Period::make('2018-01-01', '2018-01-31'); + $b = Period::make('2018-01-10', '2018-01-15'); + + $diff = $a->diff($b); + + $this->assertCount(2, $diff); + } }