Passed
Push — master ( 69797c...d32eb8 )
by Roman
01:42
created
src/RM/DateInterval.php 2 patches
Indentation   +149 added lines, -149 removed lines patch added patch discarded remove patch
@@ -10,153 +10,153 @@
 block discarded – undo
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
-
47
-			$int = $d1->diff($d2);
48
-			if ($int === FALSE) {
49
-				$parse = date_parse($int);
50
-				if ($parse['error_count']) {
51
-					throw new DateInterval\InvalidArgumentException;
52
-				}
53
-			} else {
54
-				parent::__construct(self::parse($int));
55
-			}
56
-		}
57
-		if ($invert) {
58
-			$this->invert = 1;
59
-		}
60
-	}
61
-
62
-	public function add($interval) : self
63
-	{
64
-		$d1 = $this->getRefDT();
65
-		$d1->add($this);
66
-		$d1->add(new self($interval));
67
-		$int = ($this->getRefDT())->diff($d1);
68
-		$this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
69
-		if ($d1 < $this->getRefDT())
70
-			$this->invert = 1;
71
-		return $this;
72
-	}
73
-
74
-	public function sub($interval) : self
75
-	{
76
-		$d1 = $this->getRefDT();
77
-		$d1->add($this);
78
-		$d1->sub(new self($interval));
79
-		$int = ($this->getRefDT())->diff($d1);
80
-		$this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
81
-		if ($d1 < $this->getRefDT())
82
-			$this->invert = 1;
83
-		return $this;
84
-	}
85
-
86
-	public function toSeconds() : int
87
-	{
88
-		return (($this->y * 365 * 24 * 60 * 60) +
89
-			($this->m * 30 * 24 * 60 * 60) +
90
-			($this->d * 24 * 60 * 60) +
91
-			($this->h * 60 * 60) +
92
-			($this->i * 60) +
93
-			$this->s) * (($this->invert) ? -1 : 1);
94
-	}
95
-
96
-	public static function parse(\DateInterval $dateInterval) : string
97
-	{
98
-		$date = array(
99
-			'Y' => $dateInterval->y,
100
-			'M' => $dateInterval->m,
101
-			'D' => $dateInterval->d
102
-		);
103
-
104
-		$time = array(
105
-			'H' => $dateInterval->h,
106
-			'M' => $dateInterval->i,
107
-			'S' => $dateInterval->s
108
-		);
109
-
110
-		if (isset($time['H']) && $time['H'] < 0) {
111
-			$time['H'] = 24 + $time['H'];
112
-			if ($date['D'] >= 1)
113
-				$date['D']--;
114
-			elseif ($date['M'] >= 1)
115
-				$date['M']--;
116
-			elseif ($date['Y'] >= 1)
117
-				$date['Y']--;
118
-		}
119
-
120
-		if ($time['H'] === 24) {
121
-			$date['D']++;
122
-			$time['H'] = 0;
123
-		}
124
-
125
-		$specString = 'P';
126
-
127
-		foreach (array_filter($date) as $key => $value) {
128
-			$specString .= $value . $key;
129
-		}
130
-		if (count(array_filter($time)) > 0) {
131
-			$specString .= 'T';
132
-			foreach (array_filter($time) as $key => $value) {
133
-				$specString .= $value . $key;
134
-			}
135
-		}
136
-
137
-		if (strlen($specString) === 1) {
138
-			$specString .= 'T0S';
139
-		}
140
-
141
-		return $specString;
142
-	}
143
-
144
-	public function __toString()
145
-	{
146
-		return self::parse($this);
147
-	}
148
-
149
-	public function getRefDT()
150
-	{
151
-		return new DateTime($this->refDT->format(DateTime::ISO8601));
152
-	}
153
-
154
-	private function validateRelativeFormat($interval, \Exception $e = NULL)
155
-	{
156
-		$parse = date_parse($interval);
157
-		if ($parse['error_count'])
158
-			throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
159
-		if (!isset($parse['relative']))
160
-			throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
161
-	}
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
+
47
+            $int = $d1->diff($d2);
48
+            if ($int === FALSE) {
49
+                $parse = date_parse($int);
50
+                if ($parse['error_count']) {
51
+                    throw new DateInterval\InvalidArgumentException;
52
+                }
53
+            } else {
54
+                parent::__construct(self::parse($int));
55
+            }
56
+        }
57
+        if ($invert) {
58
+            $this->invert = 1;
59
+        }
60
+    }
61
+
62
+    public function add($interval) : self
63
+    {
64
+        $d1 = $this->getRefDT();
65
+        $d1->add($this);
66
+        $d1->add(new self($interval));
67
+        $int = ($this->getRefDT())->diff($d1);
68
+        $this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
69
+        if ($d1 < $this->getRefDT())
70
+            $this->invert = 1;
71
+        return $this;
72
+    }
73
+
74
+    public function sub($interval) : self
75
+    {
76
+        $d1 = $this->getRefDT();
77
+        $d1->add($this);
78
+        $d1->sub(new self($interval));
79
+        $int = ($this->getRefDT())->diff($d1);
80
+        $this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
81
+        if ($d1 < $this->getRefDT())
82
+            $this->invert = 1;
83
+        return $this;
84
+    }
85
+
86
+    public function toSeconds() : int
87
+    {
88
+        return (($this->y * 365 * 24 * 60 * 60) +
89
+            ($this->m * 30 * 24 * 60 * 60) +
90
+            ($this->d * 24 * 60 * 60) +
91
+            ($this->h * 60 * 60) +
92
+            ($this->i * 60) +
93
+            $this->s) * (($this->invert) ? -1 : 1);
94
+    }
95
+
96
+    public static function parse(\DateInterval $dateInterval) : string
97
+    {
98
+        $date = array(
99
+            'Y' => $dateInterval->y,
100
+            'M' => $dateInterval->m,
101
+            'D' => $dateInterval->d
102
+        );
103
+
104
+        $time = array(
105
+            'H' => $dateInterval->h,
106
+            'M' => $dateInterval->i,
107
+            'S' => $dateInterval->s
108
+        );
109
+
110
+        if (isset($time['H']) && $time['H'] < 0) {
111
+            $time['H'] = 24 + $time['H'];
112
+            if ($date['D'] >= 1)
113
+                $date['D']--;
114
+            elseif ($date['M'] >= 1)
115
+                $date['M']--;
116
+            elseif ($date['Y'] >= 1)
117
+                $date['Y']--;
118
+        }
119
+
120
+        if ($time['H'] === 24) {
121
+            $date['D']++;
122
+            $time['H'] = 0;
123
+        }
124
+
125
+        $specString = 'P';
126
+
127
+        foreach (array_filter($date) as $key => $value) {
128
+            $specString .= $value . $key;
129
+        }
130
+        if (count(array_filter($time)) > 0) {
131
+            $specString .= 'T';
132
+            foreach (array_filter($time) as $key => $value) {
133
+                $specString .= $value . $key;
134
+            }
135
+        }
136
+
137
+        if (strlen($specString) === 1) {
138
+            $specString .= 'T0S';
139
+        }
140
+
141
+        return $specString;
142
+    }
143
+
144
+    public function __toString()
145
+    {
146
+        return self::parse($this);
147
+    }
148
+
149
+    public function getRefDT()
150
+    {
151
+        return new DateTime($this->refDT->format(DateTime::ISO8601));
152
+    }
153
+
154
+    private function validateRelativeFormat($interval, \Exception $e = NULL)
155
+    {
156
+        $parse = date_parse($interval);
157
+        if ($parse['error_count'])
158
+            throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
159
+        if (!isset($parse['relative']))
160
+            throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
161
+    }
162 162
 }
