Last active
December 30, 2015 10:18
-
-
Save trq/7814691 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; | |
} | |
} | |
abstract class CalBuilderAbstract | |
{ | |
protected $startDate; | |
protected $years; | |
protected $monthSplit; | |
protected $debug = false; | |
protected $results = []; | |
public function __construct(\DateTime $startDate, $years = 1, $monthSplit = [4, 4, 5]) | |
{ | |
$this->startDate = $startDate; | |
$this->years = $years; | |
$this->monthSplit = $monthSplit; | |
} | |
abstract public function build(); | |
public function setDebug($debug = true) | |
{ | |
$this->debug = $debug; | |
return $this; | |
} | |
public function fetch() | |
{ | |
return $this->results; | |
} | |
public function __toString() | |
{ | |
$this->build(); | |
return serialize($this->results); | |
} | |
} | |
class CalBuilder extends CalBuilderAbstract | |
{ | |
public function build() | |
{ | |
$this->startDate->modify('-1 day'); | |
$records = []; | |
if ($this->debug) { | |
printf( | |
"%'-55s%-3s %s %s %s %s %s %s %s %s %s %s %s\n%'-55s", | |
"\n", "yr", "qoy", "moy", "moq", "woy", "woq", "wom", "doy", "doq", "dom", "dow", "date", "\n" | |
); | |
} | |
foreach (range(1, $this->years) as $year) { | |
$dayOfWeek = 0; | |
$dayOfMonth = 0; | |
$dayOfQuarter = 0; | |
$dayOfYear = 0; | |
$weekOfMonth = 1; | |
$weekOfQuarter = 1; | |
$weekOfYear = 1; | |
$monthOfYear = 1; | |
$monthOfQuarter = 1; | |
$quarterOfYear = 1; | |
$totalDaysInQuarter = 0; | |
$monthSplitDays = []; | |
foreach ($this->monthSplit as $weeks) { | |
$totalDaysInQuarter += ($weeks*7); | |
$monthSplitDays[] = $totalDaysInQuarter+1; | |
} | |
foreach (range(1, 364) as $dayOfYear) { | |
++$dayOfMonth; | |
++$dayOfQuarter; | |
if ($dayOfWeek == 7) { | |
$dayOfWeek = 1; | |
++$weekOfQuarter; | |
++$weekOfMonth; | |
++$weekOfYear; | |
} else { | |
++$dayOfWeek; | |
} | |
if (in_array($dayOfQuarter, $monthSplitDays)) { | |
if ($this->debug) { | |
printf( | |
"%'-55s%-3s %s %s %s %s %s %s %s %s %s %s %s\n%'-55s", | |
"\n", "yr", "qoy", "moy", "moq", "woy", "woq", "wom", "doy", "doq", "dom", "dow", "date", "\n" | |
); | |
} | |
$dayOfMonth = 1; | |
$weekOfMonth = 1; | |
++$monthOfQuarter; | |
++$monthOfYear; | |
} | |
if ($dayOfQuarter > $totalDaysInQuarter) { | |
$dayOfQuarter = 1; | |
$weekOfQuarter = 1; | |
$monthOfQuarter = 1; | |
++$quarterOfYear; | |
} | |
$date = clone $this->startDate->modify('+1 day'); | |
if ($this->debug) { | |
printf( | |
"%-3s %-3s %-3s %-3s %-3s %-3s %-3s %-3s %-3s %-3s %-3s %-3s\n", | |
$year, | |
$quarterOfYear, | |
$monthOfYear , $monthOfQuarter, | |
$weekOfYear , $weekOfQuarter , $weekOfMonth, | |
$dayOfYear , $dayOfQuarter , $dayOfMonth , $dayOfWeek, $date->format('Y-m-d') | |
); | |
} | |
$records[$year][$quarterOfYear][$monthOfQuarter][$weekOfQuarter][$dayOfWeek] = $date; | |
} | |
} | |
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->results[] = $year->setQuarter($quarter); | |
} | |
} | |
} | |
} | |
(new CalBuilder(new \DateTime('2013-07-01'), 1))->setDebug()->build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment