Skip to content

Commit

Permalink
added exception and subBusinessDays method
Browse files Browse the repository at this point in the history
  • Loading branch information
royopa committed Jan 23, 2018
1 parent 0a66655 commit 487671b
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/BusinessDays/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ private function getFreeWeekDays()
*/
public function addBusinessDays($howManyDays)
{
if ($howManyDays < 1) {
throw new \InvalidArgumentException('The parameter $howManyDays must be greater than 0');
}

$iterator = 0;
while ($iterator < $howManyDays) {
$this->getDate()->modify('+1 day');
Expand All @@ -131,6 +135,29 @@ public function addBusinessDays($howManyDays)
return $this;
}

/**
* @param int $howManyDays
*
* @return $this
*/
public function subBusinessDays($howManyDays)
{
if ($howManyDays < 1) {
throw new \InvalidArgumentException('The parameter $howManyDays must be greater than 0');
}

$iterator = 0;
while ($iterator < $howManyDays) {
if ($this->isBusinessDay($this->getDate())) {
$iterator++;
}
if ($iterator < $howManyDays) { //Don`t modify the date if we are on the last iteration
$this->getDate()->modify('-1 day');
}
}
return $this;
}

/**
* @return \DateTime
*/
Expand Down

0 comments on commit 487671b

Please sign in to comment.