Completed
Push — master ( 5c0f08...da892c )
by Markus
05:55 queued 03:22
created

RecurrenceRule::setByMinute()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

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