Skip to content

Commit 7ae63c4

Browse files
committed
Add Period::contains
1 parent d488bef commit 7ae63c4

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ All notable changes to `period` will be documented in this file
55
## 1.0.0 - 201X-XX-XX
66

77
- initial release
8+
9+
## 0.3.0 - 2018-11-30
10+
11+
- Add `Period::contains`
12+
13+
## 0.2.0 - 2018-11-27
14+
15+
- Initial dev release

src/Period.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ public function endsBefore(DateTimeInterface $date): bool
176176
return $this->end < $date;
177177
}
178178

179+
public function contains(DateTimeInterface $date): bool
180+
{
181+
if ($date < $this->start) {
182+
return false;
183+
}
184+
185+
if ($date > $this->end) {
186+
return false;
187+
}
188+
189+
return true;
190+
}
191+
179192
public function equals(Period $period): bool
180193
{
181194
if ($period->start->getTimestamp() !== $this->start->getTimestamp()) {

tests/PeriodCollectionTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Spatie\Tests\Period;
44

5+
use DateTimeImmutable;
56
use Spatie\Period\Period;
67
use PHPUnit\Framework\TestCase;
78
use Spatie\Period\PeriodCollection;
@@ -120,4 +121,24 @@ public function it_can_determine_the_gaps_of_a_collection()
120121
$this->assertTrue($gaps[1]->equals(Period::make('2018-01-16', '2018-01-19')));
121122
$this->assertTrue($gaps[2]->equals(Period::make('2018-01-26', '2018-01-29')));
122123
}
124+
125+
/**
126+
* @test
127+
*
128+
* A [===============]
129+
* B |
130+
* C |
131+
* D |
132+
*/
133+
public function it_can_determine_whether_a_period_has_a_date()
134+
{
135+
$period = Period::make('2018-01-01', '2018-01-31');
136+
137+
$this->assertTrue($period->contains(new DateTimeImmutable('2018-01-01')));
138+
$this->assertTrue($period->contains(new DateTimeImmutable('2018-01-31')));
139+
$this->assertTrue($period->contains(new DateTimeImmutable('2018-01-10')));
140+
141+
$this->assertFalse($period->contains(new DateTimeImmutable('2017-12-31')));
142+
$this->assertFalse($period->contains(new DateTimeImmutable('2018-02-01')));
143+
}
123144
}

0 commit comments

Comments
 (0)