1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181:
<?php
namespace Apptus\ESales\Connector\Report;
use Apptus\ESales\Connector\Time\Weekday;
use Apptus\ESales\Connector\Time\Duration;
use Apptus\ESales\Connector\Time\Unit;
class TimelineType {
const HOUR_OF_DAY = 1;
const DAY_IN_WEEK = 2;
const DAY_OF_MONTH = 3;
const WEEK_OF_YEAR = 4;
const MONTH_OF_YEAR = 5;
const RESOLUTION = 6;
public static function hourOfDay() {
return new TimelineType(self::HOUR_OF_DAY, Duration::create(1, Unit::HOUR));
}
public static function dayInWeek() {
return new TimelineType(self::DAY_IN_WEEK, Duration::create(1, Unit::DAY));
}
public static function dayOfMonth() {
return new TimelineType(self::DAY_OF_MONTH, Duration::create(1, Unit::DAY));
}
public static function weekOfYear() {
return new TimelineType(self::WEEK_OF_YEAR, Duration::create(1, Unit::WEEK));
}
public static function monthOfYear() {
return new TimelineType(self::MONTH_OF_YEAR, Duration::create(1, Unit::MONTH));
}
public static function parse($timelineType) {
switch (strtoupper($timelineType)) {
case 'HOUR_OF_DAY':
return self::hourOfDay();
case 'DAY_IN_WEEK':
return self::dayInWeek();
case 'DAY_OF_MONTH':
return self::dayOfMonth();
case 'WEEK_OF_YEAR':
return self::weekOfYear();
case 'MONTH_OF_YEAR':
return self::monthOfYear();
}
try {
$d = Duration::parse($timelineType);
} catch (\InvalidArgumentException $iae) {
throw new \InvalidArgumentException('Timeline type not supported: ' . $timelineType, null, $iae);
}
return new TimelineType(self::RESOLUTION, $d);
}
public static function create(\DateInterval $resolution) {
return new TimelineType(self::RESOLUTION, $resolution);
}
private $val;
private $resolution;
private function __construct($val, Duration $resolution = null) {
$this->val = $val;
$this->resolution = $resolution;
}
public function slot(\DateTime $dt) {
switch ($this->val) {
case self::HOUR_OF_DAY:
return $dt->format('H:00');
case self::DAY_IN_WEEK:
return $dt->format('l');
case self::DAY_OF_MONTH:
return $dt->format('j');
case self::WEEK_OF_YEAR:
return $dt->format('W');
case self::MONTH_OF_YEAR:
return $dt->format('M');
case self::RESOLUTION:
return Unit::displayName($this->resolution->precision(), $dt);
}
return null;
}
public function resolution() {
return $this->resolution;
}
public function allAvailable() {
$result = array ();
switch ($this->val) {
case self::HOUR_OF_DAY:
for ($i = 0; $i < 24; $i++) {
$result[] = sprintf('%02d:00', $i);
}
return $result;
case self::DAY_IN_WEEK:
for ($i = 1; $i <= 7; $i++) {
$result[] = Weekday::fromNumber($i);
}
return $result;
case self::DAY_OF_MONTH:
for ($i = 1; $i <= 31; $i++) {
$result[] = $i;
}
return $result;
case self::WEEK_OF_YEAR:
for ($i = 1; $i <= 53; $i++) {
$result[] = $i;
}
return $result;
case self::MONTH_OF_YEAR:
for ($i = 1; $i <= 12; $i++) {
$dt = new \DateTime(sprintf('2012-%02d-01', $i));
$result[] = $dt->format('M');
}
return $result;
case self::RESOLUTION:
return null;
}
return null;
}
public function __toString() {
switch ($this->val) {
case self::HOUR_OF_DAY:
return 'HOUR_OF_DAY';
case self::DAY_IN_WEEK:
return 'DAY_IN_WEEK';
case self::DAY_OF_MONTH:
return 'DAY_OF_MONTH';
case self::RESOLUTION:
return (string) $this->resolution;
}
return '';
}
}