1 | <?php |
||
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() |
|
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() |
||
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() |
|
135 | } |
||
136 |