Please login to merge, or discard this patch.
Braces   +31 added lines, -22 removed lines patch added patch discarded remove patch
@@ -15,13 +15,15 @@  discard block
 block discarded – undo
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
 block discarded – undo
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 {
@@ -66,8 +70,9 @@  discard block
 block discarded – undo
66 70
 		$d1->add(new self($interval));
67 71
 		$int = ($this->getRefDT())->diff($d1);
68 72
 		$this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
69
-		if ($d1 < $this->getRefDT())
70
-			$this->invert = 1;
73
+		if ($d1 < $this->getRefDT()) {
74
+					$this->invert = 1;
75
+		}
71 76
 		return $this;
72 77
 	}
73 78
 
@@ -78,8 +83,9 @@  discard block
 block discarded – undo
78 83
 		$d1->sub(new self($interval));
79 84
 		$int = ($this->getRefDT())->diff($d1);
80 85
 		$this->__construct(self::parse(/** @scrutinizer ignore-type */$int));
81
-		if ($d1 < $this->getRefDT())
82
-			$this->invert = 1;
86
+		if ($d1 < $this->getRefDT()) {
87
+					$this->invert = 1;
88
+		}
83 89
 		return $this;
84 90
 	}
85 91
 
@@ -109,12 +115,13 @@  discard block
 block discarded – undo
109 115
 
110 116
 		if (isset($time['H']) && $time['H'] < 0) {
111 117
 			$time['H'] = 24 + $time['H'];
112
-			if ($date['D'] >= 1)
113
-				$date['D']--;
114
-			elseif ($date['M'] >= 1)
115
-				$date['M']--;
116
-			elseif ($date['Y'] >= 1)
117
-				$date['Y']--;
118
+			if ($date['D'] >= 1) {
119
+							$date['D']--;
120
+			} elseif ($date['M'] >= 1) {
121
+							$date['M']--;
122
+			} elseif ($date['Y'] >= 1) {
123
+							$date['Y']--;
124
+			}
118 125
 		}
119 126
 
120 127
 		if ($time['H'] === 24) {
@@ -154,9 +161,11 @@  discard block
 block discarded – undo
154 161
 	private function validateRelativeFormat($interval, \Exception $e = NULL)
155 162
 	{
156 163
 		$parse = date_parse($interval);
157
-		if ($parse['error_count'])
158
-			throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
159
-		if (!isset($parse['relative']))
160
-			throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
164
+		if ($parse['error_count']) {
165
+					throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
166
+		}
167
+		if (!isset($parse['relative'])) {
168
+					throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
169
+		}
161 170
 	}
162 171
 }
Please login to merge, or discard this patch.