Completed
Pull Request — master (#296)
by Luc
05:01
created

OpeningHour::deserialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\Calendar;
4
5
use Broadway\Serializer\SerializableInterface;
6
7
class OpeningHour implements SerializableInterface
8
{
9
    /**
10
     * @var OpeningTime
11
     */
12
    private $opens;
13
14
    /**
15
     * @var OpeningTime
16
     */
17
    private $closes;
18
19
    /**
20
     * @var DayOfWeek[]
21
     */
22
    private $daysOfWeek;
23
24
    /**
25
     * OpeningHour constructor.
26
     * @param OpeningTime $opens
27
     * @param OpeningTime $closes
28
     * @param DayOfWeek[] $daysOfWeek
29
     */
30
    public function __construct(
31
        OpeningTime $opens,
32
        OpeningTime $closes,
33
        DayOfWeek ...$daysOfWeek
34
    ) {
35
        $this->daysOfWeek = $daysOfWeek;
0 ignored issues
show
Documentation Bug introduced by
It seems like $daysOfWeek of type array<integer,array<inte...3\Calendar\DayOfWeek>>> is incompatible with the declared type array<integer,object<Cul...B3\Calendar\DayOfWeek>> 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...
36
        $this->opens = $opens;
37
        $this->closes = $closes;
38
    }
39
40
    /**
41
     * @param DayOfWeek[] $dayOfWeeks
42
     */
43
    public function addDaysOfWeek(DayOfWeek ...$dayOfWeeks)
44
    {
45
        $this->daysOfWeek = array_merge($this->daysOfWeek, $dayOfWeeks);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->daysOfWeek, $dayOfWeeks) of type array<integer,object<Cul...3\Calendar\DayOfWeek>>> is incompatible with the declared type array<integer,object<Cul...B3\Calendar\DayOfWeek>> 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...
46
    }
47
48
    /**
49
     * @return OpeningTime
50
     */
51
    public function getOpens()
52
    {
53
        return $this->opens;
54
    }
55
56
    /**
57
     * @return OpeningTime
58
     */
59
    public function getCloses()
60
    {
61
        return $this->closes;
62
    }
63
64
    /**
65
     * @return DayOfWeek[]
66
     */
67
    public function getDaysOfWeek()
68
    {
69
        return $this->daysOfWeek;
70
    }
71
72
    /**
73
     * @param OpeningHour $otherOpeningHour
74
     * @return bool
75
     */
76
    public function hasEqualHours(OpeningHour $otherOpeningHour)
77
    {
78
        return $otherOpeningHour->getOpens()->sameValueAs($this->getOpens()) &&
79
            $otherOpeningHour->getCloses()->sameValueAs($this->getCloses());
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public static function deserialize(array $data)
86
    {
87
        $weekDays = array_map(
88
            function ($dayOfWeek) {
89
                return DayOfWeek::fromNative($dayOfWeek);
90
            },
91
            $data['dayOfWeek']
92
        );
93
94
        return new static(
95
            OpeningTime::fromNativeString($data['opens']),
96
            OpeningTime::fromNativeString($data['closes']),
97
            ...$weekDays
98
        );
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function serialize()
105
    {
106
        $serializedWeekDays = array_map(
107
            function (DayOfWeek $dayOfWeek) {
108
                return $dayOfWeek->getValue();
109
            },
110
            $this->daysOfWeek
111
        );
112
113
        return [
114
            'opens' => $this->opens->toNativeString(),
115
            'closes' => $this->closes->toNativeString(),
116
            'dayOfWeek' => $serializedWeekDays
117
        ];
118
    }
119
}
120