Passed
Branch master (b798df)
by Roman
01:36
created

DateInterval::sub()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 8
cts 8
cp 1
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
crap 2
1
<?php
2
3
namespace RM;
4
5
use DateTime;
6
use DateTimeImmutable;
7
8
/**
9
 * @author Roman Mátyus <[email protected]>
10
 */
11 1
class DateInterval extends \DateInterval
12
{
13
	/** @var DateTimeImmutable */
14
	private $refDT;
15
16
	public function __construct($interval = 'PT0S')
17
	{
18 1
		$this->refDT = new DateTimeImmutable('midnight');
19 1
		$invert = FALSE;
20 1
		if ($interval instanceof \DateInterval) {
21 1
			if ($interval->invert)
22 1
				$invert = TRUE;
23 1
			$interval = self::parse($interval);
24 1
		} elseif (is_string($interval)) {
25 1
			if (strpos($interval, '-') === 0) {
26 1
				$invert = TRUE;
27 1
				$interval = substr($interval, 1);
28
			}
29 1
		} elseif (is_int($interval)) {
30 1
			if ($interval < 0)
31 1
				$invert = TRUE;
32 1
			$interval = sprintf("PT%uS", abs($interval));
33 1
		} elseif (is_float($interval)) {
34 1
			if ($interval < 0)
35 1
				$invert = TRUE;
36 1
			$interval = sprintf("PT%uS", abs(round($interval)));
37
		}
38
		try {
39 1
			parent::__construct($interval);
40 1
		} catch (\Exception $e) {
41 1
			$this->validateRelativeFormat($interval, $e);
42 1
			$d1 = $this->getRefDT();
43 1
			$d2 = $this->getRefDT()->modify($interval);
44 1
			parent::__construct(self::parse($d1->diff($d2)));
45
		}
46 1
		if ($invert) {
47 1
			$this->invert = 1;
48
		}
49 1
	}
50
51 View Code Duplication
	public function add($interval) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
	{
53 1
		$d1 = $this->getRefDT();
54 1
		$d1->add($this);
55 1
		$d1->add(new self($interval));
56 1
		$int = ($this->getRefDT())->diff($d1);
57 1
		$this->__construct(self::parse($int));
58 1
		if ($d1 < $this->getRefDT())
59 1
			$this->invert = 1;
60 1
		return $this;
61
	}
62
63 View Code Duplication
	public function sub($interval) : self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
	{
65 1
		$d1 = $this->getRefDT();
66 1
		$d1->add($this);
67 1
		$d1->sub(new self($interval));
68 1
		$int = ($this->getRefDT())->diff($d1);
69 1
		$this->__construct(self::parse($int));
70 1
		if ($d1 < $this->getRefDT())
71 1
			$this->invert = 1;
72 1
		return $this;
73
	}
74
75
	public function toSeconds() : int
76
	{
77 1
		return (($this->y * 365 * 24 * 60 * 60) +
78 1
			($this->m * 30 * 24 * 60 * 60) +
79 1
			($this->d * 24 * 60 * 60) +
80 1
			($this->h * 60 * 60) +
81 1
			($this->i * 60) +
82 1
			$this->s) * (($this->invert) ? -1 : 1);
83
	}
84
85
	public static function parse(\DateInterval $dateInterval) : string
86
	{
87 1
		$date = array_filter(array(
88 1
			'Y' => $dateInterval->y,
89 1
			'M' => $dateInterval->m,
90 1
			'D' => $dateInterval->d
91
		));
92
93 1
		$time = array_filter(array(
94 1
			'H' => $dateInterval->h,
95 1
			'M' => $dateInterval->i,
96 1
			'S' => $dateInterval->s
97
		));
98
99 1
		$specString = 'P';
100
101 1
		foreach ($date as $key => $value) {
102 1
			$specString .= $value . $key;
103
		}
104 1
		if (count($time) > 0) {
105 1
			$specString .= 'T';
106 1
			foreach ($time as $key => $value) {
107 1
				$specString .= $value . $key;
108
			}
109
		}
110
111 1
		if (strlen($specString) === 1) {
112 1
			$specString .= 'T0S';
113
		}
114
115 1
		return $specString;
116
	}
117
118
	public function __toString()
119
	{
120 1
		return self::parse($this);
121
	}
122
123
	public function getRefDT()
124
	{
125 1
		return new DateTime($this->refDT->format(DateTime::ISO8601));
126
	}
127
128
	private function validateRelativeFormat($interval, \Exception $e = NULL)
129
	{
130 1
		$parse = date_parse($interval);
131 1 View Code Duplication
		if ($parse['error_count'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132 1
			throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
133 1 View Code Duplication
		if (!isset($parse['relative']))
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
134 1
			throw new DateInterval\InvalidArgumentException(sprintf("First argument '%s' is not in supported relative date format.", $interval), ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
135 1
	}
136
}
137