Completed
Pull Request — master (#113)
by
unknown
01:31
created

RecurrenceRule::getInterval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\ParameterBag;
15
use Eluceo\iCal\Property\ValueInterface;
16
use InvalidArgumentException;
17
18
/**
19
 * Implementation of Recurrence Rule.
20
 *
21
 * @see https://tools.ietf.org/html/rfc5545#section-3.8.5.3
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
    const FREQ_HOURLY = 'HOURLY';
30
    const FREQ_MINUTELY = 'MINUTELY';
31
    const FREQ_SECONDLY = 'SECONDLY';
32
33
    const WEEKDAY_SUNDAY = 'SU';
34
    const WEEKDAY_MONDAY = 'MO';
35
    const WEEKDAY_TUESDAY = 'TU';
36
    const WEEKDAY_WEDNESDAY = 'WE';
37
    const WEEKDAY_THURSDAY = 'TH';
38
    const WEEKDAY_FRIDAY = 'FR';
39
    const WEEKDAY_SATURDAY = 'SA';
40
41
    /**
42
     * The frequency of an Event.
43
     *
44
     * @var string
45
     */
46
    protected $freq = self::FREQ_YEARLY;
47
48
    /**
49
     * BYSETPOS must require use of other BY*.
50
     *
51
     * @var bool
52
     */
53
    protected $canUseBySetPos = false;
54
55
    /**
56
     * @var null|int
57
     */
58
    protected $interval = 1;
59
60
    /**
61
     * @var null|int
62
     */
63
    protected $count = null;
64
65
    /**
66
     * @var null|\DateTimeInterface
67
     */
68
    protected $until = null;
69
70
    /**
71
     * @var null|string
72
     */
73
    protected $wkst;
74
75
    /**
76
     * @var null|array
77
     */
78
    protected $bySetPos = null;
79
80
    /**
81
     * @var null|string
82
     */
83
    protected $byMonth;
84
85
    /**
86
     * @var null|string
87
     */
88
    protected $byWeekNo;
89
90
    /**
91
     * @var null|string
92
     */
93
    protected $byYearDay;
94
95
    /**
96
     * @var null|string
97
     */
98
    protected $byMonthDay;
99
100
    /**
101
     * @var null|string
102
     */
103
    protected $byDay;
104
105
    /**
106
     * @var null|string
107
     */
108
    protected $byHour;
109
110
    /**
111
     * @var null|string
112
     */
113
    protected $byMinute;
114
115 1
    /**
116
     * @var null|string
117 1
     */
118
    protected $bySecond;
119
120
    public function getEscapedValue(): string
121
    {
122
        return $this->buildParameterBag()->toString();
123 1
    }
124
125 1
    /**
126
     * @return ParameterBag
127 1
     */
128
    protected function buildParameterBag()
129 1
    {
130
        $parameterBag = new ParameterBag();
131
132
        $parameterBag->setParam('FREQ', $this->freq);
133 1
134
        if (null !== $this->interval) {
135
            $parameterBag->setParam('INTERVAL', $this->interval);
136
        }
137 1
138 1
        if (null !== $this->count) {
139 1
            $parameterBag->setParam('COUNT', $this->count);
140
        }
141 1
142
        if (null != $this->until) {
143
            $parameterBag->setParam('UNTIL', $this->until->format('Ymd\THis\Z'));
144
        }
145 1
146
        if (null !== $this->wkst) {
147
            $parameterBag->setParam('WKST', $this->wkst);
148
        }
149 1
150
        if (null !== $this->bySetPos && $this->canUseBySetPos) {
151
            $parameterBag->setParam('BYSETPOS', $this->bySetPos);
152
        }
153 1
154
        if (null !== $this->byMonth) {
155
            $parameterBag->setParam('BYMONTH', explode(',', $this->byMonth));
156
        }
157 1
158
        if (null !== $this->byWeekNo) {
159
            $parameterBag->setParam('BYWEEKNO', explode(',', $this->byWeekNo));
160
        }
161 1
162
        if (null !== $this->byYearDay) {
163
            $parameterBag->setParam('BYYEARDAY', explode(',', $this->byYearDay));
164
        }
165 1
166
        if (null !== $this->byMonthDay) {
167
            $parameterBag->setParam('BYMONTHDAY', explode(',', $this->byMonthDay));
168
        }
169 1
170
        if (null !== $this->byDay) {
171
            $parameterBag->setParam('BYDAY', explode(',', $this->byDay));
172
        }
173 1
174
        if (null !== $this->byHour) {
175
            $parameterBag->setParam('BYHOUR', explode(',', $this->byHour));
176
        }
177 1
178
        if (null !== $this->byMinute) {
179
            $parameterBag->setParam('BYMINUTE', explode(',', $this->byMinute));
180
        }
181
182
        if (null !== $this->bySecond) {
183
            $parameterBag->setParam('BYSECOND', explode(',', $this->bySecond));
184
        }
185
186
        return $parameterBag;
187
    }
188
189
    /**
190
     * @param int|null $count
191
     *
192
     * @return $this
193
     */
194
    public function setCount($count)
195
    {
196
        $this->count = $count;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @return int|null
203
     */
204
    public function getCount()
205 1
    {
206
        return $this->count;
207 1
    }
208
209 1
    /**
210
     * @param \DateTimeInterface|null $until
211
     *
212
     * @return $this
213
     */
214
    public function setUntil(\DateTimeInterface $until = null)
215
    {
216
        $this->until = $until;
217
218
        return $this;
219
    }
220
221
    /**
222
     * @return \DateTimeInterface|null
223
     */
224
    public function getUntil()
225
    {
226
        return $this->until;
227
    }
228
229
    /**
230
     * The FREQ rule part identifies the type of recurrence rule.  This
231
     * rule part MUST be specified in the recurrence rule.  Valid values
232
     * include.
233
     *
234
     * SECONDLY, to specify repeating events based on an interval of a second or more;
235
     * MINUTELY, to specify repeating events based on an interval of a minute or more;
236
     * HOURLY, to specify repeating events based on an interval of an hour or more;
237
     * DAILY, to specify repeating events based on an interval of a day or more;
238
     * WEEKLY, to specify repeating events based on an interval of a week or more;
239 1
     * MONTHLY, to specify repeating events based on an interval of a month or more;
240
     * YEARLY, to specify repeating events based on an interval of a year or more.
241 1
     *
242 1
     * @param string $freq
243 1
     *
244
     * @return $this
245
     *
246
     * @throws \InvalidArgumentException
247 1
     */
248
    public function setFreq($freq)
249
    {
250
        if (@constant('static::FREQ_' . $freq) !== null) {
251
            $this->freq = $freq;
252
        } else {
253
            throw new \InvalidArgumentException("The Frequency {$freq} is not supported.");
254
        }
255
256
        return $this;
257
    }
258
259
    /**
260
     * @return string
261
     */
262
    public function getFreq()
263
    {
264
        return $this->freq;
265
    }
266 1
267
    /**
268 1
     * The INTERVAL rule part contains a positive integer representing at
269
     * which intervals the recurrence rule repeats.
270 1
     *
271
     * @param int|null $interval
272
     *
273
     * @return $this
274
     */
275
    public function setInterval($interval)
276
    {
277
        $this->interval = $interval;
278
279
        return $this;
280
    }
281
282
    /**
283
     * @return int|null
284
     */
285
    public function getInterval()
286
    {
287
        return $this->interval;
288
    }
289
290
    /**
291
     * The WKST rule part specifies the day on which the workweek starts.
292
     * Valid values are MO, TU, WE, TH, FR, SA, and SU.
293
     *
294
     * @param string $value
295
     *
296
     * @return $this
297
     */
298
    public function setWkst($value)
299
    {
300
        $this->wkst = $value;
301
302
        return $this;
303
    }
304
305
    /**
306
     * The BYSETPOS filters one interval of events by the specified position.
307
     * A positive position will start from the beginning and go forward while
308
     * a negative position will start at the end and move backward.
309
     *
310
     * Valid values are a comma separated string or an array of integers
311
     * from 1 to 366 or negative integers from -1 to -366.
312
     *
313
     * @param null|int|string|array $value
314
     *
315
     * @throws InvalidArgumentException
316
     *
317
     * @return $this
318
     */
319
    public function setBySetPos($value)
320
    {
321
        if (null === $value) {
322
            $this->bySetPos = $value;
323
            return $this;
324
        }
325
        if (!(is_string($value) || is_array($value) || is_int($value))) {
326
            throw new InvalidArgumentException('Invalid value for BYSETPOS');
327
        }
328
        $list = $value;
329
        if (is_int($value)) {
330
            if ($value === 0 || $value < -366 || $value > 366) {
331
                throw new InvalidArgumentException('Invalid value for BYSETPOS');
332
            }
333
            $this->bySetPos = [$value];
334
            return $this;
335
        }
336
        if (is_string($value)) $list = explode(',', $value);
337
        $output = [];
338
        foreach ($list as $item) {
0 ignored issues
show
Bug introduced by
The expression $list of type string|array|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
339
            if (is_string($item)) {
340
                if (!preg_match('/^ *-?[0-9]* *$/', $item)) {
341
                    throw new InvalidArgumentException('Invalid value for BYSETPOS');
342
                }
343
                $item = intval($item);
344
            }
345
            if (!is_int($item) || $item === 0 || $item < -366 || $item > 366) {
346
                throw new InvalidArgumentException('Invalid value for BYSETPOS');
347
            }
348
            $output[] = $item;
349
        }
350
        $this->bySetPos = $output;
351
352
        return $this;
353
    }
354
355
    /**
356
     * The BYMONTH rule part specifies a COMMA-separated list of months of the year.
357
     * Valid values are 1 to 12.
358
     *
359
     * @param int $month
360
     *
361
     * @throws InvalidArgumentException
362
     *
363
     * @return $this
364
     */
365 View Code Duplication
    public function setByMonth($month)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
    {
367
        if (!is_integer($month) || $month < 0 || $month > 12) {
368
            throw new InvalidArgumentException('Invalid value for BYMONTH');
369
        }
370
371
        $this->byMonth = $month;
0 ignored issues
show
Documentation Bug introduced by
It seems like $month of type integer is incompatible with the declared type null|string of property $byMonth.

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..

Loading history...
372
373
        $this->canUseBySetPos = true;
374
375
        return $this;
376
    }
377
378
    /**
379
     * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year.
380
     * Valid values are 1 to 53 or -53 to -1.
381
     *
382
     * @param int $value
383
     *
384
     * @return $this
385
     */
386
    public function setByWeekNo($value)
387
    {
388
        $this->byWeekNo = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type integer is incompatible with the declared type null|string of property $byWeekNo.

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..

Loading history...
389
390
        $this->canUseBySetPos = true;
391
392
        return $this;
393
    }
394
395
    /**
396
     * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year.
397
     * Valid values are 1 to 366 or -366 to -1.
398
     *
399
     * @param int $day
400
     *
401
     * @return $this
402
     */
403
    public function setByYearDay($day)
404
    {
405
        $this->byYearDay = $day;
0 ignored issues
show
Documentation Bug introduced by
It seems like $day of type integer is incompatible with the declared type null|string of property $byYearDay.

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..

Loading history...
406
407
        $this->canUseBySetPos = true;
408
409
        return $this;
410
    }
411
412
    /**
413
     * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month.
414
     * Valid values are 1 to 31 or -31 to -1.
415
     *
416
     * @param int $day
417
     *
418
     * @return $this
419
     */
420
    public function setByMonthDay($day)
421
    {
422
        $this->byMonthDay = $day;
0 ignored issues
show
Documentation Bug introduced by
It seems like $day of type integer is incompatible with the declared type null|string of property $byMonthDay.

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..

Loading history...
423
424
        $this->canUseBySetPos = true;
425
426
        return $this;
427
    }
428
429
    /**
430
     * The BYDAY rule part specifies a COMMA-separated list of days of the week;.
431
     *
432
     * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday;
433
     * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday.
434
     *
435
     * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer.
436
     * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE".
437
     *
438
     * @param string $day
439
     *
440
     * @return $this
441
     */
442
    public function setByDay(string $day)
443
    {
444
        $this->byDay = $day;
445
446
        $this->canUseBySetPos = true;
447
448
        return $this;
449
    }
450
451
    /**
452
     * The BYHOUR rule part specifies a COMMA-separated list of hours of the day.
453
     * Valid values are 0 to 23.
454
     *
455
     * @param int $value
456
     *
457
     * @return $this
458
     *
459
     * @throws \InvalidArgumentException
460
     */
461 View Code Duplication
    public function setByHour($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
462
    {
463
        if (!is_integer($value) || $value < 0 || $value > 23) {
464
            throw new \InvalidArgumentException('Invalid value for BYHOUR');
465
        }
466
467
        $this->byHour = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type integer is incompatible with the declared type null|string of property $byHour.

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..

Loading history...
468
469
        $this->canUseBySetPos = true;
470
471
        return $this;
472
    }
473
474
    /**
475
     * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour.
476
     * Valid values are 0 to 59.
477
     *
478
     * @param int $value
479
     *
480
     * @return $this
481
     *
482
     * @throws \InvalidArgumentException
483
     */
484 View Code Duplication
    public function setByMinute($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
485
    {
486
        if (!is_integer($value) || $value < 0 || $value > 59) {
487
            throw new \InvalidArgumentException('Invalid value for BYMINUTE');
488
        }
489
490
        $this->byMinute = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type integer is incompatible with the declared type null|string of property $byMinute.

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..

Loading history...
491
492
        $this->canUseBySetPos = true;
493
494
        return $this;
495
    }
496
497
    /**
498
     * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute.
499
     * Valid values are 0 to 60.
500
     *
501
     * @param int $value
502
     *
503
     * @return $this
504
     *
505
     * @throws \InvalidArgumentException
506
     */
507 View Code Duplication
    public function setBySecond($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
508
    {
509
        if (!is_integer($value) || $value < 0 || $value > 60) {
510
            throw new \InvalidArgumentException('Invalid value for BYSECOND');
511
        }
512
513
        $this->bySecond = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type integer is incompatible with the declared type null|string of property $bySecond.

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..

Loading history...
514
515
        $this->canUseBySetPos = true;
516
517
        return $this;
518
    }
519
}
520