Completed
Push — master ( cdc2ed...4a0186 )
by Markus
02:59 queued 01:46
created

RecurrenceRule::getUntil()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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
    /**
116
     * @var null|string
117
     */
118
    protected $bySecond;
119
120 11
    public function getEscapedValue(): string
121
    {
122 11
        return $this->buildParameterBag()->toString();
123
    }
124
125
    /**
126
     * @return ParameterBag
127
     */
128 11
    protected function buildParameterBag()
129
    {
130 11
        $parameterBag = new ParameterBag();
131
132 11
        $parameterBag->setParam('FREQ', $this->freq);
133
134 11
        if (null !== $this->interval) {
135 10
            $parameterBag->setParam('INTERVAL', $this->interval);
136
        }
137
138 11
        if (null !== $this->count) {
139 1
            $parameterBag->setParam('COUNT', $this->count);
140
        }
141
142 11
        if (null != $this->until) {
143 1
            $parameterBag->setParam('UNTIL', $this->until->format('Ymd\THis\Z'));
144
        }
145
146 11
        if (null !== $this->wkst) {
147 1
            $parameterBag->setParam('WKST', $this->wkst);
148
        }
149
150 11
        if (null !== $this->bySetPos && $this->canUseBySetPos) {
151
            $parameterBag->setParam('BYSETPOS', $this->bySetPos);
152
        }
153
154 11
        if (null !== $this->byMonth) {
155 1
            $parameterBag->setParam('BYMONTH', explode(',', $this->byMonth));
156
        }
157
158 11
        if (null !== $this->byWeekNo) {
159 1
            $parameterBag->setParam('BYWEEKNO', explode(',', $this->byWeekNo));
160
        }
161
162 11
        if (null !== $this->byYearDay) {
163 1
            $parameterBag->setParam('BYYEARDAY', explode(',', $this->byYearDay));
164
        }
165
166 11
        if (null !== $this->byMonthDay) {
167 1
            $parameterBag->setParam('BYMONTHDAY', explode(',', $this->byMonthDay));
168
        }
169
170 11
        if (null !== $this->byDay) {
171 1
            $parameterBag->setParam('BYDAY', explode(',', $this->byDay));
172
        }
173
174 11
        if (null !== $this->byHour) {
175 1
            $parameterBag->setParam('BYHOUR', explode(',', $this->byHour));
176
        }
177
178 11
        if (null !== $this->byMinute) {
179 1
            $parameterBag->setParam('BYMINUTE', explode(',', $this->byMinute));
180
        }
181
182 11
        if (null !== $this->bySecond) {
183 1
            $parameterBag->setParam('BYSECOND', explode(',', $this->bySecond));
184
        }
185
186 11
        return $parameterBag;
187
    }
188
189
    /**
190
     * @param int|null $count
191
     *
192
     * @return $this
193
     */
194 1
    public function setCount($count)
195
    {
196 1
        $this->count = $count;
197
198 1
        return $this;
199
    }
200
201
    /**
202
     * @return int|null
203
     */
204 2
    public function getCount()
205
    {
206 2
        return $this->count;
207
    }
208
209
    /**
210
     * @param \DateTimeInterface|null $until
211
     *
212
     * @return $this
213
     */
214 2
    public function setUntil(\DateTimeInterface $until = null)
215
    {
216 2
        $this->until = $until;
217
218 2
        return $this;
219
    }
220
221
    /**
222
     * @return \DateTimeInterface|null
223
     */
224 1
    public function getUntil()
225
    {
226 1
        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
     * 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
     *
242
     * @param string $freq
243
     *
244
     * @return $this
245
     *
246
     * @throws \InvalidArgumentException
247
     */
248 4
    public function setFreq($freq)
249
    {
250 4
        if (@constant('static::FREQ_' . $freq) !== null) {
251 3
            $this->freq = $freq;
252
        } else {
253 1
            throw new \InvalidArgumentException("The Frequency {$freq} is not supported.");
254
        }
255
256 3
        return $this;
257
    }
258
259
    /**
260
     * @return string
261
     */
262 1
    public function getFreq()
263
    {
264 1
        return $this->freq;
265
    }
266
267
    /**
268
     * The INTERVAL rule part contains a positive integer representing at
269
     * which intervals the recurrence rule repeats.
270
     *
271
     * @param int|null $interval
272
     *
273
     * @return $this
274
     */
275 3
    public function setInterval($interval)
276
    {
277 3
        $this->interval = $interval;
278
279 3
        return $this;
280
    }
281
282
    /**
283
     * @return int|null
284
     */
285 1
    public function getInterval()
286
    {
287 1
        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 1
    public function setWkst($value)
299
    {
300 1
        $this->wkst = $value;
301
302 1
        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
324
            return $this;
325
        }
326
327
        if (!(is_string($value) || is_array($value) || is_int($value))) {
328
            throw new InvalidArgumentException('Invalid value for BYSETPOS');
329
        }
330
331
        $list = $value;
332
333
        if (is_int($value)) {
334
            if ($value === 0 || $value < -366 || $value > 366) {
335
                throw new InvalidArgumentException('Invalid value for BYSETPOS');
336
            }
337
            $this->bySetPos = [$value];
338
339
            return $this;
340
        }
341
342
        if (is_string($value)) {
343
            $list = explode(',', $value);
344
        }
345
346
        $output = [];
347
348
        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...
349
            if (is_string($item)) {
350
                if (!preg_match('/^ *-?[0-9]* *$/', $item)) {
351
                    throw new InvalidArgumentException('Invalid value for BYSETPOS');
352
                }
353
                $item = intval($item);
354
            }
355
356
            if (!is_int($item) || $item === 0 || $item < -366 || $item > 366) {
357
                throw new InvalidArgumentException('Invalid value for BYSETPOS');
358
            }
359
360
            $output[] = $item;
361
        }
362
363
        $this->bySetPos = $output;
364
365
        return $this;
366
    }
