rfv123

TimeSlot (SO Question 40505794)

Nov 21st, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.63 KB | None | 0 0
  1. // ---------------------------------------------------------------------------------------------
  2. interface ITimeSlot
  3. {
  4.     // properties for those that like them.
  5.    
  6.    
  7.     /**
  8.     * @property-read integer $startTime When the timeslot begins in 'epoch' seconds  
  9.     */
  10.  
  11.     /**
  12.     * @property-read integer $endTime When the timeslot begins in 'epoch' seconds  
  13.     */
  14.    
  15.     /**
  16.     * @property-read integer $durection Interval between start and end  
  17.     */
  18.  
  19.     /**
  20.     * @property-read boolean $isRequired This timeslot must always be included in any valid list  
  21.     */
  22.  
  23.     /**
  24.     * @property-read DateTime $startDate When the timeslot starts as a DateTime object  
  25.     */
  26.  
  27.     /**
  28.     * @property-read DateTime $endDate When the timeslot ends as a DateTime  
  29.     */
  30.  
  31.     /**
  32.     * @property-read string $key A string that represents this timeslot and can be ordered
  33.     */
  34.    
  35.    
  36.     // getters for those that like them
  37.    
  38.     /**
  39.      * StartDate
  40.      *    
  41.      * @return DateTime      
  42.      */        
  43.     public function getStartDate();
  44.  
  45.     /**
  46.      * EndDate
  47.      *    
  48.      * @return DateTime      
  49.      */        
  50.     public function getEndDate();
  51.    
  52.     /**
  53.      * StartTime
  54.      *    
  55.      * @return integer  - TimeStamp (epoch seconds)    
  56.      */        
  57.     public function getStartTime();
  58.    
  59.     /**
  60.      * EndTime  
  61.      *    
  62.      * @return integer  - TimeStamp (epoch seconds)    
  63.      */        
  64.     public function getEndTime();
  65.  
  66.     /**
  67.      * Duration between StartTime and EndTime
  68.      *    
  69.      * @return integer  - TimeStamp (epoch seconds)    
  70.      */        
  71.     public function getDuration();
  72.    
  73.     /**
  74.      * isRequired - whether this timeslot MUST be in the final list
  75.      *
  76.      * :return boolean          
  77.      */
  78.      public function isRequired();        
  79.  
  80.     /**
  81.      * Generate a string key for use when storing in arrays for quick lookup
  82.      *
  83.      * It is just the start and end time as strings with a '|' as a separator
  84.      *
  85.      * @return string
  86.      */
  87.      public function getKey();          
  88. }
  89.  
  90. // ---------------------------------------------------------------------------------------------
  91.  
  92. class TimeSlot extends \ArrayObject  implements ITimeSlot
  93. {
  94.     public static function newFromArray($when)
  95.     {
  96.         $reqdAttrs =  array('startDate' => "", "duration" => 0, "isRequired" => false);
  97.         $w = array_merge($reqdAttrs, $when);
  98.         $p = array_diff($when, $reqdAttrs);                  
  99.                          
  100.         unset($p['startDate']);                    
  101.         return self::newTimeSlot($w['startDate'], $w['duration'],
  102.                                  $w['isRequired'], $p);      
  103.     }
  104.  
  105.     public static function newFromObject($when)
  106.     {
  107.         $reqdAttrs =  array('startDate' => "", "duration" => 0, "isRequired" => false);
  108.        
  109.         $w = array_merge($reqdAttrs, get_object_vars($when));
  110.         $p = array_diff(get_object_vars($when), $reqdAttrs);                  
  111.                            
  112.         unset($p['startDate']);                    
  113.         return self::newTimeSlot($w['startDate'], $w['duration'],
  114.                                  $w['isRequired'], $p);      
  115.     }
  116.  
  117.  
  118.     public static function newTimeSlot($start, $duration, $isRequired, $properties)
  119.     {
  120.         if (! $start instanceof \DateTime) {
  121.            $start = new \DateTime($start);
  122.         }      
  123.  
  124.         return new self($start, $duration, $isRequired, $properties);        
  125.     }
  126.  
  127.  
  128.     // use the 'named' constructors - it is easier
  129.     public function __construct(\DateTime $startDate,
  130.                                  $duration,
  131.                                  $isRequired = false,
  132.                                  $properties = array())
  133.     {
  134.  
  135.             unset($properties['startDate']); // confuses things ;-/
  136.             $properties['startTime'] = (int) $startDate->format("U");
  137.             $properties['duration'] = (int) $duration;
  138.             $properties['endTime']  = $properties['startTime'] + $properties['duration'];
  139.             $properties['isRequired'] = $isRequired;
  140.             parent::__construct($properties,
  141.                                 \ArrayObject::ARRAY_AS_PROPS); // treat properties as array entries
  142.     }
  143.  
  144.     /**
  145.      * if start is after other end OR end is before other start then they don't overlap
  146.      *
  147.      * Easiest way is to check that they don't overlap and reverse the result
  148.      */
  149.     public function isOverlap(ITimeSlot $when)
  150.     {
  151.         return ! (    $this['endTime'] <= $when['startTime']
  152.                    || $this['startTime'] >= $when['endTime']);
  153.     }
  154.  
  155.  
  156.     /**
  157.     * Is the provided timeslot completly inside the timeslot
  158.     * Used for checking if timeslot, is inside a range, which is also a timeslot.  
  159.     *
  160.     * @param ITimeslot $when
  161.     *
  162.     * @return boolean
  163.     */
  164.     public function isInside(ITimeSlot $when)
  165.     {
  166.         return   (    $when['startTime'] >= $this['startTime']
  167.                    && $when['endTime'] <= $this['endTime']);
  168.     }
  169.  
  170.  
  171.     /**
  172.      * get StartDate
  173.      *    
  174.      * @return DateTime      
  175.      */        
  176.     public function getStartDate()
  177.     {
  178.         return new \DateTime('@'. $this->startTime);
  179.     }
  180.  
  181.     /**
  182.      * get EndDate
  183.      *    
  184.      * @return DateTime      
  185.      */        
  186.     public function getEndDate()
  187.     {
  188.         return new \DateTime('@'. $this->endTime);
  189.     }
  190.    
  191.     /**
  192.      * StartTime - TimeStamp
  193.      *    
  194.      * @return integer  - TimeStamp (epoch seconds)    
  195.      */        
  196.     public function getStartTime()
  197.     {
  198.         return $this->endTime;
  199.     }
  200.    
  201.     /**
  202.      * EndTime = TimeStamp  
  203.      *    
  204.      * @return integer  - TimeStamp (epoch seconds)    
  205.      */        
  206.     public function getEndTime()
  207.     {
  208.         return $this->endTime;
  209.     }
  210.  
  211.     /**
  212.      * Duration between StartTime and EndTime
  213.      *    
  214.      * @return integer  - TimeStamp (epoch seconds)    
  215.      */        
  216.     public function getDuration()
  217.     {
  218.         return $this->duration;
  219.     }
  220.          
  221.     /**
  222.      * isRequired - whether this timeslot MUST be in the final list
  223.      *
  224.      * :return boolean          
  225.      */
  226.      public function isRequired()
  227.      {
  228.           return $this->isRequired;
  229.      }        
  230.    
  231.      
  232.      public function setIsRequired($required = true)
  233.      {
  234.           $this->isRequired = $required;
  235.      }
  236.    
  237.      /**
  238.      * A key that represents this timeslot and can be ordered when comparing timeslots
  239.      *
  240.      * @return string
  241.      */
  242.      public function getKey()
  243.      {
  244.             return $this->startTime .'|'. $this->endTime;
  245.      }
  246.      
  247.      /**
  248.      * Allow you to access any value as a property
  249.      *
  250.      * @param string $name
  251.      *
  252.      * @return mixed
  253.      */
  254.      public function offsetGet($name)
  255.      {
  256.          static $getters = 'key, startDate, endDate';
  257.          
  258.          if (stripos($getters, $name) !== false) {
  259.              $name = 'get'. ucfirst($name);
  260.              return $this->$name();
  261.          }
  262.          
  263.          return parent::offsetGet($name);
  264.      }    
  265.  
  266.      /**
  267.      * Set / Update any of *your* properties
  268.      *
  269.      * @param string $name
  270.      * @param mixed $value
  271.      *
  272.      * @return void  
  273.      */
  274.      public function offsetSet($name, $value)
  275.      {
  276.          static $readOnly = 'startTime, endTime, duration, key';
  277.          
  278.          if (stripos($readOnly, $name) !== false) {
  279.              return;
  280.          }
  281.          
  282.          parent::offsetSet($name, $value);
  283.      }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment