1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of CalendR, a Fréquence web project. |
5
|
|
|
* |
6
|
|
|
* (c) 2012 Fréquence web |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CalendR; |
13
|
|
|
|
14
|
|
|
use CalendR\Event\Manager; |
15
|
|
|
use CalendR\Period\Factory; |
16
|
|
|
use CalendR\Period\FactoryInterface; |
17
|
|
|
use CalendR\Period\PeriodInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Factory class for calendar handling. |
21
|
|
|
* |
22
|
|
|
* @author Yohan Giarelli <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class Calendar |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var Manager |
28
|
|
|
*/ |
29
|
|
|
private $eventManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var FactoryInterface |
33
|
|
|
*/ |
34
|
|
|
protected $factory; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Manager $eventManager |
38
|
|
|
*/ |
39
|
|
|
public function setEventManager(Manager $eventManager) |
40
|
|
|
{ |
41
|
|
|
$this->eventManager = $eventManager; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Manager |
46
|
|
|
*/ |
47
|
|
|
public function getEventManager() |
48
|
|
|
{ |
49
|
|
|
if (null === $this->eventManager) { |
50
|
|
|
$this->eventManager = new Manager(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->eventManager; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $yearOrStart |
58
|
|
|
* |
59
|
|
|
* @return Period\Year |
60
|
|
|
*/ |
61
|
|
|
public function getYear($yearOrStart) |
62
|
|
|
{ |
63
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
64
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-01-01', $yearOrStart)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->getFactory()->createYear($yearOrStart); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \DateTime|int $yearOrStart year if month is filled, month begin datetime otherwise |
72
|
|
|
* @param null|int $month number (1~12) |
73
|
|
|
* |
74
|
|
|
* @return PeriodInterface |
75
|
|
|
*/ |
76
|
|
|
public function getMonth($yearOrStart, $month = null) |
77
|
|
|
{ |
78
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
79
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-%s-01', $yearOrStart, $month)); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $this->getFactory()->createMonth($yearOrStart); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \DateTime|int $yearOrStart |
87
|
|
|
* @param null|int $week |
88
|
|
|
* |
89
|
|
|
* @return PeriodInterface |
90
|
|
|
*/ |
91
|
|
|
public function getWeek($yearOrStart, $week = null) |
92
|
|
|
{ |
93
|
|
|
$factory = $this->getFactory(); |
94
|
|
|
|
95
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
96
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-W%s', $yearOrStart, str_pad($week, 2, 0, STR_PAD_LEFT))); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $factory->createWeek($factory->findFirstDayOfWeek($yearOrStart)); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param \DateTime|int $yearOrStart |
104
|
|
|
* @param null|int $month |
105
|
|
|
* @param null|int $day |
106
|
|
|
* |
107
|
|
|
* @return PeriodInterface |
108
|
|
|
*/ |
109
|
|
|
public function getDay($yearOrStart, $month = null, $day = null) |
110
|
|
|
{ |
111
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
112
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-%s-%s', $yearOrStart, $month, $day)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->getFactory()->createDay($yearOrStart); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param \DateTime|int $yearOrStart |
120
|
|
|
* @param null|int $month |
121
|
|
|
* @param null|int $day |
122
|
|
|
* |
123
|
|
|
* @return PeriodInterface |
124
|
|
|
*/ |
125
|
|
|
public function getHour($yearOrStart, $month = null, $day = null, $hour = null) |
126
|
|
|
{ |
127
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
128
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-%s-%s %s:00', $yearOrStart, $month, $day, $hour)); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $this->getFactory()->createHour($yearOrStart); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param \DateTime|int $yearOrStart |
136
|
|
|
* @param null|int $month |
137
|
|
|
* @param null|int $day |
138
|
|
|
* |
139
|
|
|
* @return PeriodInterface |
140
|
|
|
*/ |
141
|
|
|
public function getMinute($yearOrStart, $month = null, $day = null, $hour = null, $minute = null) |
142
|
|
|
{ |
143
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
144
|
|
|
$yearOrStart = new \DateTime(sprintf('%s-%s-%s %s:%s', $yearOrStart, $month, $day, $hour, $minute)); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this->getFactory()->createMinute($yearOrStart); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param \DateTime|int $yearOrStart |
152
|
|
|
* @param null|int $month |
153
|
|
|
* @param null|int $day |
154
|
|
|
* |
155
|
|
|
* @return PeriodInterface |
156
|
|
|
*/ |
157
|
|
|
public function getSecond($yearOrStart, $month = null, $day = null, $hour = null, $minute = null, $second = null) |
158
|
|
|
{ |
159
|
|
|
if (!$yearOrStart instanceof \DateTime) { |
160
|
|
|
$yearOrStart = new \DateTime( |
161
|
|
|
sprintf('%s-%s-%s %s:%s:%s', $yearOrStart, $month, $day, $hour, $minute, $second) |
162
|
|
|
); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $this->getFactory()->createSecond($yearOrStart); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Period\PeriodInterface $period |
170
|
|
|
* @param array $options |
171
|
|
|
* |
172
|
|
|
* @return array<Event\EventInterface> |
|
|
|
|
173
|
|
|
*/ |
174
|
|
|
public function getEvents(PeriodInterface $period, array $options = array()) |
175
|
|
|
{ |
176
|
|
|
return $this->getEventManager()->find($period, $options); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param FactoryInterface $factory |
181
|
|
|
*/ |
182
|
|
|
public function setFactory(FactoryInterface $factory) |
183
|
|
|
{ |
184
|
|
|
$this->factory = $factory; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return FactoryInterface |
189
|
|
|
*/ |
190
|
|
|
public function getFactory() |
191
|
|
|
{ |
192
|
|
|
if (null === $this->factory) { |
193
|
|
|
$this->factory = new Factory(); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return $this->factory; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @param int $firstWeekday |
201
|
|
|
*/ |
202
|
|
|
public function setFirstWeekday($firstWeekday) |
203
|
|
|
{ |
204
|
|
|
$this->getFactory()->setFirstWeekday($firstWeekday); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
|
|
public function getFirstWeekday() |
211
|
|
|
{ |
212
|
|
|
return $this->factory->getFirstWeekday(); |
213
|
|
|
} |
214
|
|
|
} |
215
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.