| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace morphos; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use DateInterval; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use DateTime; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use RuntimeException; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  | abstract class TimeSpeller | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |     const YEAR = 'year'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |     const MONTH = 'month'; | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |     const DAY = 'day'; | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |     const HOUR = 'hour'; | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |     const MINUTE = 'minute'; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |     const SECOND = 'second'; | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     const AGO = 'ago'; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     const IN = 'in'; | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     const AND_WORD = 'and'; | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     const JUST_NOW = 'just now'; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     const DIRECTION = 1; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     const SEPARATE = 2; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      * @abstract | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |      * @param int $count | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @param string $unit | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public static function spellUnit($count, $unit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         throw new RuntimeException('Not implemented'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      * @param DateInterval $interval | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * @param int $options | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @param int $limit | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 23 |  |     public static function spellInterval(DateInterval $interval, $options = 0, $limit = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 23 |  |         $parts = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 23 |  |         $k = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         foreach ([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 23 |  |             'y' => static::YEAR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 23 |  |             'm' => static::MONTH, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 23 |  |             'd' => static::DAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 23 |  |             'h' => static::HOUR, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 23 |  |             'i' => static::MINUTE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 23 |  |             's' => static::SECOND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         ] as $interval_field => $unit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 23 |  |             if ($interval->{$interval_field} > 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 23 |  |                 if($limit > 0 && $k >= $limit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 6 |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 23 |  |                 $parts[] = static::spellUnit($interval->{$interval_field}, $unit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 23 |  |                 $k++; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 23 |  |         if (empty($parts)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return static::JUST_NOW; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 23 |  |         if ($options & static::SEPARATE && count($parts) > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 7 |  |             $last_part = array_pop($parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 7 |  |             $spelled = implode(', ', $parts).' '.static::AND_WORD.' '.$last_part; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 16 |  |             $spelled = implode(' ', $parts); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 23 |  |         if ($options & static::DIRECTION) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |             if ($interval->invert) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                 $spelled = static::IN.' '.$spelled; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 5 |  |                 $spelled .= ' '.static::AGO; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 23 |  |         return $spelled; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param int|string|DateTime $dateTime Unix timestamp | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      *                                      or datetime in strtotime() format | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      *                                      or DateTime instance | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * @param int                 $options | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * @param int                 $limit | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 6 |  |     public static function spellDifference($dateTime, $options = 0, $limit = 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 6 |  |         $now = new DateTime('@'.time()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 6 |  |         if ($dateTime instanceof DateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $interval = $dateTime->diff($now); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 6 |  |             $date_time = new DateTime(is_numeric($dateTime) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 4 |  |                 ? '@' . $dateTime | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 6 |  |                 : $dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 6 |  |             $interval = $date_time->diff($now); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 6 |  |         return static::spellInterval($interval, $options, $limit); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |