diff --git a/CHANGELOG.md b/CHANGELOG.md index 4768ad1..e310e46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,3 +5,11 @@ All notable changes to `period` will be documented in this file ## 1.0.0 - 201X-XX-XX - initial release + +## 0.3.0 - 2018-11-30 + +- Add `Period::contains` + +## 0.2.0 - 2018-11-27 + +- Initial dev release diff --git a/src/Period.php b/src/Period.php index ac80649..91c5884 100644 --- a/src/Period.php +++ b/src/Period.php @@ -176,6 +176,19 @@ public function endsBefore(DateTimeInterface $date): bool return $this->end < $date; } + public function contains(DateTimeInterface $date): bool + { + if ($date < $this->start) { + return false; + } + + if ($date > $this->end) { + return false; + } + + return true; + } + public function equals(Period $period): bool { if ($period->start->getTimestamp() !== $this->start->getTimestamp()) { diff --git a/tests/PeriodCollectionTest.php b/tests/PeriodCollectionTest.php index 0eba6ad..9f97c2b 100644 --- a/tests/PeriodCollectionTest.php +++ b/tests/PeriodCollectionTest.php @@ -2,6 +2,7 @@ namespace Spatie\Tests\Period; +use DateTimeImmutable; use Spatie\Period\Period; use PHPUnit\Framework\TestCase; use Spatie\Period\PeriodCollection; @@ -120,4 +121,24 @@ public function it_can_determine_the_gaps_of_a_collection() $this->assertTrue($gaps[1]->equals(Period::make('2018-01-16', '2018-01-19'))); $this->assertTrue($gaps[2]->equals(Period::make('2018-01-26', '2018-01-29'))); } + + /** + * @test + * + * A [===============] + * B | + * C | + * D | + */ + public function it_can_determine_whether_a_period_has_a_date() + { + $period = Period::make('2018-01-01', '2018-01-31'); + + $this->assertTrue($period->contains(new DateTimeImmutable('2018-01-01'))); + $this->assertTrue($period->contains(new DateTimeImmutable('2018-01-31'))); + $this->assertTrue($period->contains(new DateTimeImmutable('2018-01-10'))); + + $this->assertFalse($period->contains(new DateTimeImmutable('2017-12-31'))); + $this->assertFalse($period->contains(new DateTimeImmutable('2018-02-01'))); + } }