BookingInfo::getAvailabilityStarts()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use CultuurNet\UDB3\Model\ValueObject\Contact\BookingInfo as Udb3ModelBookingInfo;
6
use CultuurNet\UDB3\ValueObject\MultilingualString;
7
use DateTimeImmutable;
8
9
final class BookingInfo implements JsonLdSerializableInterface
10
{
11
    /**
12
     * @var string|null
13
     */
14
    protected $phone;
15
16
    /**
17
     * @var string|null
18
     */
19
    protected $email;
20
21
    /**
22
     * @var string|null
23
     */
24
    protected $url;
25
26
    /**
27
     * @var MultilingualString|null
28
     */
29
    protected $urlLabel;
30
31
    /**
32
     * @var DateTimeImmutable|null
33
     */
34
    protected $availabilityStarts;
35
36
    /**
37
     * @var DateTimeImmutable|null
38
     */
39
    protected $availabilityEnds;
40
41
    public function __construct(
42
        ?string $url = null,
43
        ?MultilingualString $urlLabel = null,
44
        ?string $phone = null,
45
        ?string $email = null,
46
        ?DateTimeImmutable $availabilityStarts = null,
47
        ?DateTimeImmutable $availabilityEnds = null
48
    ) {
49
        // Workaround to maintain compatibility with older BookingInfo data.
50
        // Empty BookingInfo properties used to be stored as empty strings in the past.
51
        // Convert those to null in case they are injected via the constructor (via BookingInfo::deserialize()).
52
        // API clients are also allowed to send empty strings for BookingInfo properties via EntryAPI3, which should
53
        // also be treated as null.
54
        $url = $this->castEmptyStringToNull($url);
55
        $phone = $this->castEmptyStringToNull($phone);
56
        $email = $this->castEmptyStringToNull($email);
57
58
        $this->url = $url;
59
        $this->urlLabel = $urlLabel;
60
        $this->phone = $phone;
61
        $this->email = $email;
62
        $this->availabilityStarts = $availabilityStarts;
63
        $this->availabilityEnds = $availabilityEnds;
64
    }
65
66
    public function getPhone(): ?string
67
    {
68
        return $this->phone;
69
    }
70
71
    public function getEmail(): ?string
72
    {
73
        return $this->email;
74
    }
75
76
    public function getUrl(): ?string
77
    {
78
        return $this->url;
79
    }
80
81
    public function getUrlLabel(): ?MultilingualString
82
    {
83
        return $this->urlLabel;
84
    }
85
86
    public function getAvailabilityStarts(): ?DateTimeImmutable
87
    {
88
        return $this->availabilityStarts;
89
    }
90
91
    public function getAvailabilityEnds(): ?DateTimeImmutable
92
    {
93
        return $this->availabilityEnds;
94
    }
95
96
    public function serialize(): array
97
    {
98
        $serialized = array_filter(
99
            [
100
              'phone' => $this->phone,
101
              'email' => $this->email,
102
              'url' => $this->url,
103
            ]
104
        );
105
106
        if ($this->availabilityStarts) {
107
            $serialized['availabilityStarts'] = $this->availabilityStarts->format(\DATE_ATOM);
108
        }
109
110
        if ($this->availabilityEnds) {
111
            $serialized['availabilityEnds'] = $this->availabilityEnds->format(\DATE_ATOM);
112
        }
113
114
        if ($this->urlLabel) {
115
            $serialized['urlLabel'] = $this->urlLabel->serialize();
116
        }
117
118
        return $serialized;
119
    }
120
121
    public static function deserialize(array $data): BookingInfo
122
    {
123
        $defaults = [
124
            'url' => null,
125
            'urlLabel' => null,
126
            'phone' => null,
127
            'email' => null,
128
            'availabilityStarts' => null,
129
            'availabilityEnds' => null,
130
        ];
131
132
        $data = array_merge($defaults, $data);
133
134
        $availabilityStarts = null;
135
        if ($data['availabilityStarts']) {
136
            $availabilityStarts = DateTimeImmutable::createFromFormat(\DATE_ATOM, $data['availabilityStarts']);
137
        }
138
139
        $availabilityEnds = null;
140
        if ($data['availabilityEnds']) {
141
            $availabilityEnds = DateTimeImmutable::createFromFormat(\DATE_ATOM, $data['availabilityEnds']);
142
        }
143
144
        $urlLabel = null;
145
        if ($data['urlLabel']) {
146
            $urlLabel = MultilingualString::deserialize($data['urlLabel']);
0 ignored issues
show
Documentation introduced by
$data['urlLabel'] is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
147
        }
148
149
        return new self(
150
            $data['url'],
151
            $urlLabel,
152
            $data['phone'],
153
            $data['email'],
154
            $availabilityStarts,
155
            $availabilityEnds
156
        );
157
    }
158
159
    public function toJsonLd(): array
160
    {
161
        return $this->serialize();
162
    }
163
164
    public function sameAs(BookingInfo $otherBookingInfo): bool
165
    {
166
        return $this->toJsonLd() === $otherBookingInfo->toJsonLd();
167
    }
168
169
    public static function fromUdb3ModelBookingInfo(Udb3ModelBookingInfo $udb3ModelBookingInfo): BookingInfo
170
    {
171
        $url = null;
172
        $urlLabel = null;
173
        $phone = null;
174
        $email = null;
175
        $availabilityStarts = null;
176
        $availabilityEnds = null;
177
178
        if ($udb3ModelWebsite = $udb3ModelBookingInfo->getWebsite()) {
179
            $url = $udb3ModelWebsite->getUrl()->toString();
180
            $urlLabel = MultilingualString::fromUdb3ModelTranslatedValueObject($udb3ModelWebsite->getLabel());
181
        }
182
183
        if ($udb3ModelPhone = $udb3ModelBookingInfo->getTelephoneNumber()) {
184
            $phone = $udb3ModelPhone->toString();
185
        }
186
187
        if ($udb3ModelEmail = $udb3ModelBookingInfo->getEmailAddress()) {
188
            $email = $udb3ModelEmail->toString();
189
        }
190
191
        if ($udb3ModelAvailability = $udb3ModelBookingInfo->getAvailability()) {
192
            $availabilityStarts = $udb3ModelAvailability->getFrom();
193
            $availabilityEnds = $udb3ModelAvailability->getTo();
194
        }
195
196
        return new self(
197
            $url,
198
            $urlLabel,
199
            $phone,
200
            $email,
201
            $availabilityStarts,
202
            $availabilityEnds
203
        );
204
    }
205
206
    private function castEmptyStringToNull(?string $string = null): ?string
207
    {
208
        return is_string($string) && $string === '' ? null : $string;
209
    }
210
}
211