Completed
Pull Request — master (#18)
by Samuel
03:55
created

ExcludeDaysOfWeekConstraint::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Recurrence\Constraint;
4
5
use Recurrence\Model\Recurrence;
6
7
/**
8
 * Class ExcludeDaysOfWeekConstraint
9
 * @package Recurrence\Constraint
10
 */
11
class ExcludeDaysOfWeekConstraint implements RecurrenceConstraintInterface
12
{
13
14
    /**
15
     * @var array
16
     */
17
    private $excludedDays = [];
18
19
    /**
20
     * @param array $excludedDays
21
     */
22
    public function __construct(array $excludedDays = [])
23
    {
24
        $this->excludedDays = array_unique($excludedDays);
25
26
        sort($this->excludedDays);
27
28
        if (count($this->excludedDays) >= 7) {
29
            throw new \InvalidArgumentException('At least one day of the week must be allowed');
30
        }
31
    }
32
33
    /**
34
     * @param Recurrence $recurrence
35
     * @param \Datetime $datetime
36
     * @return \Datetime
37
     */
38
    public function apply(Recurrence $recurrence, \Datetime $datetime)
39
    {
40
        while (in_array((int) $datetime->format('N'), $this->excludedDays)) {
41
            $datetime->modify('+1 day');
42
        }
43
44
        return $datetime;
45
    }
46
}
47