Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ---------------------------------------------------------------------------------------------
- interface ITimeSlot
- {
- // properties for those that like them.
- /**
- * @property-read integer $startTime When the timeslot begins in 'epoch' seconds
- */
- /**
- * @property-read integer $endTime When the timeslot begins in 'epoch' seconds
- */
- /**
- * @property-read integer $durection Interval between start and end
- */
- /**
- * @property-read boolean $isRequired This timeslot must always be included in any valid list
- */
- /**
- * @property-read DateTime $startDate When the timeslot starts as a DateTime object
- */
- /**
- * @property-read DateTime $endDate When the timeslot ends as a DateTime
- */
- /**
- * @property-read string $key A string that represents this timeslot and can be ordered
- */
- // getters for those that like them
- /**
- * StartDate
- *
- * @return DateTime
- */
- public function getStartDate();
- /**
- * EndDate
- *
- * @return DateTime
- */
- public function getEndDate();
- /**
- * StartTime
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getStartTime();
- /**
- * EndTime
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getEndTime();
- /**
- * Duration between StartTime and EndTime
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getDuration();
- /**
- * isRequired - whether this timeslot MUST be in the final list
- *
- * :return boolean
- */
- public function isRequired();
- /**
- * Generate a string key for use when storing in arrays for quick lookup
- *
- * It is just the start and end time as strings with a '|' as a separator
- *
- * @return string
- */
- public function getKey();
- }
- // ---------------------------------------------------------------------------------------------
- class TimeSlot extends \ArrayObject implements ITimeSlot
- {
- public static function newFromArray($when)
- {
- $reqdAttrs = array('startDate' => "", "duration" => 0, "isRequired" => false);
- $w = array_merge($reqdAttrs, $when);
- $p = array_diff($when, $reqdAttrs);
- unset($p['startDate']);
- return self::newTimeSlot($w['startDate'], $w['duration'],
- $w['isRequired'], $p);
- }
- public static function newFromObject($when)
- {
- $reqdAttrs = array('startDate' => "", "duration" => 0, "isRequired" => false);
- $w = array_merge($reqdAttrs, get_object_vars($when));
- $p = array_diff(get_object_vars($when), $reqdAttrs);
- unset($p['startDate']);
- return self::newTimeSlot($w['startDate'], $w['duration'],
- $w['isRequired'], $p);
- }
- public static function newTimeSlot($start, $duration, $isRequired, $properties)
- {
- if (! $start instanceof \DateTime) {
- $start = new \DateTime($start);
- }
- return new self($start, $duration, $isRequired, $properties);
- }
- // use the 'named' constructors - it is easier
- public function __construct(\DateTime $startDate,
- $duration,
- $isRequired = false,
- $properties = array())
- {
- unset($properties['startDate']); // confuses things ;-/
- $properties['startTime'] = (int) $startDate->format("U");
- $properties['duration'] = (int) $duration;
- $properties['endTime'] = $properties['startTime'] + $properties['duration'];
- $properties['isRequired'] = $isRequired;
- parent::__construct($properties,
- \ArrayObject::ARRAY_AS_PROPS); // treat properties as array entries
- }
- /**
- * if start is after other end OR end is before other start then they don't overlap
- *
- * Easiest way is to check that they don't overlap and reverse the result
- */
- public function isOverlap(ITimeSlot $when)
- {
- return ! ( $this['endTime'] <= $when['startTime']
- || $this['startTime'] >= $when['endTime']);
- }
- /**
- * Is the provided timeslot completly inside the timeslot
- * Used for checking if timeslot, is inside a range, which is also a timeslot.
- *
- * @param ITimeslot $when
- *
- * @return boolean
- */
- public function isInside(ITimeSlot $when)
- {
- return ( $when['startTime'] >= $this['startTime']
- && $when['endTime'] <= $this['endTime']);
- }
- /**
- * get StartDate
- *
- * @return DateTime
- */
- public function getStartDate()
- {
- return new \DateTime('@'. $this->startTime);
- }
- /**
- * get EndDate
- *
- * @return DateTime
- */
- public function getEndDate()
- {
- return new \DateTime('@'. $this->endTime);
- }
- /**
- * StartTime - TimeStamp
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getStartTime()
- {
- return $this->endTime;
- }
- /**
- * EndTime = TimeStamp
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getEndTime()
- {
- return $this->endTime;
- }
- /**
- * Duration between StartTime and EndTime
- *
- * @return integer - TimeStamp (epoch seconds)
- */
- public function getDuration()
- {
- return $this->duration;
- }
- /**
- * isRequired - whether this timeslot MUST be in the final list
- *
- * :return boolean
- */
- public function isRequired()
- {
- return $this->isRequired;
- }
- public function setIsRequired($required = true)
- {
- $this->isRequired = $required;
- }
- /**
- * A key that represents this timeslot and can be ordered when comparing timeslots
- *
- * @return string
- */
- public function getKey()
- {
- return $this->startTime .'|'. $this->endTime;
- }
- /**
- * Allow you to access any value as a property
- *
- * @param string $name
- *
- * @return mixed
- */
- public function offsetGet($name)
- {
- static $getters = 'key, startDate, endDate';
- if (stripos($getters, $name) !== false) {
- $name = 'get'. ucfirst($name);
- return $this->$name();
- }
- return parent::offsetGet($name);
- }
- /**
- * Set / Update any of *your* properties
- *
- * @param string $name
- * @param mixed $value
- *
- * @return void
- */
- public function offsetSet($name, $value)
- {
- static $readOnly = 'startTime, endTime, duration, key';
- if (stripos($readOnly, $name) !== false) {
- return;
- }
- parent::offsetSet($name, $value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment