/
Recurrence.php
520 lines (464 loc) · 12.6 KB
/
Recurrence.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
<?php
namespace Oro\Bundle\CalendarBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Recurrence Entity.
*
* @ORM\Table(
* name="oro_calendar_recurrence",
* indexes={
* @ORM\Index(name="oro_calendar_r_start_time_idx", columns={"start_time"}),
* @ORM\Index(name="oro_calendar_r_end_time_idx", columns={"end_time"}),
* @ORM\Index(name="oro_calendar_r_c_end_time_idx", columns={"calculated_end_time"})
* }
* )
* @ORM\Entity
*/
class Recurrence
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\OneToOne(
* targetEntity="Oro\Bundle\CalendarBundle\Entity\CalendarEvent",
* mappedBy="recurrence"
* )
*/
protected $calendarEvent;
/**
* Determines what recurrence strategy must be used to calculate occurrences of recurring event,
* to get textual representation etc. Possible values are: daily, weekly, monthly, monthnth, yearly, yearnth.
*
* Constants for possible values:
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_DAILY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_WEEKLY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_MONTHLY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_MONTH_N_TH
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_YEARLY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::TYPE_YEAR_N_TH
*
* Corresponding strategies:
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\DailyStrategy
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\WeeklyStrategy
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\MonthlyStrategy
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\MonthNthStrategy
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\YearlyStrategy
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\YearNthStrategy
*
* @var string
*
* @ORM\Column(name="recurrence_type", type="string", length=16)
*/
protected $recurrenceType;
/**
* Contains number of units how often recurring events must repeat.
* For example, 'every X days', 'every X weeks', 'every X months' (where X is a value of $interval).
*
* Units of this attribute depend of recurrenceType.
* For daily recurrence it is number of days.
* For weekly recurrence it is number of weeks.
* For monthly, monthnth recurrences it is number of months.
* For yearly, yearnth recurrences it is number of month, which is multiple of 12. I.e. 12, 24, 36 etc.
*
* @var int
*
* @ORM\Column(name="`interval`", type="integer")
*/
protected $interval;
/**
* Contains a value from 1 to 5, that is relative value for 'first', 'second', 'third', 'fourth' and 'last'.
*
* It is used in monthnth and yearnth strategies, for creating recurring events like:
* 'Yearly every 2 years on the first Saturday of April',
* 'Monthly the fourth Saturday of every 2 months',
* 'Yearly every 2 years on the last Saturday of April'.
*
* Constants for possible values:
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::INSTANCE_FIRST
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::INSTANCE_SECOND
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::INSTANCE_THIRD
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::INSTANCE_FOURTH
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::INSTANCE_LAST
*
* @var int
*
* @ORM\Column(name="instance", type="integer", nullable=true)
*/
protected $instance;
/**
* Contains array of weekdays.
*
* Possible values: 'sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'.
* For relative 'weekday' value the array will be ['monday', 'tuesday', 'wednesday', 'thursday', 'friday'].
* For relative 'weekend' value the array will be ['sunday', 'saturday'].
* For relative 'day' value
* the array will be ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'].
* In other cases there is no relative value and $dayOfWeek array contains one of possible values.
*
* Constants for days:
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_SUNDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_MONDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_TUESDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_WEDNESDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_THURSDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_FRIDAY
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence::DAY_SATURDAY
*
* @var string[]
*
* @ORM\Column(name="day_of_week", type="array", nullable=true)
*/
protected $dayOfWeek;
/**
* Contains day of month that is used by monthly and yearly strategies.
*
* @var int
*
* @ORM\Column(name="day_of_month", type="integer", nullable=true)
*/
protected $dayOfMonth;
/**
* Contains month number that is used by yearly and yearnth strategies.
*
* @var int
*
* @ORM\Column(name="month_of_year", type="integer", nullable=true)
*/
protected $monthOfYear;
/**
* Start datetime for range of recurrence.
*
* @var \DateTime
*
* @ORM\Column(name="start_time", type="datetime")
*/
protected $startTime;
/**
* End datetime for range of recurrence.
*
* @var \DateTime
*
* @ORM\Column(name="end_time", type="datetime", nullable=true)
*/
protected $endTime;
/**
* Contains calculated end datetime for range of recurrence.
* It is used for optimization of SQL query responsible to get all events in some range of time.
*
* @see \Oro\Bundle\CalendarBundle\Model\Recurrence\StrategyInterface::getCalculatedEndTime
*
* @var \DateTime
*
* @ORM\Column(name="calculated_end_time", type="datetime")
*/
protected $calculatedEndTime;
/**
* Contains the number of occurrences for range of recurrence.
* It means that recurrence ends after X occurrences, where X is value of $occurrences.
*
* @var int
*
* @ORM\Column(name="occurrences", type="integer", nullable=true)
*/
protected $occurrences;
/**
* The time zone in which the time is specified.
* For recurring events this field is required and specifies the time zone in which the recurrence is expanded.
*
* The list of supported Timezones http://php.net/manual/en/timezones.php
*
* @var string
*
* @ORM\Column(name="timezone", type="string", length=255)
*/
protected $timeZone;
/**
* Gets id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* @param CalendarEvent $calendarEvent
*
* @return self
*/
public function setCalendarEvent(CalendarEvent $calendarEvent)
{
$this->calendarEvent = $calendarEvent;
return $this;
}
/**
* @return CalendarEvent
*/
public function getCalendarEvent()
{
return $this->calendarEvent;
}
/**
* Sets recurrenceType.
*
* @param string $recurrenceType
*
* @return self
*/
public function setRecurrenceType($recurrenceType)
{
$this->recurrenceType = $recurrenceType;
return $this;
}
/**
* Gets recurrenceType.
*
* @return string
*/
public function getRecurrenceType()
{
return $this->recurrenceType;
}
/**
* Sets interval.
*
* @param integer $interval
*
* @return self
*/
public function setInterval($interval)
{
$this->interval = $interval;
return $this;
}
/**
* Gets interval.
*
* @return integer
*/
public function getInterval()
{
return $this->interval;
}
/**
* Sets instance.
*
* @param integer|null $instance
*
* @return self
*/
public function setInstance($instance)
{
$this->instance = $instance;
return $this;
}
/**
* Gets instance.
*
* @return integer|null
*/
public function getInstance()
{
return $this->instance;
}
/**
* Sets dayOfWeek.
*
* @param array|null $dayOfWeek
*
* @return self
*/
public function setDayOfWeek(array $dayOfWeek = null)
{
$this->dayOfWeek = $dayOfWeek;
return $this;
}
/**
* Gets dayOfWeek.
*
* @return array|null
*/
public function getDayOfWeek()
{
return $this->dayOfWeek;
}
/**
* Sets dayOfMonth.
*
* @param integer|null $dayOfMonth
*
* @return self
*/
public function setDayOfMonth($dayOfMonth)
{
$this->dayOfMonth = $dayOfMonth;
return $this;
}
/**
* Gets dayOfMonth.
*
* @return integer|null
*/
public function getDayOfMonth()
{
return $this->dayOfMonth;
}
/**
* Sets monthOfYear.
*
* @param integer|null $monthOfYear
*
* @return self
*/
public function setMonthOfYear($monthOfYear)
{
$this->monthOfYear = $monthOfYear;
return $this;
}
/**
* Gets monthOfYear.
*
* @return integer|null
*/
public function getMonthOfYear()
{
return $this->monthOfYear;
}
/**
* Sets startTime.
*
* @param \DateTime|null $startTime
*
* @return self
*/
public function setStartTime(\DateTime $startTime = null)
{
$this->startTime = $startTime;
return $this;
}
/**
* Gets startTime.
*/
public function getStartTime(): ?\DateTime
{
return $this->startTime;
}
/**
* Sets endTime.
*
* @param \DateTime|null $endTime
*
* @return self
*/
public function setEndTime(\DateTime $endTime = null)
{
$this->endTime = $endTime;
return $this;
}
/**
* Gets endTime.
*/
public function getEndTime(): ?\DateTime
{
return $this->endTime;
}
/**
* @param \DateTime|null $calculatedEndTime
*
* @return self
*/
public function setCalculatedEndTime($calculatedEndTime)
{
$this->calculatedEndTime = $calculatedEndTime;
return $this;
}
/**
* @return \DateTime|null
*/
public function getCalculatedEndTime()
{
return $this->calculatedEndTime;
}
/**
* Sets occurrences.
*
* @param integer|null $occurrences
*
* @return self
*/
public function setOccurrences($occurrences)
{
$this->occurrences = $occurrences;
return $this;
}
/**
* Gets occurrences.
*
* @return integer|null
*/
public function getOccurrences()
{
return $this->occurrences;
}
/**
* Sets time zone.
*
* @param string $timeZone
*
* @return self
*/
public function setTimeZone($timeZone)
{
$this->timeZone = $timeZone;
return $this;
}
/**
* Gets time zone.
*
* @return string
*/
public function getTimeZone()
{
return $this->timeZone;
}
/**
* Compares instance with another instance and not taking into account relation to event.
*
* @param Recurrence|null $other
* @return bool
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function isEqual($other)
{
if (!$other instanceof Recurrence) {
return false;
}
return
$this->recurrenceType == $other->recurrenceType &&
$this->interval == $other->interval &&
$this->instance == $other->instance &&
$this->dayOfWeek == $other->dayOfWeek &&
$this->dayOfMonth == $other->dayOfMonth &&
$this->monthOfYear == $other->monthOfYear &&
$this->isDateTimeValueEqual($this->startTime, $other->startTime) &&
$this->isDateTimeValueEqual($this->endTime, $other->endTime) &&
$this->occurrences == $other->occurrences &&
$this->timeZone == $other->timeZone;
}
/**
* @param \DateTime|null $source
* @param \DateTime|null $target
* @return bool
*/
protected function isDateTimeValueEqual(\DateTime $source = null, \DateTime $target = null)
{
if ($source && $target) {
return $source->format('U') == $target->format('U');
}
return !$source && !$target;
}
}