367
368
    /**
369
     * The BYMONTH rule part specifies a COMMA-separated list of months of the year.
370
     * Valid values are 1 to 12.
371
     *
372
     * @param int $month
373
     *
374
     * @throws \InvalidArgumentException
375
     *
376
     * @return $this
377
     */
378 6
    public function setByMonth($month)
379
    {
380 6
        if (!is_integer($month) || $month <= 0 || $month > 12) {
381 5
            throw new InvalidArgumentException('Invalid value for BYMONTH');
382
        }
383
384 1
        $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...
385
386 1
        $this->canUseBySetPos = true;
387
388 1
        return $this;
389
    }
390
391
    /**
392
     * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year.
393
     * Valid values are 1 to 53 or -53 to -1.
394
     *
395
     * @param int $value
396
     *
397
     * @throws \InvalidArgumentException
398
     *
399
     * @return $this
400
     */
401 5 View Code Duplication
    public function setByWeekNo($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...
402
    {
403 5
        if (!is_integer($value) || $value > 53 || $value < -53 || $value === 0) {
404 4
            throw new InvalidArgumentException('Invalid value for BYWEEKNO');
405
        }
406
407 1
        $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...
408
409 1
        $this->canUseBySetPos = true;
410
411 1
        return $this;
412
    }
413
414
    /**
415
     * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year.
416
     * Valid values are 1 to 366 or -366 to -1.
417
     *
418
     * @param int $day
419
     *
420
     * @throws \InvalidArgumentException
421
     *
422
     * @return $this
423
     */
424 5 View Code Duplication
    public function setByYearDay($day)
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...
425
    {
426 5
        if (!is_integer($day) || $day > 366 || $day < -366 || $day === 0) {
427 4
            throw new InvalidArgumentException('Invalid value for BYYEARDAY');
428
        }
429
430 1
        $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...
431
432 1
        $this->canUseBySetPos = true;
433
434 1
        return $this;
435
    }
436
437
    /**
438
     * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month.
439
     * Valid values are 1 to 31 or -31 to -1.
440
     *
441
     * @param int $day
442
     *
443
     * @return $this
444
     *
445
     * @throws \InvalidArgumentException
446
     */
447 5 View Code Duplication
    public function setByMonthDay($day)
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...
448
    {
449 5
        if (!is_integer($day) || $day > 31 || $day < -31 || $day === 0) {
450 4
            throw new InvalidArgumentException('Invalid value for BYMONTHDAY');
451
        }
452
453 1
        $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...
454
455 1
        $this->canUseBySetPos = true;
456
457 1
        return $this;
458
    }
459
460
    /**
461
     * The BYDAY rule part specifies a COMMA-separated list of days of the week;.
462
     *
463
     * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday;
464
     * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday.
465
     *
466
     * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer.
467
     * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE".
468
     *
469
     * @param string $day
470
     *
471
     * @return $this
472
     */
473 1
    public function setByDay(string $day)
474
    {
475 1
        $this->byDay = $day;
476
477 1
        $this->canUseBySetPos = true;
478
479 1
        return $this;
480
    }
481
482
    /**
483
     * The BYHOUR rule part specifies a COMMA-separated list of hours of the day.
484
     * Valid values are 0 to 23.
485
     *
486
     * @param int $value
487
     *
488
     * @return $this
489
     *
490
     * @throws \InvalidArgumentException
491
     */
492 4
    public function setByHour($value)
493
    {
494 4
        if (!is_integer($value) || $value < 0 || $value > 23) {
495 3
            throw new \InvalidArgumentException('Invalid value for BYHOUR');
496
        }
497
498 1
        $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...
499
500 1
        $this->canUseBySetPos = true;
501
502 1
        return $this;
503
    }
504
505
    /**
506
     * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour.
507
     * Valid values are 0 to 59.
508
     *
509
     * @param int $value
510
     *
511
     * @return $this
512
     *
513
     * @throws \InvalidArgumentException
514
     */
515 4
    public function setByMinute($value)
516
    {
517 4
        if (!is_integer($value) || $value < 0 || $value > 59) {
518 3
            throw new \InvalidArgumentException('Invalid value for BYMINUTE');
519
        }
520
521 1
        $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...
522
523 1
        $this->canUseBySetPos = true;
524
525 1
        return $this;
526
    }
527
528
    /**
529
     * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute.
530
     * Valid values are 0 to 60.
531
     *
532
     * @param int $value
533
     *
534
     * @return $this
535
     *
536
     * @throws \InvalidArgumentException
537
     */
538 4
    public function setBySecond($value)
539
    {
540 4
        if (!is_integer($value) || $value < 0 || $value > 60) {
541 3
            throw new \InvalidArgumentException('Invalid value for BYSECOND');
542
        }
543
544 1
        $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...
545
546 1
        $this->canUseBySetPos = true;
547
548 1
        return $this;
549
    }
550
}
551