Completed
Push — master ( e1538c...0e36b2 )
by Roman
04:59
created

DateInterval::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace RM;
4
5
use DateTime;
6
use DateTimeImmutable;
7
8
/**
9
 * @author Roman Mátyus <[email protected]>
10
 */
11
class DateInterval extends \DateInterval
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 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...
58
	{
59
		$d1 = $this->getRefDT();
60
		$d1->add($this);
61
		$d1->add(new self($interval));
62
		$int = ($this->getRefDT())->diff($d1);
63
		self::__construct(self::parse($int));
0 ignored issues
show
Bug Best Practice introduced by
The method RM\DateInterval::__construct() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
		/** @scrutinizer ignore-call */ self::__construct(self::parse($int));
Loading history...
64
		if ($d1 < $this->getRefDT())
65
			$this->invert = 1;
66
		return $this;
67
	}
68
69 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...
70
	{
71
		$d1 = $this->getRefDT();
72
		$d1->add($this);
73
		$d1->sub(new self($interval));
74
		$int = ($this->getRefDT())->diff($d1);
75
		self::__construct(self::parse($int));
0 ignored issues
show
Bug Best Practice introduced by
The method RM\DateInterval::__construct() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
		/** @scrutinizer ignore-call */ self::__construct(self::parse($int));
Loading history...
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) : void
135
	{
136
		$parse = date_parse($interval);
137 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...
138
			throw new DateInterval\InvalidArgumentException(($e) ? $e->getMessage() : NULL, ($e) ? $e->getCode() : NULL, ($e) ? $e : NULL);
139 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...
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
}
143