Completed
Pull Request — master (#358)
by Luc
05:26
created

BookingInfo::deserialize()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 32
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 22
nc 4
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3;
4
5
use CultuurNet\UDB3\Model\ValueObject\Contact\BookingInfo as Udb3ModelBookingInfo;
6
7
/**
8
 * BookingInfo info.
9
 */
10
class BookingInfo implements JsonLdSerializableInterface
11
{
12
    /**
13
     * @var string|null
14
     */
15
    protected $phone;
16
17
    /**
18
     * @var string|null
19
     */
20
    protected $email;
21
22
    /**
23
     * @var string|null
24
     */
25
    protected $url;
26
27
    /**
28
     * @var string|null
29
     */
30
    protected $urlLabel;
31
32
    /**
33
     * @var \DateTimeImmutable|null
34
     */
35
    protected $availabilityStarts;
36
37
    /**
38
     * @var \DateTimeImmutable|null
39
     */
40
    protected $availabilityEnds;
41
42
    /**
43
     * @param string|null $url
44
     * @param string|null $urlLabel
45
     * @param string|null $phone
46
     * @param string|null $email
47
     * @param \DateTimeImmutable|null $availabilityStarts
48
     * @param \DateTimeImmutable|null $availabilityEnds
49
     */
50
    public function __construct(
51
        $url = null,
52
        $urlLabel = null,
53
        $phone = null,
54
        $email = null,
55
        \DateTimeImmutable $availabilityStarts = null,
56
        \DateTimeImmutable $availabilityEnds = null
57
    ) {
58
        // Workaround to maintain compatibility with older BookingInfo data.
59
        // Empty BookingInfo properties used to be stored as empty strings in the past.
60
        // Convert those to null in case they are injected via the constructor (via BookingInfo::deserialize()).
61
        // API clients are also allowed to send empty strings for BookingInfo properties via EntryAPI3, which should
62
        // also be treated as null.
63
        $url = $this->castEmptyStringToNull($url);
64
        $urlLabel = $this->castEmptyStringToNull($urlLabel);
65
        $phone = $this->castEmptyStringToNull($phone);
66
        $email = $this->castEmptyStringToNull($email);
67
68
        $this->url = $url;
69
        $this->urlLabel = $urlLabel;
70
        $this->phone = $phone;
71
        $this->email = $email;
72
        $this->availabilityStarts = $availabilityStarts;
73
        $this->availabilityEnds = $availabilityEnds;
74
    }
75
76
    public function getPhone()
77
    {
78
        return $this->phone;
79
    }
80
81
    public function getEmail()
82
    {
83
        return $this->email;
84
    }
85
86
    public function getUrl()
87
    {
88
        return $this->url;
89
    }
90
91
    public function getUrlLabel()
92
    {
93
        return $this->urlLabel;
94
    }
95
96
    /**
97
     * @return \DateTimeImmutable|null
98
     */
99
    public function getAvailabilityStarts()
100
    {
101
        return $this->availabilityStarts;
102
    }
103
104
    /**
105
     * @return \DateTimeImmutable|null
106
     */
107
    public function getAvailabilityEnds()
108
    {
109
        return $this->availabilityEnds;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function serialize()
116
    {
117
        $serialized = array_filter(
118
            [
119
              'phone' => $this->phone,
120
              'email' => $this->email,
121
              'url' => $this->url,
122
              'urlLabel' => $this->urlLabel,
123
            ]
124
        );
125
126
        if ($this->availabilityStarts) {
127
            $serialized['availabilityStarts'] = $this->availabilityStarts->format(\DATE_ATOM);
128
        }
129
130
        if ($this->availabilityEnds) {
131
            $serialized['availabilityEnds'] = $this->availabilityEnds->format(\DATE_ATOM);
132
        }
133
134
        return $serialized;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public static function deserialize(array $data)
141
    {
142
        $defaults = [
143
            'url' => null,
144
            'urlLabel' => null,
145
            'phone' => null,
146
            'email' => null,
147
            'availabilityStarts' => null,
148
            'availabilityEnds' => null,
149
        ];
150
151
        $data = array_merge($defaults, $data);
152
153
        $availabilityStarts = null;
154
        if ($data['availabilityStarts']) {
155
            $availabilityStarts = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $data['availabilityStarts']);
156
        }
157
158
        $availabilityEnds = null;
159
        if ($data['availabilityEnds']) {
160
            $availabilityEnds = \DateTimeImmutable::createFromFormat(\DATE_ATOM, $data['availabilityEnds']);
161
        }
162
163
        return new static(
164
            $data['url'],
165
            $data['urlLabel'],
166
            $data['phone'],
167
            $data['email'],
168
            $availabilityStarts,
169
            $availabilityEnds
170
        );
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function toJsonLd()
177
    {
178
        return $this->serialize();
179
    }
180
181
    /**
182
     * @param BookingInfo $otherBookingInfo
183
     * @return bool
184
     */
185
    public function sameAs(BookingInfo $otherBookingInfo)
186
    {
187
        return $this->toJsonLd() === $otherBookingInfo->toJsonLd();
188
    }
189
190
    /**
191
     * @param Udb3ModelBookingInfo $udb3ModelBookingInfo
192
     * @return BookingInfo
193
     */
194
    public static function fromUdb3ModelBookingInfo(Udb3ModelBookingInfo $udb3ModelBookingInfo)
195
    {
196
        $url = null;
197
        $urlLabel = null;
198
        $phone = null;
199
        $email = null;
200
        $availabilityStarts = null;
201
        $availabilityEnds = null;
202
203
        if ($udb3ModelWebsite = $udb3ModelBookingInfo->getWebsite()) {
204
            $url = $udb3ModelWebsite->getUrl()->toString();
205
            $urlLabel = $udb3ModelWebsite->getLabel()->getTranslation(
206
                $udb3ModelWebsite->getLabel()->getOriginalLanguage()
207
            )->toString();
208
        }
209
210
        if ($udb3ModelPhone = $udb3ModelBookingInfo->getTelephoneNumber()) {
211
            $phone = $udb3ModelPhone->toString();
212
        }
213
214
        if ($udb3ModelEmail = $udb3ModelBookingInfo->getEmailAddress()) {
215
            $email = $udb3ModelEmail->toString();
216
        }
217
218
        if ($udb3ModelAvailability = $udb3ModelBookingInfo->getAvailability()) {
219
            $availabilityStarts = $udb3ModelAvailability->getFrom();
220
            $availabilityEnds = $udb3ModelAvailability->getTo();
221
        }
222
223
        return new BookingInfo(
224
            $url,
225
            $urlLabel,
226
            $phone,
227
            $email,
228
            $availabilityStarts,
229
            $availabilityEnds
230
        );
231
    }
232
233
    /**
234
     * @param $string
235
     * @return null|string
236
     */
237
    private function castEmptyStringToNull($string)
238
    {
239
        return is_string($string) && strlen($string) === 0 ? null : $string;
240
    }
241
}
242