DateRange::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
 * DateRange class.
13
 *
14
 * @since  2.0.0
15
 */
16
final class DateRange implements \IteratorAggregate
17
{
18
	/**
19
	 * DateTimeRange object
20
	 *
21
	 * @var    DateTimeRange
22
	 * @since  2.0.0
23
	 */
24
	private $range;
25
26
	/**
27
	 * Constructor.
28
	 *
29
	 * @param   Date  $start  The start date.
30
	 * @param   Date  $end    The end date.
31
	 *
32
	 * @since   2.0.0
33
	 */
34 11
	public function __construct(Date $start, Date $end)
35
	{
36 11
		$this->range = new DateTimeRange(new DateTime($start), new DateTime($end), new DateInterval('P1D'));
37 11
	}
38
39
	/**
40
	 * Creates a DateRange object from the start date for the given amount of days.
41
	 *
42
	 * @param   Date     $start   The start date.
43
	 * @param   integer  $amount  The amount of dates included in a range.
44
	 *
45
	 * @return  DateRange
46
	 *
47
	 * @since   2.0.0
48
	 */
49 1
	public static function from(Date $start, $amount)
50
	{
51 1
		return self::cast(DateTimeRange::from(new DateTime($start), $amount, new DateInterval('P1D')));
52
	}
53
54
	/**
55
	 * Creates a DateRange object to the end date for the given amount of days.
56
	 *
57
	 * @param   Date     $end     The end date.
58
	 * @param   integer  $amount  The amount of dates included in a range.
59
	 *
60
	 * @return  DateRange
61
	 *
62
	 * @since   2.0.0
63
	 */
64 1
	public static function to(Date $end, $amount)
65
	{
66 1
		return self::cast(DateTimeRange::to(new DateTime($end), $amount, new DateInterval('P1D')));
67
	}
68
69
	/**
70
	 * Returns an empty range.
71
	 *
72
	 * @return  DateRange
73
	 *
74
	 * @since   2.0.0
75
	 */
76 1
	public static function emptyRange()
77
	{
78 1
		return self::cast(DateTimeRange::emptyRange());
79
	}
80
81
	/**
82
	 * Returns the start date.
83
	 *
84
	 * @return  Date
85
	 *
86
	 * @since   2.0.0
87
	 */
88 4
	public function start()
89
	{
90 4
		return new Date($this->range->start());
91
	}
92
93
	/**
94
	 * Returns the end date.
95
	 *
96
	 * @return  Date
97
	 *
98
	 * @since   2.0.0
99
	 */
100 4
	public function end()
101
	{
102 4
		return new Date($this->range->end());
103
	}
104
105
	/**
106
	 * Checks if a range is empty.
107
	 *
108
	 * @return  boolean
109
	 *
110
	 * @since   2.0.0
111
	 */
112 2
	public function isEmpty()
113
	{
114 2
		return $this->range->isEmpty();
115
	}
116
117
	/**
118
	 * Checks if the given date is included in the range.
119
	 *
120
	 * @param   Date  $date  The date to compare to.
121
	 *
122
	 * @return  boolean
123
	 *
124
	 * @since   2.0.0
125
	 */
126 5
	public function includes(Date $date)
127
	{
128 5
		return $this->range->includes(new DateTime($date));
129
	}
130
131
	/**
132
	 * Checks if ranges are equal.
133
	 *
134
	 * @param   DateRange  $range  The range to compare to.
135
	 *
136
	 * @return  boolean
137
	 *
138
	 * @since   2.0.0
139
	 */
140 1
	public function equals(DateRange $range)
141
	{
142 1
		return $this->range->equals($range->range);
143
	}
144
145
	/**
146
	 * Checks if ranges overlap with each other.
147
	 *
148
	 * @param   DateRange  $range  The range to compare to.
149
	 *
150
	 * @return  boolean
151
	 *
152
	 * @since   2.0.0
153
	 */
154 10
	public function overlaps(DateRange $range)
155
	{
156 10
		return $this->range->overlaps($range->range);
157
	}
158
159
	/**
160
	 * Checks if the given range is included in the current one.
161
	 *
162
	 * @param   DateRange  $range  The range to compare to.
163
	 *
164
	 * @return  boolean
165
	 *
166
	 * @since   2.0.0
167
	 */
168 10
	public function includesRange(DateRange $range)
169
	{
170 10
		return $this->range->includesRange($range->range);
171
	}
172
173
	/**
174
	 * Returns a gap range between two ranges.
175
	 *
176
	 * @param   DateRange  $range  The range to compare to.
177
	 *
178
	 * @return  DateRange
179
	 *
180
	 * @since   2.0.0
181
	 */
182 3
	public function gap(DateRange $range)
183
	{
184 3
		return self::cast($this->range->gap($range->range));
185
	}
186
187
	/**
188
	 * Checks if ranges abuts with each other.
189
	 *
190
	 * @param   DateRange  $range  The range to compare to.
191
	 *
192
	 * @return  boolean
193
	 *
194
	 * @since   2.0.0
195
	 */
196 5
	public function abuts(DateRange $range)
197
	{
198 5
		return $this->range->abuts($range->range);
199
	}
200
201
	/**
202
	 * Returns an array of dates which are included in the current range.
203
	 *
204
	 * @return  Date[]
205
	 *
206
	 * @since   2.0.0
207
	 */
208 3
	public function toArray()
209
	{
210 3
		$range = array();
211
212 3
		foreach ($this as $day)
213
		{
214 3
			$range[] = $day;
215 3
		}
216
217 3
		return $range;
218
	}
219
220
	/**
221
	 * Returns an external iterator.
222
	 *
223
	 * @return  DateIterator
224
	 *
225
	 * @since   2.0.0
226
	 */
227 3
	public function getIterator()
228
	{
229 3
		return new DateIterator($this->start(), $this->end());
230
	}
231
232
	/**
233
	 * Returns string representation of the range.
234
	 *
235
	 * @return  string
236
	 *
237
	 * @since   2.0.0
238
	 */
239 1
	public function __toString()
240
	{
241 1
		return sprintf('%s - %s', $this->start()->format('Y-m-d'), $this->end()->format('Y-m-d'));
242
	}
243
244
	/**
245
	 * Returns a range which is created by combination of given ranges. There can't
246
	 * be a gap between given ranges.
247
	 *
248
	 * @param   DateRange[]  $ranges  An array of ranges to combine.
249
	 *
250
	 * @return  DateRange
251
	 *
252
	 * @since   2.0.0
253
	 */
254 5
	public static function combination(array $ranges)
255
	{
256 5
		$dateTimeRanges = array();
257
258 5
		foreach ($ranges as $range)
259
		{
260 5
			$dateTimeRanges[] = $range->range;
261 5
		}
262
263 5
		return self::cast(DateTimeRange::combination($dateTimeRanges));
264
	}
265
266
	/**
267
	 * Checks if ranges are contiguous.
268
	 *
269
	 * @param   DateRange[]  $ranges  An array of ranges to combine.
270
	 *
271
	 * @return  boolean
272
	 *
273
	 * @since   2.0.0
274
	 */
275 2
	public static function isContiguous(array $ranges)
276
	{
277 2
		$dateTimeRange = array();
278
279 2
		foreach ($ranges as $range)
280
		{
281 2
			$dateTimeRange[] = $range->range;
282 2
		}
283
284 2
		return DateTimeRange::isContiguous($dateTimeRange);
285
	}
286
287
	/**
288
	 * Casts an DateTimeRange object into DateRange.
289
	 *
290
	 * @param   DateTimeRange  $range  A DateTimeRange object
291
	 *
292
	 * @return  DateRange
293
	 *
294
	 * @since   2.0.0
295
	 */
296 7
	private static function cast(DateTimeRange $range)
297
	{
298 7
		$start = new Date($range->start());
299 7
		$end   = new Date($range->end());
300
301 7
		return new DateRange($start, $end);
302
	}
303
}
304