-
-
Save pocky/ecdbfb90727a63d811ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Class Holidays | |
*/ | |
class Holidays | |
{ | |
/** | |
* @var array | |
*/ | |
protected $days = []; | |
/** | |
* @param $day | |
*/ | |
public function addDay($day) | |
{ | |
$this->days[] = $day; | |
} | |
/** | |
* @param array $days | |
*/ | |
public function addDays(array $days) | |
{ | |
$this->days = array_merge($this->days, $days); | |
} | |
/** | |
* @return array | |
*/ | |
public function getDays() | |
{ | |
return $this->days; | |
} | |
} | |
/** | |
* Class Workingdays | |
*/ | |
class Workingdays | |
{ | |
/** | |
* @var array | |
*/ | |
protected $days = []; | |
/** | |
* @param $day | |
*/ | |
public function addDay($day) | |
{ | |
$this->days[] = $day; | |
} | |
/** | |
* @param array $days | |
*/ | |
public function addDays(array $days) | |
{ | |
$this->days = array_merge($this->days, $days); | |
} | |
/** | |
* @return array | |
*/ | |
public function getDays() | |
{ | |
return $this->days; | |
} | |
} | |
/** | |
* Class HolidayManager | |
*/ | |
class HolidayManager | |
{ | |
/** | |
* @var DateTime | |
*/ | |
protected $startDate; | |
/** | |
* @var Holidays | |
*/ | |
protected $holidays; | |
/** | |
* @var Workingdays | |
*/ | |
protected $workingdays; | |
/** | |
* @var | |
*/ | |
protected $endDate; | |
/** | |
* @param DateTime $startDate | |
* @param Holidays $holidays | |
* @param Workingdays $workingdays | |
*/ | |
public function __construct(\DateTime $startDate, Holidays $holidays, Workingdays $workingdays) | |
{ | |
$this->startDate = $startDate; | |
$this->holidays = $holidays; | |
$this->workingdays = $workingdays; | |
} | |
/** | |
* @param $delay | |
* @return DateTime | |
*/ | |
public function findNextWorkingDay($delay) | |
{ | |
$day = 1; | |
while($delay >= $day) { | |
$currentDate = $this->addDay($this->startDate); | |
while ($currentDate == $this->startDate) { | |
$currentDate = $this->addDay($this->startDate->add(new \DateInterval('P1D'))); | |
} | |
$day++; | |
} | |
return $this->startDate; | |
} | |
/** | |
* @param $currentDate | |
* @return mixed | |
*/ | |
protected function addDay($currentDate) | |
{ | |
if (in_array($currentDate->format('Y-m-d'), $this->holidays->getDays())) { | |
$this->startDate->add(new \DateInterval('P1D')); | |
} | |
if (in_array($currentDate->format('*-m-d'), $this->holidays->getDays())) { | |
$this->startDate->add(new \DateInterval('P1D')); | |
} | |
if (!in_array($currentDate->format('N'), $this->workingdays->getDays())) { | |
return $currentDate; | |
} | |
$this->startDate->add(new \DateInterval('P1D')); | |
} | |
} | |
$holidays = new Holidays; | |
$holidays->addDays(['2015-12-24', '*-12-25']); | |
$holidays->addDay('*-01-01'); | |
$workingdays = new Workingdays; | |
$workingdays->addDays(['1', '2']); | |
$workingdays->addDay('3'); | |
$workingdays->addDay('4'); | |
$workingdays->addDay('5'); | |
$holidayManager = new HolidayManager(new \DateTime('2015-12-01'), $holidays, $workingdays); | |
$nextBusinessDay = $holidayManager->findNextWorkingDay(45); | |
var_dump($nextBusinessDay); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment