| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace AlgoWeb\xsdTypes\AxillaryClasses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | class XMLDateInterval extends \DateInterval | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |      * formating string like ISO 8601 (PnYnMnDTnHnMnS). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     const INTERVAL_ISO8601 = 'P%yY%mM%dDT%hH%iM%sS'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private $pattern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $patternLen; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     public function __construct($intervalSpec, $pattern = 'PnYnMnDTnHnMnS') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         parent::__construct(trim($intervalSpec, '-')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         if ($intervalSpec[0] == '-') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             $this->invert = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->pattern = trim($pattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->patternLen = strlen($this->pattern); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * formating the interval like ISO 8601 (PnYnMnDTnHnMnS). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function __toString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $sReturn = $this->handleSign(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $tSeen = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         for ($i = 0; $i < $this->patternLen; $i++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             $sReturn .= $this->handleChar($i, $tSeen); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return $sReturn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     private function handleSign() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         if ($this->invert === 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return '-'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param int  $i | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param bool $tSeen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private function handleChar($i, &$tSeen) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         switch ($this->pattern[$i]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             case 'n': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 return $this->handleN($i, $tSeen); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             case 'T': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 return $this->handleT($tSeen); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 return $this->handleOther($i); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param int  $i | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @param bool $tSeen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     private function handleN($i, $tSeen) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $v = ($this->pattern[$i + 1] == 'M' && $tSeen) ? 'i' : strtolower($this->pattern[$i + 1]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->$v; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param bool $tSeen | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     private function handleT(&$tSeen) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $tSeen = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return 'T'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param int $i | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |     private function handleOther($i) | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |         return $this->pattern[$i]; | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 101 |  |  |  |