DateTimeIterator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 89.47%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 120
ccs 17
cts 19
cp 0.8947
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A next() 0 5 1
A rewind() 0 5 1
A __construct() 0 6 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
 * Iterator for ranges of DateTime objects.
13
 *
14
 * @since  2.0.0
15
 */
16
class DateTimeIterator implements \Iterator
17
{
18
	/**
19
	 * DateTime object representing the start date of the iterator
20
	 *
21
	 * @var    DateTime
22
	 * @since  2.0.0
23
	 */
24
	private $start;
25
26
	/**
27
	 * DateTime object representing the end date of the iterator
28
	 *
29
	 * @var    DateTime
30
	 * @since  2.0.0
31
	 */
32
	private $end;
33
34
	/**
35
	 * Interval between dates
36
	 *
37
	 * @var    DateInterval
38
	 * @since  2.0.0
39
	 */
40
	private $interval;
41
42
	/**
43
	 * DateTime object representing the current date
44
	 *
45
	 * @var    DateTime
46
	 * @since  2.0.0
47
	 */
48
	private $current;
49
50
	/**
51
	 * The key of the current date
52
	 *
53
	 * @var    integer
54
	 * @since  2.0.0
55
	 */
56
	private $key;
57
58
	/**
59
	 * Constructor.
60
	 *
61
	 * @param   DateTime      $start     The start date.
62
	 * @param   DateTime      $end       The end date.
63
	 * @param   DateInterval  $interval  The interval between adjacent dates.
64
	 *
65
	 * @since   2.0.0
66
	 */
67 10
	public function __construct(DateTime $start, DateTime $end, DateInterval $interval)
68
	{
69 10
		$this->start = $this->current = $start;
70 10
		$this->end = $end;
71 10
		$this->interval = $interval;
72 10
	}
73
74
	/**
75
	 * Returns the current date.
76
	 *
77
	 * @return  DateTime
78
	 *
79
	 * @since   2.0.0
80
	 */
81 10
	public function current()
82
	{
83 10
		return $this->current;
84
	}
85
86
	/**
87
	 * Returns the key of the current date.
88
	 *
89
	 * @return  integer
90
	 *
91
	 * @since   2.0.0
92
	 */
93
	public function key()
94
	{
95
		return $this->key;
96
	}
97
98
	/**
99
	 * Moves the current position to the next date.
100
	 *
101
	 * @return  void
102
	 *
103
	 * @since   2.0.0
104
	 */
105 10
	public function next()
106
	{
107 10
		$this->key++;
108 10
		$this->current = $this->current->add($this->interval);
109 10
	}
110
111
	/**
112
	 * Rewinds back to the first date.
113
	 *
114
	 * @return  void
115
	 *
116
	 * @since   2.0.0
117
	 */
118 10
	public function rewind()
119
	{
120 10
		$this->key = 0;
121 10
		$this->current = $this->start;
122 10
	}
123
124
	/**
125
	 * Checks if current position is valid.
126
	 *
127
	 * @return  boolean
128
	 *
129
	 * @since   2.0.0
130
	 */
131 10
	public function valid()
132
	{
133 10
		return !$this->current->isAfter($this->end);
134
	}
135
}
136