Passed
Push — master ( 1490e2...5ea173 )
by Roman
01:54
created
src/RM/DateInterval.php 2 patches
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -10,133 +10,133 @@
 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
-			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_filter(array(
90
-			'Y' => $dateInterval->y,
91
-			'M' => $dateInterval->m,
92
-			'D' => $dateInterval->d
93
-		));
94
-
95
-		$time = array_filter(array(
96
-			'H' => $dateInterval->h,
97
-			'M' => $dateInterval->i,
98
-			'S' => $dateInterval->s
99
-		));
100
-
101
-		$specString = 'P';
102
-
103
-		foreach ($date as $key => $value) {
104
-			$specString .= $value . $key;
105
-		}
106
-		if (count($time) > 0) {
107
-			$specString .= 'T';
108
-			foreach ($time as $key => $value) {
109
-				$specString .= $value . $key;
110
-			}
111
-		}
112
-
113
-		if (strlen($specString) === 1) {
114
-			$specString .= 'T0S';
115
-		}
116
-
117
-		if (strpos($specString, 'T-1H') !== FALSE) {
118
-			$specString = str_replace('T-1H', 'T23H', $specString);
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
+        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_filter(array(
90
+            'Y' => $dateInterval->y,
91
+            'M' => $dateInterval->m,
92
+            'D' => $dateInterval->d
93
+        ));
94
+
95
+        $time = array_filter(array(
96
+            'H' => $dateInterval->h,
97
+            'M' => $dateInterval->i,
98
+            'S' => $dateInterval->s
99
+        ));
100
+
101
+        $specString = 'P';
102
+
103
+        foreach ($date as $key => $value) {
104
+            $specString .= $value . $key;
105
+        }
106
+        if (count($time) > 0) {
107
+            $specString .= 'T';
108
+            foreach ($time as $key => $value) {
109
+                $specString .= $value . $key;
110
+            }
111
+        }
112
+
113
+        if (strlen($specString) === 1) {
114
+            $specString .= 'T0S';
115
+        }
116
+
117
+        if (strpos($specString, 'T-1H') !== FALSE) {
118
+            $specString = str_replace('T-1H', 'T23H', $specString);
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
 }
Please login to merge, or discard this patch.
Braces   +24 added lines, -16 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 {
@@ -57,8 +61,9 @@  discard block
 block discarded – undo
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
 block discarded – undo
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
 
@@ -134,9 +140,11 @@  discard block
 block discarded – undo
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
 }
Please login to merge, or discard this patch.