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