@@ -10,127 +10,127 @@ |
||
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 | - $d1 = $this->getRefDT(); |
|
43 | - $d2 = $this->getRefDT()->modify($interval); |
|
44 | - parent::__construct(self::parse($d1->diff($d2))); |
|
45 | - } |
|
46 | - if ($invert) { |
|
47 | - $this->invert = 1; |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - public function add($interval) : self |
|
52 | - { |
|
53 | - $d1 = $this->getRefDT(); |
|
54 | - $d1->add($this); |
|
55 | - $d1->add(new self($interval)); |
|
56 | - $int = ($this->getRefDT())->diff($d1); |
|
57 | - $this->__construct(self::parse($int)); |
|
58 | - if ($d1 < $this->getRefDT()) |
|
59 | - $this->invert = 1; |
|
60 | - return $this; |
|
61 | - } |
|
62 | - |
|
63 | - public function sub($interval) : self |
|
64 | - { |
|
65 | - $d1 = $this->getRefDT(); |
|
66 | - $d1->add($this); |
|
67 | - $d1->sub(new self($interval)); |
|
68 | - $int = ($this->getRefDT())->diff($d1); |
|
69 | - $this->__construct(self::parse($int)); |
|
70 | - if ($d1 < $this->getRefDT()) |
|
71 | - $this->invert = 1; |
|
72 | - return $this; |
|
73 | - } |
|
74 | - |
|
75 | - public function toSeconds() : int |
|
76 | - { |
|
77 | - return (($this->y * 365 * 24 * 60 * 60) + |
|
78 | - ($this->m * 30 * 24 * 60 * 60) + |
|
79 | - ($this->d * 24 * 60 * 60) + |
|
80 | - ($this->h * 60 * 60) + |
|
81 | - ($this->i * 60) + |
|
82 | - $this->s) * (($this->invert) ? -1 : 1); |
|
83 | - } |
|
84 | - |
|
85 | - public static function parse(\DateInterval $dateInterval) : string |
|
86 | - { |
|
87 | - $date = array_filter(array( |
|
88 | - 'Y' => $dateInterval->y, |
|
89 | - 'M' => $dateInterval->m, |
|
90 | - 'D' => $dateInterval->d |
|
91 | - )); |
|
92 | - |
|
93 | - $time = array_filter(array( |
|
94 | - 'H' => $dateInterval->h, |
|
95 | - 'M' => $dateInterval->i, |
|
96 | - 'S' => $dateInterval->s |
|
97 | - )); |
|
98 | - |
|
99 | - $specString = 'P'; |
|
100 | - |
|
101 | - foreach ($date as $key => $value) { |
|
102 | - $specString .= $value . $key; |
|
103 | - } |
|
104 | - if (count($time) > 0) { |
|
105 | - $specString .= 'T'; |
|
106 | - foreach ($time as $key => $value) { |
|
107 | - $specString .= $value . $key; |
|
108 | - } |
|
109 | - } |
|
110 | - |
|
111 | - if (strlen($specString) === 1) { |
|
112 | - $specString .= 'T0S'; |
|
113 | - } |
|
114 | - |
|
115 | - return $specString; |
|
116 | - } |
|
117 | - |
|
118 | - public function __toString() |
|
119 | - { |
|
120 | - return self::parse($this); |
|
121 | - } |
|
122 | - |
|
123 | - public function getRefDT() |
|
124 | - { |
|
125 | - return new DateTime($this->refDT->format(DateTime::ISO8601)); |
|
126 | - } |
|
127 | - |
|
128 | - private function validateRelativeFormat($interval, \Exception $e = NULL) |
|
129 | - { |
|
130 | - $parse = date_parse($interval); |
|
131 | - if ($parse['error_count']) |
|
132 | - throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
133 | - if (!isset($parse['relative'])) |
|
134 | - throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
135 | - } |
|
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 | + $d1 = $this->getRefDT(); |
|
43 | + $d2 = $this->getRefDT()->modify($interval); |
|
44 | + parent::__construct(self::parse($d1->diff($d2))); |
|
45 | + } |
|
46 | + if ($invert) { |
|
47 | + $this->invert = 1; |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + public function add($interval) : self |
|
52 | + { |
|
53 | + $d1 = $this->getRefDT(); |
|
54 | + $d1->add($this); |
|
55 | + $d1->add(new self($interval)); |
|
56 | + $int = ($this->getRefDT())->diff($d1); |
|
57 | + $this->__construct(self::parse($int)); |
|
58 | + if ($d1 < $this->getRefDT()) |
|
59 | + $this->invert = 1; |
|
60 | + return $this; |
|
61 | + } |
|
62 | + |
|
63 | + public function sub($interval) : self |
|
64 | + { |
|
65 | + $d1 = $this->getRefDT(); |
|
66 | + $d1->add($this); |
|
67 | + $d1->sub(new self($interval)); |
|
68 | + $int = ($this->getRefDT())->diff($d1); |
|
69 | + $this->__construct(self::parse($int)); |
|
70 | + if ($d1 < $this->getRefDT()) |
|
71 | + $this->invert = 1; |
|
72 | + return $this; |
|
73 | + } |
|
74 | + |
|
75 | + public function toSeconds() : int |
|
76 | + { |
|
77 | + return (($this->y * 365 * 24 * 60 * 60) + |
|
78 | + ($this->m * 30 * 24 * 60 * 60) + |
|
79 | + ($this->d * 24 * 60 * 60) + |
|
80 | + ($this->h * 60 * 60) + |
|
81 | + ($this->i * 60) + |
|
82 | + $this->s) * (($this->invert) ? -1 : 1); |
|
83 | + } |
|
84 | + |
|
85 | + public static function parse(\DateInterval $dateInterval) : string |
|
86 | + { |
|
87 | + $date = array_filter(array( |
|
88 | + 'Y' => $dateInterval->y, |
|
89 | + 'M' => $dateInterval->m, |
|
90 | + 'D' => $dateInterval->d |
|
91 | + )); |
|
92 | + |
|
93 | + $time = array_filter(array( |
|
94 | + 'H' => $dateInterval->h, |
|
95 | + 'M' => $dateInterval->i, |
|
96 | + 'S' => $dateInterval->s |
|
97 | + )); |
|
98 | + |
|
99 | + $specString = 'P'; |
|
100 | + |
|
101 | + foreach ($date as $key => $value) { |
|
102 | + $specString .= $value . $key; |
|
103 | + } |
|
104 | + if (count($time) > 0) { |
|
105 | + $specString .= 'T'; |
|
106 | + foreach ($time as $key => $value) { |
|
107 | + $specString .= $value . $key; |
|
108 | + } |
|
109 | + } |
|
110 | + |
|
111 | + if (strlen($specString) === 1) { |
|
112 | + $specString .= 'T0S'; |
|
113 | + } |
|
114 | + |
|
115 | + return $specString; |
|
116 | + } |
|
117 | + |
|
118 | + public function __toString() |
|
119 | + { |
|
120 | + return self::parse($this); |
|
121 | + } |
|
122 | + |
|
123 | + public function getRefDT() |
|
124 | + { |
|
125 | + return new DateTime($this->refDT->format(DateTime::ISO8601)); |
|
126 | + } |
|
127 | + |
|
128 | + private function validateRelativeFormat($interval, \Exception $e = NULL) |
|
129 | + { |
|
130 | + $parse = date_parse($interval); |
|
131 | + if ($parse['error_count']) |
|
132 | + throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
133 | + if (!isset($parse['relative'])) |
|
134 | + throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
135 | + } |
|
136 | 136 | } |
@@ -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 { |
@@ -55,8 +58,9 @@ discard block |
||
55 | 58 | $d1->add(new self($interval)); |
56 | 59 | $int = ($this->getRefDT())->diff($d1); |
57 | 60 | $this->__construct(self::parse($int)); |
58 | - if ($d1 < $this->getRefDT()) |
|
59 | - $this->invert = 1; |
|
61 | + if ($d1 < $this->getRefDT()) { |
|
62 | + $this->invert = 1; |
|
63 | + } |
|
60 | 64 | return $this; |
61 | 65 | } |
62 | 66 | |
@@ -67,8 +71,9 @@ discard block |
||
67 | 71 | $d1->sub(new self($interval)); |
68 | 72 | $int = ($this->getRefDT())->diff($d1); |
69 | 73 | $this->__construct(self::parse($int)); |
70 | - if ($d1 < $this->getRefDT()) |
|
71 | - $this->invert = 1; |
|
74 | + if ($d1 < $this->getRefDT()) { |
|
75 | + $this->invert = 1; |
|
76 | + } |
|
72 | 77 | return $this; |
73 | 78 | } |
74 | 79 | |
@@ -128,9 +133,11 @@ discard block |
||
128 | 133 | private function validateRelativeFormat($interval, \Exception $e = NULL) |
129 | 134 | { |
130 | 135 | $parse = date_parse($interval); |
131 | - if ($parse['error_count']) |
|
132 | - throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
133 | - if (!isset($parse['relative'])) |
|
134 | - throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
136 | + if ($parse['error_count']) { |
|
137 | + throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL); |
|
138 | + } |
|
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 | + } |
|
135 | 142 | } |
136 | 143 | } |