Last active
December 30, 2015 08:49
-
-
Save trq/7805165 to your computer and use it in GitHub Desktop.
This file contains 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 CalDay | |
{ | |
protected $date; | |
protected $week; | |
protected $id; | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setDate(\DateTime $date) | |
{ | |
$this->date = $date; | |
return $this; | |
} | |
public function getDate() | |
{ | |
return $this->date; | |
} | |
public function setWeek(CalWeek $week) | |
{ | |
$this->week = $week; | |
return $this; | |
} | |
public function getWeek() | |
{ | |
return $this->week; | |
} | |
} | |
class CalWeek | |
{ | |
protected $days = []; | |
protected $month; | |
protected $id; | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setDay(CalDay $day) | |
{ | |
$this->days[] = $day; | |
return $this; | |
} | |
public function getDays() | |
{ | |
return $this->days; | |
} | |
public function setMonth(CalMonth $month) | |
{ | |
$this->month = $month; | |
return $this; | |
} | |
public function getMonth() | |
{ | |
return $this->month; | |
} | |
} | |
class CalMonth | |
{ | |
protected $weeks = []; | |
protected $quarter; | |
protected $id; | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setWeek(CalWeek $week) | |
{ | |
$this->weeks[] = $week; | |
return $this; | |
} | |
public function getWeeks() | |
{ | |
return $this->weeks; | |
} | |
public function setQuarter(CalQuarter $quarter) | |
{ | |
$this->quarter = $quarter; | |
return $this; | |
} | |
public function getQuarter() | |
{ | |
return $this->quarter; | |
} | |
} | |
class CalQuarter | |
{ | |
protected $months = []; | |
protected $year; | |
protected $id; | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setMonth(CalMonth $month) | |
{ | |
$this->months[] = $month; | |
return $this; | |
} | |
public function getMonths() | |
{ | |
return $this->months; | |
} | |
public function setYear(CalYear $year) | |
{ | |
$this->year = $year; | |
return $this; | |
} | |
public function getYear() | |
{ | |
return $this->year; | |
} | |
} | |
class CalYear | |
{ | |
protected $quarters = []; | |
protected $id; | |
public function setId($id) | |
{ | |
$this->id = $id; | |
return $this; | |
} | |
public function getId() | |
{ | |
return $this->id; | |
} | |
public function setQuarter(CalQuarter $quarter) | |
{ | |
$this->quarters[] = $quarter; | |
return $this; | |
} | |
public function getQuarters() | |
{ | |
return $this->quarters; | |
} | |
} | |
class Calendar | |
{ | |
protected $years = []; | |
public function __construct(\DateTime $startDate, $years = 1) | |
{ | |
$startDate->modify('-1 day'); | |
$records = []; | |
foreach (range(1, $years) as $year) { | |
$dayOfWeek = 0; | |
$dayOfMonth = 0; | |
$dayOfQuarter = 0; | |
$dayOfYear = 0; | |
$weekOfMonth = 1; | |
$weekOfQuarter = 1; | |
$weekOfYear = 1; | |
$monthOfYear = 1; | |
$monthOfQuarter = 1; | |
$quarterOfYear = 1; | |
foreach (range(1, 364) as $dayOfYear) { | |
++$dayOfMonth; | |
++$dayOfQuarter; | |
if ($dayOfWeek == 7) { | |
$dayOfWeek = 1; | |
++$weekOfQuarter; | |
++$weekOfMonth; | |
++$weekOfYear; | |
} else { | |
++$dayOfWeek; | |
} | |
if ($dayOfQuarter == 29 || $dayOfQuarter == 57 || $dayOfQuarter == 92) { | |
$dayOfMonth = 1; | |
$weekOfMonth = 1; | |
++$monthOfQuarter; | |
++$monthOfYear; | |
} | |
if ($dayOfQuarter > 91) { | |
$dayOfQuarter = 1; | |
$weekOfQuarter = 1; | |
$monthOfQuarter = 1; | |
++$quarterOfYear; | |
} | |
$records[$year][$quarterOfYear][$monthOfQuarter][$weekOfQuarter][$dayOfWeek] = clone $startDate->modify('+1 day'); | |
} | |
} | |
foreach ($records as $year => $years) { | |
$year = (new CalYear)->setId($year); | |
foreach ($years as $quarter => $months) { | |
$quarter = (new CalQuarter)->setId($quarter); | |
foreach ($months as $month => $weeks) { | |
$month = (new CalMonth)->setId($month); | |
foreach ($weeks as $week => $days) { | |
$week = (new CalWeek)->setId($week); | |
foreach ($days as $day => $date) { | |
$day = (new CalDay)->setId($day)->setDate($date); | |
$week->setDay($day); | |
} | |
$month->setWeek($week); | |
} | |
$quarter->setMonth($month); | |
} | |
$this->years[] = $year->setQuarter($quarter); | |
} | |
} | |
} | |
public function fetch() | |
{ | |
return $this->years; | |
} | |
public function __toString() | |
{ | |
return serialize($this->years); | |
} | |
} | |
var_dump((new Calendar(new \DateTime('2013-07-01'), 3))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment