| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Vctls\IntervalGraph\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use DateInterval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Vctls\IntervalGraph\IntervalGraph; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Utility class for creating interval graphs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package Vctls\IntervalGraph | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      * Create an interval of dates from two integers and a value. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * @param $start | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @param $end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public static function intv(int $start, int $end, $value = null): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $startDate = DateTime::createFromFormat('Y-m-d', '2019-01-01') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ->add(new DateInterval("P{$start}D"))->setTime(0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $endDate = DateTime::createFromFormat('Y-m-d', '2019-01-01') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ->add(new DateInterval("P{$end}D"))->setTime(23, 59, 59); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return [$startDate, $endDate, $value]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Create an IntervalGraph for handling dates. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param $intervals | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return IntervalGraph | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public static function intvg($intervals = null): IntervalGraph | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $intvg = new IntervalGraph(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $intvg->getFlattener() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             ->setSubstractStep(static function (DateTime $bound) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 return (clone $bound)->sub(new DateInterval('PT1S')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             }) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ->setAddStep(static function (DateTime $bound) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 return (clone $bound)->add(new DateInterval('PT1S')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (isset($intervals)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $intvg->setIntervals($intervals); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return $intvg; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Generate a random date. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param int $min | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param int $max | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return DateTime | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public static function rdm($min = 1514764800, $max = 1577750400): ?DateTime | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |             return (new DateTime)->setTimestamp(random_int($min, $max)); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         } catch (Exception $e) { | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  |