Skip to content

Commit

Permalink
Add Period::contains
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed Nov 30, 2018
1 parent d488bef commit 7ae63c4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
21 changes: 21 additions & 0 deletions tests/PeriodCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Tests\Period;

use DateTimeImmutable;
use Spatie\Period\Period;
use PHPUnit\Framework\TestCase;
use Spatie\Period\PeriodCollection;
Expand Down Expand Up @@ -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')));
}
}

0 comments on commit 7ae63c4

Please sign in to comment.