|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CalendR\Period; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Represents a Month. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Yohan Giarelli <[email protected]> |
|
9
|
|
|
*/ |
|
10
|
|
|
class Month extends PeriodAbstract implements \Iterator |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var PeriodInterface |
|
14
|
|
|
*/ |
|
15
|
|
|
private $current; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns the period as a DatePeriod. |
|
19
|
|
|
* |
|
20
|
|
|
* @return \DatePeriod |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getDatePeriod() |
|
23
|
|
|
{ |
|
24
|
|
|
return new \DatePeriod($this->begin, new \DateInterval('P1D'), $this->end); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Returns a Day array. |
|
29
|
|
|
* |
|
30
|
|
|
* @return array<Day> |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getDays() |
|
33
|
|
|
{ |
|
34
|
|
|
$days = array(); |
|
35
|
|
|
foreach ($this->getDatePeriod() as $date) { |
|
36
|
|
|
$days[] = $this->getFactory()->createDay($date); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $days; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns the first day of the first week of month. |
|
44
|
|
|
* First day of week is configurable via {@link Factory:setOption()}. |
|
45
|
|
|
* |
|
46
|
|
|
* @return \DateTime |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getFirstDayOfFirstWeek() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->getFactory()->findFirstDayOfWeek($this->begin); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Returns a Range period beginning at the first day of first week of this month, |
|
55
|
|
|
* and ending at the last day of the last week of this month. |
|
56
|
|
|
* |
|
57
|
|
|
* @return Range |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getExtendedMonth() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getFactory()->createRange($this->getFirstDayOfFirstWeek(), $this->getLastDayOfLastWeek()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Returns the last day of last week of month |
|
66
|
|
|
* First day of week is configurable via {@link Factory::setOption()}. |
|
67
|
|
|
* |
|
68
|
|
|
* @return \DateTime |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getLastDayOfLastWeek() |
|
71
|
|
|
{ |
|
72
|
|
|
$lastDay = clone $this->end; |
|
73
|
|
|
$lastDay->sub(new \DateInterval('P1D')); |
|
74
|
|
|
|
|
75
|
|
|
return $this->getFactory()->findFirstDayOfWeek($lastDay)->add(new \DateInterval('P6D')); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/* |
|
79
|
|
|
* Iterator implementation |
|
80
|
|
|
*/ |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Week |
|
84
|
|
|
*/ |
|
85
|
|
|
public function current() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->current; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function next() |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$this->valid()) { |
|
96
|
|
|
$this->current = $this->getFactory()->createWeek($this->getFirstDayOfFirstWeek()); |
|
97
|
|
|
} else { |
|
98
|
|
|
$this->current = $this->current->getNext(); |
|
99
|
|
|
|
|
100
|
|
|
if ($this->current->getBegin()->format('m') != $this->begin->format('m')) { |
|
101
|
|
|
$this->current = null; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritdoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function key() |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->current->getBegin()->format('W'); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function valid() |
|
118
|
|
|
{ |
|
119
|
|
|
return null !== $this->current(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function rewind() |
|
126
|
|
|
{ |
|
127
|
|
|
$this->current = null; |
|
128
|
|
|
$this->next(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Returns the month name (probably in english). |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function __toString() |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->format('F'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param \DateTime $start |
|
143
|
|
|
* |
|
144
|
|
|
* @return bool |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function isValid(\DateTime $start) |
|
147
|
|
|
{ |
|
148
|
|
|
return $start->format('d H:i:s') === '01 00:00:00'; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Returns a \DateInterval equivalent to the period. |
|
153
|
|
|
* |
|
154
|
|
|
* @return \DateInterval |
|
155
|
|
|
*/ |
|
156
|
|
|
public static function getDateInterval() |
|
157
|
|
|
{ |
|
158
|
|
|
return new \DateInterval('P1M'); |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|