1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the eluceo/iCal package. |
5
|
|
|
* |
6
|
|
|
* (c) Markus Poerschke <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Eluceo\iCal\Property\Event; |
13
|
|
|
|
14
|
|
|
use Eluceo\iCal\Property\ValueInterface; |
15
|
|
|
use Eluceo\iCal\ParameterBag; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Implementation of Recurrence Rule. |
20
|
|
|
* |
21
|
|
|
* @see http://www.ietf.org/rfc/rfc2445.txt 3.3.10. Recurrence Rule |
22
|
|
|
*/ |
23
|
|
|
class RecurrenceRule implements ValueInterface |
24
|
|
|
{ |
25
|
|
|
const FREQ_YEARLY = 'YEARLY'; |
26
|
|
|
const FREQ_MONTHLY = 'MONTHLY'; |
27
|
|
|
const FREQ_WEEKLY = 'WEEKLY'; |
28
|
|
|
const FREQ_DAILY = 'DAILY'; |
29
|
|
|
|
30
|
|
|
const WEEKDAY_SUNDAY = 'SU'; |
31
|
|
|
const WEEKDAY_MONDAY = 'MO'; |
32
|
|
|
const WEEKDAY_TUESDAY = 'TU'; |
33
|
|
|
const WEEKDAY_WEDNESDAY = 'WE'; |
34
|
|
|
const WEEKDAY_THURSDAY = 'TH'; |
35
|
|
|
const WEEKDAY_FRIDAY = 'FR'; |
36
|
|
|
const WEEKDAY_SATURDAY = 'SA'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The frequency of an Event. |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $freq = self::FREQ_YEARLY; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var null|int |
47
|
|
|
*/ |
48
|
|
|
protected $interval = 1; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var null|int |
52
|
|
|
*/ |
53
|
|
|
protected $count = null; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var null|\DateTime |
57
|
|
|
*/ |
58
|
|
|
protected $until = null; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var null|string |
62
|
|
|
*/ |
63
|
|
|
protected $wkst; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var null|string |
67
|
|
|
*/ |
68
|
|
|
protected $byMonth; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var null|string |
72
|
|
|
*/ |
73
|
|
|
protected $byWeekNo; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var null|string |
77
|
|
|
*/ |
78
|
|
|
protected $byYearDay; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var null|string |
82
|
|
|
*/ |
83
|
|
|
protected $byMonthDay; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var null|string |
87
|
|
|
*/ |
88
|
|
|
protected $byDay; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var null|string |
92
|
|
|
*/ |
93
|
|
|
protected $byHour; |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var null|string |
97
|
|
|
*/ |
98
|
|
|
protected $byMinute; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var null|string |
102
|
|
|
*/ |
103
|
|
|
protected $bySecond; |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Return the value of the Property as an escaped string. |
107
|
|
|
* |
108
|
|
|
* Escape values as per RFC 2445. See http://www.kanzaki.com/docs/ical/text.html |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
1 |
|
public function getEscapedValue() |
113
|
|
|
{ |
114
|
1 |
|
return $this->buildParameterBag()->toString(); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return ParameterBag |
119
|
|
|
*/ |
120
|
1 |
|
protected function buildParameterBag() |
121
|
|
|
{ |
122
|
1 |
|
$parameterBag = new ParameterBag(); |
123
|
|
|
|
124
|
1 |
|
$parameterBag->setParam('FREQ', $this->freq); |
125
|
|
|
|
126
|
1 |
|
if (null !== $this->interval) { |
127
|
|
|
$parameterBag->setParam('INTERVAL', $this->interval); |
128
|
|
|
} |
129
|
|
|
|
130
|
1 |
|
if (null !== $this->count) { |
131
|
|
|
$parameterBag->setParam('COUNT', $this->count); |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
if (null != $this->until) { |
135
|
1 |
|
$parameterBag->setParam('UNTIL', $this->until->format('Ymd\THis\Z')); |
136
|
|
|
} |
137
|
|
|
|
138
|
1 |
|
if (null !== $this->wkst) { |
139
|
|
|
$parameterBag->setParam('WKST', $this->wkst); |
140
|
|
|
} |
141
|
|
|
|
142
|
1 |
|
if (null !== $this->byMonth) { |
143
|
|
|
$parameterBag->setParam('BYMONTH', $this->byMonth); |
144
|
|
|
} |
145
|
|
|
|
146
|
1 |
|
if (null !== $this->byWeekNo) { |
147
|
|
|
$parameterBag->setParam('BYWEEKNO', $this->byWeekNo); |
148
|
|
|
} |
149
|
|
|
|
150
|
1 |
|
if (null !== $this->byYearDay) { |
151
|
|
|
$parameterBag->setParam('BYYEARDAY', $this->byYearDay); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
if (null !== $this->byMonthDay) { |
155
|
|
|
$parameterBag->setParam('BYMONTHDAY', $this->byMonthDay); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
if (null !== $this->byDay) { |
159
|
|
|
$parameterBag->setParam('BYDAY', $this->byDay); |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
if (null !== $this->byHour) { |
163
|
|
|
$parameterBag->setParam('BYHOUR', $this->byHour); |
164
|
|
|
} |
165
|
|
|
|
166
|
1 |
|
if (null !== $this->byMinute) { |
167
|
|
|
$parameterBag->setParam('BYMINUTE', $this->byMinute); |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
if (null !== $this->bySecond) { |
171
|
|
|
$parameterBag->setParam('BYSECOND', $this->bySecond); |
172
|
|
|
} |
173
|
|
|
|
174
|
1 |
|
return $parameterBag; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param int|null $count |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
|
|
public function setCount($count) |
183
|
|
|
{ |
184
|
|
|
$this->count = $count; |
185
|
|
|
|
186
|
|
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return int|null |
191
|
|
|
*/ |
192
|
|
|
public function getCount() |
193
|
|
|
{ |
194
|
|
|
return $this->count; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param \DateTime|null $until |
199
|
|
|
* |
200
|
|
|
* @return $this |
201
|
|
|
*/ |
202
|
1 |
|
public function setUntil(\DateTime $until = null) |
203
|
|
|
{ |
204
|
1 |
|
$this->until = $until; |
205
|
|
|
|
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return \DateTime|null |
211
|
|
|
*/ |
212
|
|
|
public function getUntil() |
213
|
|
|
{ |
214
|
|
|
return $this->until; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* The FREQ rule part identifies the type of recurrence rule. This |
219
|
|
|
* rule part MUST be specified in the recurrence rule. Valid values |
220
|
|
|
* include. |
221
|
|
|
* |
222
|
|
|
* SECONDLY, to specify repeating events based on an interval of a second or more; |
223
|
|
|
* MINUTELY, to specify repeating events based on an interval of a minute or more; |
224
|
|
|
* HOURLY, to specify repeating events based on an interval of an hour or more; |
225
|
|
|
* DAILY, to specify repeating events based on an interval of a day or more; |
226
|
|
|
* WEEKLY, to specify repeating events based on an interval of a week or more; |
227
|
|
|
* MONTHLY, to specify repeating events based on an interval of a month or more; |
228
|
|
|
* YEARLY, to specify repeating events based on an interval of a year or more. |
229
|
|
|
* |
230
|
|
|
* @param string $freq |
231
|
|
|
* |
232
|
|
|
* @return $this |
233
|
|
|
* |
234
|
|
|
* @throws \InvalidArgumentException |
235
|
|
|
*/ |
236
|
1 |
|
public function setFreq($freq) |
237
|
|
|
{ |
238
|
1 |
|
if (self::FREQ_YEARLY === $freq || self::FREQ_MONTHLY === $freq |
239
|
1 |
|
|| self::FREQ_WEEKLY === $freq |
240
|
1 |
|
|| self::FREQ_DAILY === $freq |
241
|
|
|
) { |
242
|
1 |
|
$this->freq = $freq; |
243
|
|
|
} else { |
244
|
|
|
throw new \InvalidArgumentException("The Frequency {$freq} is not supported."); |
245
|
|
|
} |
246
|
|
|
|
247
|
1 |
|
return $this; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getFreq() |
254
|
|
|
{ |
255
|
|
|
return $this->freq; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* The INTERVAL rule part contains a positive integer representing at |
260
|
|
|
* which intervals the recurrence rule repeats. |
261
|
|
|
* |
262
|
|
|
* @param int|null $interval |
263
|
|
|
* |
264
|
|
|
* @return $this |
265
|
|
|
*/ |
266
|
1 |
|
public function setInterval($interval) |
267
|
|
|
{ |
268
|
1 |
|
$this->interval = $interval; |
269
|
|
|
|
270
|
1 |
|
return $this; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @return int|null |
275
|
|
|
*/ |
276
|
|
|
public function getInterval() |
277
|
|
|
{ |
278
|
|
|
return $this->interval; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* The WKST rule part specifies the day on which the workweek starts. |
283
|
|
|
* Valid values are MO, TU, WE, TH, FR, SA, and SU. |
284
|
|
|
* |
285
|
|
|
* @param string $value |
286
|
|
|
* |
287
|
|
|
* @return $this |
288
|
|
|
*/ |
289
|
|
|
public function setWkst($value) |
290
|
|
|
{ |
291
|
|
|
$this->wkst = $value; |
292
|
|
|
|
293
|
|
|
return $this; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
298
|
|
|
* Valid values are 1 to 12. |
299
|
|
|
* |
300
|
|
|
* @param int $month |
301
|
|
|
* |
302
|
|
|
* @throws InvalidArgumentException |
303
|
|
|
* |
304
|
|
|
* @return $this |
305
|
|
|
*/ |
306
|
|
|
public function setByMonth($month) |
307
|
|
|
{ |
308
|
|
|
if (!is_integer($month) || $month < 0 || $month > 12) { |
309
|
|
|
throw new InvalidArgumentException('Invalid value for BYMONTH'); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
$this->byMonth = $month; |
|
|
|
|
313
|
|
|
|
314
|
|
|
return $this; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
319
|
|
|
* Valid values are 1 to 53 or -53 to -1. |
320
|
|
|
* |
321
|
|
|
* @param int $value |
322
|
|
|
* |
323
|
|
|
* @return $this |
324
|
|
|
*/ |
325
|
|
|
public function setByWeekNo($value) |
326
|
|
|
{ |
327
|
|
|
$this->byWeekNo = $value; |
|
|
|
|
328
|
|
|
|
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
334
|
|
|
* Valid values are 1 to 366 or -366 to -1. |
335
|
|
|
* |
336
|
|
|
* @param int $day |
337
|
|
|
* |
338
|
|
|
* @return $this |
339
|
|
|
*/ |
340
|
|
|
public function setByYearDay($day) |
341
|
|
|
{ |
342
|
|
|
$this->byYearDay = $day; |
|
|
|
|
343
|
|
|
|
344
|
|
|
return $this; |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
349
|
|
|
* Valid values are 1 to 31 or -31 to -1. |
350
|
|
|
* |
351
|
|
|
* @param int $day |
352
|
|
|
* |
353
|
|
|
* @return $this |
354
|
|
|
*/ |
355
|
|
|
public function setByMonthDay($day) |
356
|
|
|
{ |
357
|
|
|
$this->byMonthDay = $day; |
|
|
|
|
358
|
|
|
|
359
|
|
|
return $this; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
364
|
|
|
* |
365
|
|
|
* SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
366
|
|
|
* WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
367
|
|
|
* |
368
|
|
|
* Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
369
|
|
|
* If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
370
|
|
|
* |
371
|
|
|
* @param string $day |
372
|
|
|
* |
373
|
|
|
* @return $this |
374
|
|
|
*/ |
375
|
|
|
public function setByDay($day) |
376
|
|
|
{ |
377
|
|
|
$this->byDay = $day; |
378
|
|
|
|
379
|
|
|
return $this; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
384
|
|
|
* Valid values are 0 to 23. |
385
|
|
|
* |
386
|
|
|
* @param int $value |
387
|
|
|
* |
388
|
|
|
* @return $this |
389
|
|
|
* |
390
|
|
|
* @throws \InvalidArgumentException |
391
|
|
|
*/ |
392
|
|
View Code Duplication |
public function setByHour($value) |
|
|
|
|
393
|
|
|
{ |
394
|
|
|
if (!is_integer($value) || $value < 0 || $value > 23) { |
395
|
|
|
throw new \InvalidArgumentException('Invalid value for BYHOUR'); |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
$this->byHour = $value; |
|
|
|
|
399
|
|
|
|
400
|
|
|
return $this; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
405
|
|
|
* Valid values are 0 to 59. |
406
|
|
|
* |
407
|
|
|
* @param int $value |
408
|
|
|
* |
409
|
|
|
* @return $this |
410
|
|
|
* |
411
|
|
|
* @throws \InvalidArgumentException |
412
|
|
|
*/ |
413
|
|
View Code Duplication |
public function setByMinute($value) |
|
|
|
|
414
|
|
|
{ |
415
|
|
|
if (!is_integer($value) || $value < 0 || $value > 59) { |
416
|
|
|
throw new \InvalidArgumentException('Invalid value for BYMINUTE'); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
$this->byMinute = $value; |
|
|
|
|
420
|
|
|
|
421
|
|
|
return $this; |
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
426
|
|
|
* Valid values are 0 to 60. |
427
|
|
|
* |
428
|
|
|
* @param int $value |
429
|
|
|
* |
430
|
|
|
* @return $this |
431
|
|
|
* |
432
|
|
|
* @throws \InvalidArgumentException |
433
|
|
|
*/ |
434
|
|
View Code Duplication |
public function setBySecond($value) |
|
|
|
|
435
|
|
|
{ |
436
|
|
|
if (!is_integer($value) || $value < 0 || $value > 60) { |
437
|
|
|
throw new \InvalidArgumentException('Invalid value for BYSECOND'); |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
$this->bySecond = $value; |
|
|
|
|
441
|
|
|
|
442
|
|
|
return $this; |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..