DateInterval::invert()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * Part of the Joomla Framework DateTime Package
4
 *
5
 * @copyright  Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
6
 * @license    GNU Lesser General Public License version 2.1 or later; see LICENSE
7
 */
8
9
namespace Joomla\DateTime;
10
11
/**
12
 * DateInterval.
13
 *
14
 * @since  2.0.0
15
 *
16
 * @property-read  integer  $y       Number of years.
17
 * @property-read  integer  $m       Number of months.
18
 * @property-read  integer  $d       Number of days.
19
 * @property-read  integer  $h       Number of hours.
20
 * @property-read  integer  $i       Number of minutes.
21
 * @property-read  integer  $s       Number of seconds.
22
 * @property-read  integer  $invert  Is 1 if the interval represents a negative time period and 0 otherwise.
23
 */
24
final class DateInterval
25
{
26
	/**
27
	 * PHP DateInverval object
28
	 *
29
	 * @var    \DateInterval
30
	 * @since  2.0.0
31
	 */
32
	private $interval;
33
34
	/**
35
	 * Constructor.
36
	 *
37
	 * @param   \DateInterval|string  $interval  Either a PHP DateInterval object or a string with an interval specification.
38
	 *
39
	 * @since   2.0.0
40
	 */
41 303
	public function __construct($interval)
42
	{
43 303
		$this->interval = ($interval instanceof \DateInterval) ? $this->copy($interval) : new \DateInterval($interval);
44 303
	}
45
46
	/**
47
	 * Creates a DateInterval object from the relative parts of the string.
48
	 *
49
	 * @param   string  $time  A date with relative parts.
50
	 *
51
	 * @return  DateInterval
52
	 *
53
	 * @since   2.0.0
54
	 */
55 8
	public static function createFromDateString($time)
56
	{
57 8
		return new DateInterval(\DateInterval::createFromDateString($time));
58
	}
59
60
	/**
61
	 * Creates a DateInterval object by adding another interval into it.
62
	 * Uses absolute values for addition process.
63
	 *
64
	 * @param   DateInterval  $interval  The interval to be added.
65
	 *
66
	 * @return  DateInterval
67
	 *
68
	 * @since   2.0.0
69
	 */
70 6
	public function add(DateInterval $interval)
71
	{
72 6
		$years   = $this->y + $interval->y;
73 6
		$months  = $this->m + $interval->m;
74 6
		$days    = $this->d + $interval->d;
75 6
		$hours   = $this->h + $interval->h;
76 6
		$minutes = $this->i + $interval->i;
77 6
		$seconds = $this->s + $interval->s;
78
79 6
		$spec = sprintf('P%sY%sM%sDT%sH%sM%sS', $years, $months, $days, $hours, $minutes, $seconds);
80
81 6
		return new DateInterval($spec);
82
	}
83
84
	/**
85
	 * Checks if the current interval is equals to the interval given as parameter.
86
	 *
87
	 * @param   DateInterval  $interval  The interval to compare.
88
	 *
89
	 * @return  boolean
90
	 *
91
	 * @since   2.0.0
92
	 */
93 18
	public function equals(DateInterval $interval)
94
	{
95 18
		$years   = $this->y == $interval->y;
96 18
		$months  = $this->m == $interval->m;
97 18
		$days    = $this->d == $interval->d;
98 18
		$hours   = $this->h == $interval->h;
99 18
		$minutes = $this->i == $interval->i;
100 18
		$seconds = $this->s == $interval->s;
101 18
		$invert  = $this->invert == $interval->invert;
102
103 18
		return $years && $months && $days && $hours && $minutes && $seconds && $invert;
104
	}
105
106
	/**
107
	 * Creates a DateInterval object by inverting the value of the current one.
108
	 *
109
	 * @return  DateInterval
110
	 *
111
	 * @since   2.0.0
112
	 */
113 2
	public function invert()
114
	{
115 2
		$interval = $this->copy($this->interval);
116 2
		$interval->invert = $interval->invert ? 0 : 1;
117
118 2
		return new DateInterval($interval);
119
	}
120
121
	/**
122
	 * Formats the interval.
123
	 *
124
	 * @param   string  $format  Format accepted by PHP DateInterval::format().
125
	 *
126
	 * @return  string
127
	 *
128
	 * @since   2.0.0
129
	 */
130 143
	public function format($format)
131
	{
132 143
		return $this->interval->format($format);
133
	}
134
135
	/**
136
	 * Magic method to access properties of the interval.
137
	 *
138
	 * @param   string  $name  The name of the property.
139
	 *
140
	 * @return  mixed
141
	 *
142
	 * @since   2.0.0
143
	 */
144 31
	public function __get($name)
145
	{
146 31
		return $this->interval->$name;
147
	}
148
149
	/**
150
	 * Returns a PHP DateInterval object.
151
	 *
152
	 * @return  \DateInterval
153
	 *
154
	 * @since   2.0.0
155
	 */
156 164
	public function getDateInterval()
157
	{
158 164
		return $this->copy($this->interval);
159
	}
160
161
	/**
162
	 * Creates a copy of PHP DateInterval object.
163
	 *
164
	 * @param   \DateInterval  $interval  The object to copy.
165
	 *
166
	 * @return  \DateInterval
167
	 *
168
	 * @since   2.0.0
169
	 */
170 314
	private function copy(\DateInterval $interval)
171
	{
172 314
		$copy = new \DateInterval($interval->format('P%yY%mM%dDT%hH%iM%sS'));
173 314
		$copy->invert = $interval->invert;
174
175 314
		return $copy;
176
	}
177
}
178