Completed
Pull Request — master (#296)
by Luc
08:35 queued 03:51
created

DayOfWeekCollection::addDayOfWeek()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Calendar;
4
5
use Broadway\Serializer\SerializableInterface;
6
7
class DayOfWeekCollection implements SerializableInterface
8
{
9
    /**
10
     * @var string[]
11
     */
12
    private $daysOfWeek = [];
13
14
    /**
15
     * DayOfWeekCollection constructor.
16
     * @param DayOfWeek|null $dayOfWeek
17
     */
18
    public function __construct(DayOfWeek $dayOfWeek = null)
19
    {
20
        if ($dayOfWeek !== null) {
21
            $this->addDayOfWeek($dayOfWeek);
22
        }
23
    }
24
25
    /**
26
     * Keeps the collection of days of week unique.
27
     * Makes sure that the objects are stored as strings to allow PHP serialize method.
28
     *
29
     * @param DayOfWeek $dayOfWeek
30
     * @return DayOfWeekCollection
31
     */
32
    public function addDayOfWeek(DayOfWeek $dayOfWeek)
33
    {
34
        $this->daysOfWeek = array_unique(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_unique(array_merge...ayOfWeek->toNative()))) of type array<integer,string|nul...nteger|double|string"}> is incompatible with the declared type array<integer,string> of property $daysOfWeek.

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...
35
            array_merge(
36
                $this->daysOfWeek,
37
                [
38
                    $dayOfWeek->toNative(),
39
                ]
40
            )
41
        );
42
43
        return $this;
44
    }
45
46
    /**
47
     * @return DayOfWeek[]
48
     */
49
    public function getDaysOfWeek()
50
    {
51
        return array_map(
52
            function ($dayOfWeek) {
53
                return DayOfWeek::fromNative($dayOfWeek);
54
            },
55
            $this->daysOfWeek
56
        );
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public static function deserialize(array $data)
63
    {
64
        $dayOfWeekCollection = new DayOfWeekCollection();
65
66
        foreach ($data as $dayOfWeek) {
67
            $dayOfWeekCollection->addDayOfWeek(
68
                DayOfWeek::fromNative($dayOfWeek)
69
            );
70
        }
71
72
        return $dayOfWeekCollection;
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function serialize()
79
    {
80
        return $this->daysOfWeek;
81
    }
82
}
83