Skip to content

Commit

Permalink
Merge pull request #74 from morrislaptop/renew
Browse files Browse the repository at this point in the history
add renew function
  • Loading branch information
brendt committed Feb 24, 2021
2 parents 577c10f + fd63855 commit b70455a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to `period` will be documented in this file

## 1.6.0 - 2021-02-24

- Add `Period::renew` (#74)

## 1.5.3 - 2020-12-03

- PHP8 compatibility
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ $period->getEnd(): DateTimeImmutable
$period->getEndIncluded(): DateTimeImmutable
```

#### Renewing a Period

$period = Period::make('2018-01-01', '2018-01-15');
$renewal = $period->renew(); // '2018-01-16' to '2018-01-31'

#### Comparisons

```php
Expand Down
10 changes: 10 additions & 0 deletions src/Period.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ public static function make(
);
}

public function renew(): Period
{
$length = $this->includedStart->diff($this->includedEnd);

$start = $this->includedEnd->add($this->interval);
$end = $start->add($length);

return new self($start, $end, $this->precisionMask, $this->boundaryExclusionMask);
}

public function startIncluded(): bool
{
return ! $this->startExcluded();
Expand Down
11 changes: 11 additions & 0 deletions tests/PeriodTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public function it_can_determine_the_period_length()
$this->assertEquals(15, $period->length());
}

/** @test */
public function it_can_renew_a_period()
{
$period = Period::make('2018-01-01', '2018-01-15');

$rewewal = $period->renew();

$this->assertTrue($rewewal->touchesWith($period));
$this->assertEquals($rewewal->length(), $period->length());
}

/**
* @test
* @dataProvider overlappingDates
Expand Down

0 comments on commit b70455a

Please sign in to comment.