@@ -10,144 +10,144 @@ |
||
| 10 | 10 | */ |
| 11 | 11 | class DateInterval extends \DateInterval |
| 12 | 12 | { |
| 13 | - /** @var DateTimeImmutable */ |
|
| 14 | - private $refDT; |
|
| 15 | - |
|
| 16 | - public function __construct($interval = 'PT0S') |
|
| 17 | - { |
|
| 18 | - if ($interval === NULL) |
|
| 19 | - $interval = 'PT0S'; |
|
| 20 | - $this->refDT = new DateTimeImmutable('midnight'); |
|
| 21 | - $invert = FALSE; |
|
| 22 | - if ($interval instanceof \DateInterval) { |
|
| 23 | - if ($interval->invert) |
|
| 24 | - $invert = TRUE; |
|
| 25 | - $interval = self::parse($interval); |
|
| 26 | - } elseif (is_string($interval)) { |
|
| 27 | - if (strpos($interval, '-') === 0) { |
|
| 28 | - $invert = TRUE; |
|
| 29 | - $interval = substr($interval, 1); |
|
| 30 | - } |
|
| 31 | - } elseif (is_int($interval)) { |
|
| 32 | - if ($interval < 0) |
|
| 33 | - $invert = TRUE; |
|
| 34 | - $interval = sprintf("PT%uS", abs($interval)); |
|
| 35 | - } elseif (is_float($interval)) { |
|
| 36 | - if ($interval < 0) |
|
| 37 | - $invert = TRUE; |
|
| 38 | - $interval = sprintf("PT%uS", abs(round($interval))); |
|
| 39 | - } |
|
| 40 | - try { |
|
| 41 | - parent::__construct($interval); |
|
| 42 | - } catch (\Exception $e) { |
|
| 43 | - $this->validateRelativeFormat($interval, $e); |
|
| 44 | - $d1 = $this->getRefDT(); |
|
| 45 | - $d2 = $this->getRefDT()->modify($interval); |
|
| 46 | - parent::__construct(self::parse($d1->diff($d2))); |
|
| 47 | - } |
|
| 48 | - if ($invert) { |
|
| 49 | - $this->invert = 1; |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - public function add($interval) : self |
|
| 54 | - { |
|
| 55 | - $d1 = $this->getRefDT(); |
|
| 56 | - $d1->add($this); |
|
| 57 | - $d1->add(new self($interval)); |
|
| 58 | - $int = ($this->getRefDT())->diff($d1); |
|
| 59 | - $this->__construct(self::parse($int)); |
|
| 60 | - if ($d1 < $this->getRefDT()) |
|
| 61 | - $this->invert = 1; |
|
| 62 | - return $this; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - public function sub($interval) : self |
|
| 66 | - { |
|
| 67 | - $d1 = $this->getRefDT(); |
|
| 68 | - $d1->add($this); |
|
| 69 | - $d1->sub(new self($interval)); |
|
| 70 | - $int = ($this->getRefDT())->diff($d1); |
|
| 71 | - $this->__construct(self::parse($int)); |
|
| 72 | - if ($d1 < $this->getRefDT()) |
|
| 73 | - $this->invert = 1; |
|
| 74 | - return $this; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - public function toSeconds() : int |
|
| 78 | - { |
|
| 79 | - return (($this->y * 365 * 24 * 60 * 60) + |
|
| 80 | - ($this->m * 30 * 24 * 60 * 60) + |
|
| 81 | - ($this->d * 24 * 60 * 60) + |
|
| 82 | - ($this->h * 60 * 60) + |
|
| 83 | - ($this->i * 60) + |
|
| 84 | - $this->s) * (($this->invert) ? -1 : 1); |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - public static function parse(\DateInterval $dateInterval) : string |
|
| 88 | - { |
|
| 89 | - $date = array( |
|
| 90 | - 'Y' => $dateInterval->y, |
|
| 91 | - 'M' => $dateInterval->m, |
|
| 92 | - 'D' => $dateInterval->d |
|
| 93 | - ); |
|
| 94 | - |
|
| 95 | - $time = array( |
|
| 96 | - 'H' => $dateInterval->h, |
|
| 97 | - 'M' => $dateInterval->i, |
|
| 98 | - 'S' => $dateInterval->s |
|
| 99 | - ); |
|
| 100 | - |
|
| 101 | - if (isset($time['H']) && $time['H'] < 0) { |
|
| 102 | - $time['H'] = 24 + $time['H']; |
|
| 103 | - if ($date['D'] >= 1) |
|
| 104 | - $date['D']--; |
|
| 105 | - elseif ($date['M'] >= 1) |
|
| 106 | - $date['M']--; |
|
| 107 | - elseif ($date['Y'] >= 1) |
|
| 108 | - $date['Y']--; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - if ($time['H'] === 24) { |
|
| 112 | - $date['D']++; |
|
| 113 | - $time['H'] = 0; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $specString = 'P'; |
|
| 117 | - |
|
| 118 | - foreach (array_filter($date) as $key => $value) { |
|
| 119 | - $specString .= $value . $key; |
|
| 120 | - } |
|
| 121 | - if (count(array_filter($time)) > 0) { |
|
| 122 | - $specString .= 'T'; |
|
| 123 | - foreach (array_filter($time) as $key => $value) { |
|
| 124 | - $specString .= $value . $key; |
|
| 125 | - } |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - if (strlen($specString) === 1) { |
|
| 129 | - $specString .= 'T0S'; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return $specString; |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - public function __toString() |
|
| 136 | - { |
|
| 137 | - return self::parse($this); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - public function getRefDT() |
|
| 141 | - { |
|
| 142 | - return new DateTime($this->refDT->format(DateTime::ISO8601)); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - private function validateRelativeFormat($interval, \Exception $e = NULL) |
|
| 146 | - { |
|
| 147 | - $parse = date_parse($interval); |
|
| 148 | - if ($parse['error_count']) |
|
| 149 | - throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 150 | - if (!isset($parse['relative'])) |
|
| 151 | - throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 152 | - } |
|
| 13 | + /** @var DateTimeImmutable */ |
|
| 14 | + private $refDT; |
|
| 15 | + |
|
| 16 | + public function __construct($interval = 'PT0S') |
|
| 17 | + { |
|
| 18 | + if ($interval === NULL) |
|
| 19 | + $interval = 'PT0S'; |
|
| 20 | + $this->refDT = new DateTimeImmutable('midnight'); |
|
| 21 | + $invert = FALSE; |
|
| 22 | + if ($interval instanceof \DateInterval) { |
|
| 23 | + if ($interval->invert) |
|
| 24 | + $invert = TRUE; |
|
| 25 | + $interval = self::parse($interval); |
|
| 26 | + } elseif (is_string($interval)) { |
|
| 27 | + if (strpos($interval, '-') === 0) { |
|
| 28 | + $invert = TRUE; |
|
| 29 | + $interval = substr($interval, 1); |
|
| 30 | + } |
|
| 31 | + } elseif (is_int($interval)) { |
|
| 32 | + if ($interval < 0) |
|
| 33 | + $invert = TRUE; |
|
| 34 | + $interval = sprintf("PT%uS", abs($interval)); |
|
| 35 | + } elseif (is_float($interval)) { |
|
| 36 | + if ($interval < 0) |
|
| 37 | + $invert = TRUE; |
|
| 38 | + $interval = sprintf("PT%uS", abs(round($interval))); |
|
| 39 | + } |
|
| 40 | + try { |
|
| 41 | + parent::__construct($interval); |
|
| 42 | + } catch (\Exception $e) { |
|
| 43 | + $this->validateRelativeFormat($interval, $e); |
|
| 44 | + $d1 = $this->getRefDT(); |
|
| 45 | + $d2 = $this->getRefDT()->modify($interval); |
|
| 46 | + parent::__construct(self::parse($d1->diff($d2))); |
|
| 47 | + } |
|
| 48 | + if ($invert) { |
|
| 49 | + $this->invert = 1; |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + public function add($interval) : self |
|
| 54 | + { |
|
| 55 | + $d1 = $this->getRefDT(); |
|
| 56 | + $d1->add($this); |
|
| 57 | + $d1->add(new self($interval)); |
|
| 58 | + $int = ($this->getRefDT())->diff($d1); |
|
| 59 | + $this->__construct(self::parse($int)); |
|
| 60 | + if ($d1 < $this->getRefDT()) |
|
| 61 | + $this->invert = 1; |
|
| 62 | + return $this; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + public function sub($interval) : self |
|
| 66 | + { |
|
| 67 | + $d1 = $this->getRefDT(); |
|
| 68 | + $d1->add($this); |
|
| 69 | + $d1->sub(new self($interval)); |
|
| 70 | + $int = ($this->getRefDT())->diff($d1); |
|
| 71 | + $this->__construct(self::parse($int)); |
|
| 72 | + if ($d1 < $this->getRefDT()) |
|
| 73 | + $this->invert = 1; |
|
| 74 | + return $this; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + public function toSeconds() : int |
|
| 78 | + { |
|
| 79 | + return (($this->y * 365 * 24 * 60 * 60) + |
|
| 80 | + ($this->m * 30 * 24 * 60 * 60) + |
|
| 81 | + ($this->d * 24 * 60 * 60) + |
|
| 82 | + ($this->h * 60 * 60) + |
|
| 83 | + ($this->i * 60) + |
|
| 84 | + $this->s) * (($this->invert) ? -1 : 1); |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + public static function parse(\DateInterval $dateInterval) : string |
|
| 88 | + { |
|
| 89 | + $date = array( |
|
| 90 | + 'Y' => $dateInterval->y, |
|
| 91 | + 'M' => $dateInterval->m, |
|
| 92 | + 'D' => $dateInterval->d |
|
| 93 | + ); |
|
| 94 | + |
|
| 95 | + $time = array( |
|
| 96 | + 'H' => $dateInterval->h, |
|
| 97 | + 'M' => $dateInterval->i, |
|
| 98 | + 'S' => $dateInterval->s |
|
| 99 | + ); |
|
| 100 | + |
|
| 101 | + if (isset($time['H']) && $time['H'] < 0) { |
|
| 102 | + $time['H'] = 24 + $time['H']; |
|
| 103 | + if ($date['D'] >= 1) |
|
| 104 | + $date['D']--; |
|
| 105 | + elseif ($date['M'] >= 1) |
|
| 106 | + $date['M']--; |
|
| 107 | + elseif ($date['Y'] >= 1) |
|
| 108 | + $date['Y']--; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + if ($time['H'] === 24) { |
|
| 112 | + $date['D']++; |
|
| 113 | + $time['H'] = 0; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $specString = 'P'; |
|
| 117 | + |
|
| 118 | + foreach (array_filter($date) as $key => $value) { |
|
| 119 | + $specString .= $value . $key; |
|
| 120 | + } |
|
| 121 | + if (count(array_filter($time)) > 0) { |
|
| 122 | + $specString .= 'T'; |
|
| 123 | + foreach (array_filter($time) as $key => $value) { |
|
| 124 | + $specString .= $value . $key; |
|
| 125 | + } |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + if (strlen($specString) === 1) { |
|
| 129 | + $specString .= 'T0S'; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return $specString; |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + public function __toString() |
|
| 136 | + { |
|
| 137 | + return self::parse($this); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + public function getRefDT() |
|
| 141 | + { |
|
| 142 | + return new DateTime($this->refDT->format(DateTime::ISO8601)); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + private function validateRelativeFormat($interval, \Exception $e = NULL) |
|
| 146 | + { |
|
| 147 | + $parse = date_parse($interval); |
|
| 148 | + if ($parse['error_count']) |
|
| 149 | + throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 150 | + if (!isset($parse['relative'])) |
|
| 151 | + throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 152 | + } |
|
| 153 | 153 | } |
@@ -116,12 +116,12 @@ |
||
| 116 | 116 | $specString = 'P'; |
| 117 | 117 | |
| 118 | 118 | foreach (array_filter($date) as $key => $value) { |
| 119 | - $specString .= $value . $key; |
|
| 119 | + $specString .= $value.$key; |
|
| 120 | 120 | } |
| 121 | 121 | if (count(array_filter($time)) > 0) { |
| 122 | 122 | $specString .= 'T'; |
| 123 | 123 | foreach (array_filter($time) as $key => $value) { |
| 124 | - $specString .= $value . $key; |
|
| 124 | + $specString .= $value.$key; |
|
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | 127 | |
@@ -15,13 +15,15 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | public function __construct($interval = 'PT0S') |
| 17 | 17 | { |
| 18 | - if ($interval === NULL) |
|
| 19 | - $interval = 'PT0S'; |
|
| 18 | + if ($interval === NULL) { |
|
| 19 | + $interval = 'PT0S'; |
|
| 20 | + } |
|
| 20 | 21 | $this->refDT = new DateTimeImmutable('midnight'); |
| 21 | 22 | $invert = FALSE; |
| 22 | 23 | if ($interval instanceof \DateInterval) { |
| 23 | - if ($interval->invert) |
|
| 24 | - $invert = TRUE; |
|
| 24 | + if ($interval->invert) { |
|
| 25 | + $invert = TRUE; |
|
| 26 | + } |
|
| 25 | 27 | $interval = self::parse($interval); |
| 26 | 28 | } elseif (is_string($interval)) { |
| 27 | 29 | if (strpos($interval, '-') === 0) { |
@@ -29,12 +31,14 @@ discard block |
||
| 29 | 31 | $interval = substr($interval, 1); |
| 30 | 32 | } |
| 31 | 33 | } elseif (is_int($interval)) { |
| 32 | - if ($interval < 0) |
|
| 33 | - $invert = TRUE; |
|
| 34 | + if ($interval < 0) { |
|
| 35 | + $invert = TRUE; |
|
| 36 | + } |
|
| 34 | 37 | $interval = sprintf("PT%uS", abs($interval)); |
| 35 | 38 | } elseif (is_float($interval)) { |
| 36 | - if ($interval < 0) |
|
| 37 | - $invert = TRUE; |
|
| 39 | + if ($interval < 0) { |
|
| 40 | + $invert = TRUE; |
|
| 41 | + } |
|
| 38 | 42 | $interval = sprintf("PT%uS", abs(round($interval))); |
| 39 | 43 | } |
| 40 | 44 | try { |
@@ -57,8 +61,9 @@ discard block |
||
| 57 | 61 | $d1->add(new self($interval)); |
| 58 | 62 | $int = ($this->getRefDT())->diff($d1); |
| 59 | 63 | $this->__construct(self::parse($int)); |
| 60 | - if ($d1 < $this->getRefDT()) |
|
| 61 | - $this->invert = 1; |
|
| 64 | + if ($d1 < $this->getRefDT()) { |
|
| 65 | + $this->invert = 1; |
|
| 66 | + } |
|
| 62 | 67 | return $this; |
| 63 | 68 | } |
| 64 | 69 | |
@@ -69,8 +74,9 @@ discard block |
||
| 69 | 74 | $d1->sub(new self($interval)); |
| 70 | 75 | $int = ($this->getRefDT())->diff($d1); |
| 71 | 76 | $this->__construct(self::parse($int)); |
| 72 | - if ($d1 < $this->getRefDT()) |
|
| 73 | - $this->invert = 1; |
|
| 77 | + if ($d1 < $this->getRefDT()) { |
|
| 78 | + $this->invert = 1; |
|
| 79 | + } |
|
| 74 | 80 | return $this; |
| 75 | 81 | } |
| 76 | 82 | |
@@ -100,12 +106,13 @@ discard block |
||
| 100 | 106 | |
| 101 | 107 | if (isset($time['H']) && $time['H'] < 0) { |
| 102 | 108 | $time['H'] = 24 + $time['H']; |
| 103 | - if ($date['D'] >= 1) |
|
| 104 | - $date['D']--; |
|
| 105 | - elseif ($date['M'] >= 1) |
|
| 106 | - $date['M']--; |
|
| 107 | - elseif ($date['Y'] >= 1) |
|
| 108 | - $date['Y']--; |
|
| 109 | + if ($date['D'] >= 1) { |
|
| 110 | + $date['D']--; |
|
| 111 | + } elseif ($date['M'] >= 1) { |
|
| 112 | + $date['M']--; |
|
| 113 | + } elseif ($date['Y'] >= 1) { |
|
| 114 | + $date['Y']--; |
|
| 115 | + } |
|
| 109 | 116 | } |
| 110 | 117 | |
| 111 | 118 | if ($time['H'] === 24) { |
@@ -145,9 +152,11 @@ discard block |
||
| 145 | 152 | private function validateRelativeFormat($interval, \Exception $e = NULL) |
| 146 | 153 | { |
| 147 | 154 | $parse = date_parse($interval); |
| 148 | - if ($parse['error_count']) |
|
| 149 | - throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 150 | - if (!isset($parse['relative'])) |
|
| 151 | - throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 155 | + if ($parse['error_count']) { |
|
| 156 | + throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 157 | + } |
|
| 158 | + if (!isset($parse['relative'])) { |
|
| 159 | + throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
| 160 | + } |
|
| 152 | 161 | } |
| 153 | 162 | } |