diff --git a/src/PeriodTraits/PeriodOperations.php b/src/PeriodTraits/PeriodOperations.php index 292abad..a3ccd84 100644 --- a/src/PeriodTraits/PeriodOperations.php +++ b/src/PeriodTraits/PeriodOperations.php @@ -38,7 +38,9 @@ public function gap(Period $period): ?static public function overlap(Period ...$others): ?static { - if (count($others) > 1) { + if (count($others) === 0) { + return null; + } else if (count($others) > 1) { return $this->overlapAll(...$others); } else { $other = $others[0]; @@ -115,7 +117,9 @@ public function overlapAny(Period ...$others): PeriodCollection */ public function subtract(Period ...$others): PeriodCollection { - if (count($others) > 1) { + if (count($others) === 0) { + return PeriodCollection::make($this); + } else if (count($others) > 1) { return $this->subtractAll(...$others); } else { $other = $others[0]; diff --git a/tests/Operations/OverlapTest.php b/tests/Operations/OverlapTest.php index d65e770..1eb3f91 100644 --- a/tests/Operations/OverlapTest.php +++ b/tests/Operations/OverlapTest.php @@ -5,6 +5,7 @@ use Generator; use PHPUnit\Framework\TestCase; use Spatie\Period\Period; +use Spatie\Period\PeriodCollection; class OverlapTest extends TestCase { @@ -178,4 +179,15 @@ public function noOverlappingDates() */ yield [Period::make('2018-02-01', '2018-02-28'), Period::make('2018-01-01', '2018-01-31')]; } + + /** @test */ + public function passing_empty_period_collection_returns_null() + { + $current = Period::make('2018-01-01', '2018-01-31'); + $emptyCollection = new PeriodCollection; + + $diff = $current->overlap(... $emptyCollection); + + $this->assertNull($diff); + } } diff --git a/tests/Operations/SubtractTest.php b/tests/Operations/SubtractTest.php index 3789786..616a2e3 100644 --- a/tests/Operations/SubtractTest.php +++ b/tests/Operations/SubtractTest.php @@ -4,6 +4,7 @@ use PHPUnit\Framework\TestCase; use Spatie\Period\Period; +use Spatie\Period\PeriodCollection; class SubtractTest extends TestCase { @@ -223,4 +224,17 @@ public function it_can_determine_multiple_diffs_for_sure() $this->assertTrue($diff[1]->equals(Period::make('2018-01-11', '2018-01-14'))); $this->assertTrue($diff[2]->equals(Period::make('2018-01-21', '2018-01-31'))); } + + /** @test */ + public function passing_empty_period_collection_returns_same_period_within_collection() + { + $current = Period::make('2018-01-01', '2018-01-31'); + $emptyCollection = new PeriodCollection; + + $diff = $current->subtract(... $emptyCollection); + + $this->assertInstanceOf(PeriodCollection::class, $diff); + $this->assertCount(1, $diff); + $this->assertTrue($diff[0]->equals($current)); + } }