| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Palmtree\Chrono; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Palmtree\Chrono\Option\Comparision; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | abstract class DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |     /** @var \DateTime */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $dateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 16 |  |     public function __construct(string $time = 'now', $timezone = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 | 16 |  |         if (is_string($timezone)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |             $timezone = new \DateTimeZone($timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 16 |  |         $this->dateTime = new \DateTime($time, $timezone); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 16 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     abstract protected function getFormatFromTimePrecision(?string $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     abstract protected function getDateInterval(int $value, string $period): \DateInterval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 16 |  |     public function format(string $format): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 16 |  |         return $this->dateTime->format($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 6 |  |     public function isSame(DateTime $date, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 6 |  |         return $this->compareTo($date, Comparision::EQUAL_TO, $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |     public function isBefore(DateTime $date, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |         return $this->compareTo($date, Comparision::LESS_THAN, $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function isSameOrBefore(DateTime $date, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         return $this->compareTo($date, Comparision::LESS_THAN_OR_EQUAL_TO, $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |     public function isAfter(DateTime $date, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |         return $this->compareTo($date, Comparision::GREATER_THAN, $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function isSameOrAfter(DateTime $date, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $this->compareTo($date, Comparision::GREATER_THAN_OR_EQUAL_TO, $precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |     public function add(int $value, string $period): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 2 |  |         $this->dateTime->add($this->getDateInterval($value, $period)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 2 |  |     public function subtract(int $value, string $period): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 2 |  |         $this->dateTime->sub($this->getDateInterval($value, $period)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     public function toDateTime(): \DateTime | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         return clone $this->dateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |     public static function min(...$dates): ?DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return \array_reduce($dates, function (?DateTime $carry, DateTime $dateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |             if (!$carry || $dateTime->isBefore($carry)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |                 $carry = $dateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |             return $carry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 2 |  |     public static function max(...$dates): ?DateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         return \array_reduce($dates, function (?DateTime $carry, DateTime $dateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 2 |  |             if (!$carry || $dateTime->isAfter($carry)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |                 $carry = $dateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |             return $carry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 12 |  |     private function compareTo(DateTime $date, string $operator, ?string $precision = null): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 12 |  |         $operators = Comparision::toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 12 |  |         if (!in_array($operator, $operators)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             $operators = implode("','", $operators); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             throw new \InvalidArgumentException("Operator must be one of '$operators'. $operator given"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 12 |  |         $format = $this->getFormatFromTimePrecision($precision); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 12 |  |         $thisFormatted = (int)$this->format($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 12 |  |         $dateFormatted = (int)$date->format($format); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         switch ($operator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 12 |  |             case Comparision::LESS_THAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |                 $result = $thisFormatted < $dateFormatted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 3 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 9 |  |             case Comparision::GREATER_THAN: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 3 |  |                 $result = $thisFormatted > $dateFormatted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 3 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 6 |  |             case Comparision::LESS_THAN_OR_EQUAL_TO: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                 $result = $thisFormatted <= $dateFormatted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 6 |  |             case Comparision::GREATER_THAN_OR_EQUAL_TO: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 $result = $thisFormatted >= $dateFormatted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 6 |  |             case Comparision::EQUAL_TO: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 6 |  |                 $result = $thisFormatted === $dateFormatted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 6 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 12 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 130 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |  |