Date   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 415
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.3%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 31
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 415
ccs 63
cts 69
cp 0.913
rs 9.8

29 Methods

Rating   Name   Duplication   Size   Complexity  
A isBefore() 0 4 1
A addDays() 0 4 1
A subDays() 0 4 1
A addWeeks() 0 4 1
A subWeeks() 0 4 1
A addMonths() 0 4 1
A subMonths() 0 4 1
A addYears() 0 4 1
A subYears() 0 4 1
A startOfWeek() 0 4 1
A endOfWeek() 0 4 1
A startOfMonth() 0 4 1
A endOfMonth() 0 4 1
A startOfYear() 0 4 1
A endOfYear() 0 4 1
A format() 0 4 1
A since() 0 4 1
A sinceAlmost() 0 4 1
A getDateTime() 0 4 1
A setTranslator() 0 4 1
A setLocale() 0 4 1
A cast() 0 9 2
A __construct() 0 9 2
A today() 0 4 1
A tomorrow() 0 4 1
A yesterday() 0 4 1
A isAfter() 0 4 1
A equals() 0 4 1
A diff() 0 4 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
 * Day-precision wrapper for a DateTime class.
13
 *
14
 * @since  2.0.0
15
 */
16
class Date
17
{
18
	/**
19
	 * DateTime object
20
	 *
21
	 * @var    DateTime
22
	 * @since  2.0.0
23
	 */
24
	private $datetime;
25
26
	/**
27
	 * Constructor.
28
	 *
29
	 * @param   mixed  $date  Either a Joomla\DateTime object, a PHP DateTime object
30
	 *                        or a string in a format accepted by strtotime().
31
	 *
32
	 * @since   2.0.0
33
	 */
34 70
	public function __construct($date)
35
	{
36 70
		if (!$date instanceof DateTime)
37 70
		{
38 14
			$date = new DateTime($date);
39 14
		}
40
41 70
		$this->datetime = $date->startOfDay();
42 70
	}
43
44
	/**
45
	 * Creates a Date object which represents today.
46
	 *
47
	 * @param   \DateTimeZone  $timezone  The timezone.
48
	 *
49
	 * @return  Date
50
	 *
51
	 * @since   2.0.0
52
	 */
53 7
	public static function today(\DateTimeZone $timezone = null)
54
	{
55 7
		return new static(DateTime::today($timezone));
56
	}
57
58
	/**
59
	 * Creates a Date object which represents tomorrow.
60
	 *
61
	 * @param   \DateTimeZone  $timezone  The timezone.
62
	 *
63
	 * @return  Date
64
	 *
65
	 * @since   2.0.0
66
	 */
67 4
	public static function tomorrow(\DateTimeZone $timezone = null)
68
	{
69 4
		return new static(DateTime::tomorrow($timezone));
70
	}
71
72
	/**
73
	 * Creates a Date object which represents yesterday.
74
	 *
75
	 * @param   \DateTimeZone  $timezone  The timezone.
76
	 *
77
	 * @return  Date
78
	 *
79
	 * @since   2.0.0
80
	 */
81 4
	public static function yesterday(\DateTimeZone $timezone = null)
82
	{
83 4
		return new static(DateTime::yesterday($timezone));
84
	}
85
86
	/**
87
	 * Checks if the current date is after the date given as parameter.
88
	 *
89
	 * @param   Date  $date  The date to compare to.
90
	 *
91
	 * @return  boolean
92
	 *
93
	 * @since   2.0.0
94
	 */
95 1
	public function isAfter(Date $date)
96
	{
97 1
		return $this->datetime->isAfter($date->datetime);
98
	}
99
100
	/**
101
	 * Checks if the current date is before the date given as parameter.
102
	 *
103
	 * @param   Date  $date  The date to compare to.
104
	 *
105
	 * @return  boolean
106
	 *
107
	 * @since   2.0.0
108
	 */
109 1
	public function isBefore(Date $date)
110
	{
111 1
		return $this->datetime->isBefore($date->datetime);
112
	}
113
114
	/**
115
	 * Checks if the current date is equal to the date given as parameter.
116
	 *
117
	 * @param   Date  $date  The date to compare to.
118
	 *
119
	 * @return  boolean
120
	 *
121
	 * @since   2.0.0
122
	 */
123 1
	public function equals(Date $date)
124
	{
125 1
		return $this->datetime->equals($date->datetime);
126
	}
127
128
	/**
129
	 * Returns the difference between two objects.
130
	 *
131
	 * @param   Date     $date      The date to compare to.
132
	 * @param   boolean  $absolute  Should the interval be forced to be positive?
133
	 *
134
	 * @return  DateInterval
135
	 *
136
	 * @since   2.0.0
137
	 */
138 1
	public function diff(Date $date, $absolute = false)
139
	{
140 1
		return $this->datetime->diff($date->datetime, $absolute);
141
	}
142
143
	/**
144
	 * Returns a new Date object by adding the specified number of days to the current one.
145
	 *
146
	 * @param   integer  $value  Number of days to be added.
147
	 *
148
	 * @return  Date
149
	 *
150
	 * @since   2.0.0
151
	 */
152 7
	public function addDays($value)
153
	{
154 7
		return new static($this->datetime->addDays($value));
155
	}
156
157
	/**
158
	 * Returns a new Date object by subtracting the specified number of days from the current one.
159
	 *
160
	 * @param   integer  $value  Number of days to be subtracted.
161
	 *
162
	 * @return  Date
163
	 *
164
	 * @since   2.0.0
165
	 */
166 5
	public function subDays($value)
167
	{
168 5
		return new static($this->datetime->subDays($value));
169
	}
170
171
	/**
172
	 * Returns a new Date object by adding the specified number of weeks to the current one.
173
	 *
174
	 * @param   integer  $value  Number of weeks to be added.
175
	 *
176
	 * @return  Date
177
	 *
178
	 * @since   2.0.0
179
	 */
180 4
	public function addWeeks($value)
181
	{
182 4
		return new static($this->datetime->addWeeks($value));
183
	}
184
185
	/**
186
	 * Returns a new Date object by subtracting the specified number of weeks from the current one.
187
	 *
188
	 * @param   integer  $value  Number of weeks to be subtracted.
189
	 *
190
	 * @return  Date
191
	 *
192
	 * @since   2.0.0
193
	 */
194 4
	public function subWeeks($value)
195
	{
196 4
		return new static($this->datetime->subWeeks($value));
197
	}
198
199
	/**
200
	 * Returns a new Date object by adding the specified number of months to the current one.
201
	 *
202
	 * @param   integer  $value  Number of months to be added.
203
	 *
204
	 * @return  Date
205
	 *
206
	 * @since   2.0.0
207
	 */
208 10
	public function addMonths($value)
209
	{
210 10
		return new static($this->datetime->addMonths($value));
211
	}
212
213
	/**
214
	 * Returns a new Date object by subtracting the specified number of months from the current one.
215
	 *
216
	 * @param   integer  $value  Number of months to be subtracted.
217
	 *
218
	 * @return  Date
219
	 *
220
	 * @since   2.0.0
221
	 */
222 10
	public function subMonths($value)
223
	{
224 10
		return new static($this->datetime->subMonths($value));
225
	}
226
227
	/**
228
	 * Returns a new Date object by adding the specified number of years to the current one.
229
	 *
230
	 * @param   integer  $value  Number of years to be added.
231
	 *
232
	 * @return  Date
233
	 *
234
	 * @since   2.0.0
235
	 */
236 4
	public function addYears($value)
237
	{
238 4
		return new static($this->datetime->addYears($value));
239
	}
240
241
	/**
242
	 * Returns a new Date object by subtracting the specified number of years from the current one.
243
	 *
244
	 * @param   integer  $value  Number of years to be subtracted.
245
	 *
246
	 * @return  Date
247
	 *
248
	 * @since   2.0.0
249
	 */
250 4
	public function subYears($value)
251
	{
252 4
		return new static($this->datetime->subYears($value));
253
	}
254
255
	/**
256
	 * Returns a new Date object representing the start of the current week.
257
	 *
258
	 * @return  Date
259
	 *
260
	 * @since   2.0.0
261
	 */
262 1
	public function startOfWeek()
263
	{
264 1
		return new static($this->datetime->startOfWeek());
265
	}
266
267
	/**
268
	 * Returns a new Date object representing the end of the current week.
269
	 *
270
	 * @return  Date
271
	 *
272
	 * @since   2.0.0
273
	 */
274 1
	public function endOfWeek()
275
	{
276 1
		return new static($this->datetime->endOfWeek());
277
	}
278
279
	/**
280
	 * Returns a new Date object representing the start of the current month.
281
	 *
282
	 * @return  Date
283
	 *
284
	 * @since   2.0.0
285
	 */
286 1
	public function startOfMonth()
287
	{
288 1
		return new static($this->datetime->startOfMonth());
289
	}
290
291
	/**
292
	 * Returns a new Date object representing the end of the current month.
293
	 *
294
	 * @return  Date
295
	 *
296
	 * @since   2.0.0
297
	 */
298 1
	public function endOfMonth()
299
	{
300 1
		return new static($this->datetime->endOfMonth());
301
	}
302
303
	/**
304
	 * Returns a new Date object representing the start of the current year.
305
	 *
306
	 * @return  Date
307
	 *
308
	 * @since   2.0.0
309
	 */
310 1
	public function startOfYear()
311
	{
312 1
		return new static($this->datetime->startOfYear());
313
	}
314
315
	/**
316
	 * Returns a new Date object representing the end of the current year.
317
	 *
318
	 * @return  Date
319
	 *
320
	 * @since   2.0.0
321
	 */
322 1
	public function endOfYear()
323
	{
324 1
		return new static($this->datetime->endOfYear());
325
	}
326
327
	/**
328
	 * Returns date formatted according to given format.
329
	 *
330
	 * @param   string  $format  Format accepted by date().
331
	 *
332
	 * @return  string
333
	 *
334
	 * @since   2.0.0
335
	 */
336 1
	public function format($format)
337
	{
338 1
		return $this->datetime->format($format);
339
	}
340
341
	/**
342
	 * Returns the difference in a human readable format.
343
	 *
344
	 * @param   Date     $date         The date to compare to. Default is null and this means that
345
	 *                                  the current object will be compared to the current time.
346
	 * @param   integer  $detailLevel  How much details do you want to get
347
	 *
348
	 * @return  string
349
	 *
350
	 * @since   2.0.0
351
	 */
352 1
	public function since(Date $date = null, $detailLevel = 1)
353
	{
354 1
		return $this->datetime->since(self::cast($date), $detailLevel);
355
	}
356
357
	/**
358
	 * Returns the almost difference in a human readable format.
359
	 *
360
	 * @param   Date  $date  The date to compare to. Default is null and this means that
361
	 *                        the current object will be compared to the current time.
362
	 *
363
	 * @return  string
364
	 *
365
	 * @since   2.0.0
366
	 */
367 1
	public function sinceAlmost(Date $date = null)
368
	{
369 1
		return $this->datetime->sinceAlmost(self::cast($date));
370
	}
371
372
	/**
373
	 * Returns a PHP DateTime object.
374
	 *
375
	 * @return  \DateTime
376
	 *
377
	 * @since   2.0.0
378
	 */
379 19
	public function getDateTime()
380
	{
381 19
		return $this->datetime->getDateTime();
382
	}
383
384
	/**
385
	 * Sets the Translator implementation.
386
	 *
387
	 * @param   Translator\AbstractTranslator  $translator  The Translator implementation.
388
	 *
389
	 * @return  void
390
	 *
391
	 * @since   2.0.0
392
	 */
393
	public static function setTranslator(Translator\AbstractTranslator $translator)
394
	{
395
		DateTime::setTranslator($translator);
396
	}
397
398
	/**
399
	 * Sets the locale.
400
	 *
401
	 * @param   string  $locale  The locale to set.
402
	 *
403
	 * @return  void
404
	 *
405
	 * @since   2.0.0
406
	 */
407
	public static function setLocale($locale)
408
	{
409
		DateTime::setLocale($locale);
410
	}
411
412
	/**
413
	 * Casts to DateTime.
414
	 *
415
	 * @param   Date  $date  Date to cast.
416
	 *
417
	 * @return  DateTime|null
418
	 *
419
	 * @since   2.0.0
420
	 */
421 2
	private static function cast(Date $date = null)
422
	{
423 2
		if (!is_null($date))
424 2
		{
425 2
			$date = new DateTime($date);
426 2
		}
427
428 2
		return $date;
429
	}
430
}
431