Completed
Pull Request — master (#345)
by
unknown
05:39
created

DayOfWeekCollection::fromUdb3ModelDays()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
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
use CultuurNet\UDB3\Model\ValueObject\Calendar\OpeningHours\Day;
7
use CultuurNet\UDB3\Model\ValueObject\Calendar\OpeningHours\Days;
8
9
/**
10
 * @todo Replace by CultuurNet\UDB3\Model\ValueObject\Calendar\OpeningHours\Days.
11
 */
12
class DayOfWeekCollection implements SerializableInterface
13
{
14
    /**
15
     * @var string[]
16
     */
17
    private $daysOfWeek = [];
18
19
    /**
20
     * DayOfWeekCollection constructor.
21
     * @param DayOfWeek[] ...$daysOfWeek
22
     */
23
    public function __construct(DayOfWeek ...$daysOfWeek)
24
    {
25
        array_walk($daysOfWeek, [$this, 'addDayOfWeek']);
26
    }
27
28
    /**
29
     * Keeps the collection of days of week unique.
30
     * Makes sure that the objects are stored as strings to allow PHP serialize method.
31
     *
32
     * @param DayOfWeek $dayOfWeek
33
     * @return DayOfWeekCollection
34
     */
35
    public function addDayOfWeek(DayOfWeek $dayOfWeek)
36
    {
37
        $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...
38
            array_merge(
39
                $this->daysOfWeek,
40
                [
41
                    $dayOfWeek->toNative(),
42
                ]
43
            )
44
        );
45
46
        return $this;
47
    }
48
49
    /**
50
     * @return DayOfWeek[]
51
     */
52
    public function getDaysOfWeek()
53
    {
54
        return array_map(
55
            function ($dayOfWeek) {
56
                return DayOfWeek::fromNative($dayOfWeek);
57
            },
58
            $this->daysOfWeek
59
        );
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public static function deserialize(array $data)
66
    {
67
        return array_reduce(
68
            $data,
69
            function (DayOfWeekCollection $collection, $dayOfWeek) {
70
                 return $collection->addDayOfWeek(DayOfWeek::fromNative($dayOfWeek));
71
            },
72
            new DayOfWeekCollection()
73
        );
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function serialize()
80
    {
81
        return $this->daysOfWeek;
82
    }
83
84
    /**
85
     * @param Days $days
86
     * @return self
87
     */
88
    public static function fromUdb3ModelDays(Days $days)
89
    {
90
        $days = array_map(
91
            function (Day $day) {
92
                return DayOfWeek::fromUdb3ModelDay($day);
93
            },
94
            $days->toArray()
95
        );
96
97
        return new self(...$days);
98
    }
99
}
100