Completed
Push — master ( 2816fc...3be0d8 )
by Luc
13s
created

BookingInfo::getUrlLabel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 string|null
34
     */
35
    protected $availabilityStarts;
36
37
    /**
38
     * @var string|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 string|null $availabilityStarts
48
     * @param string|null $availabilityEnds
49
     */
50
    public function __construct(
51
        $url = null,
52
        $urlLabel = null,
53
        $phone = null,
54
        $email = null,
55
        $availabilityStarts = null,
56
        $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
        $availabilityStarts = $this->castEmptyStringToNull($availabilityStarts);
68
        $availabilityEnds = $this->castEmptyStringToNull($availabilityEnds);
69
70
        $this->url = $url;
71
        $this->urlLabel = $urlLabel;
72
        $this->phone = $phone;
73
        $this->email = $email;
74
        $this->availabilityStarts = $availabilityStarts;
75
        $this->availabilityEnds = $availabilityEnds;
76
    }
77
78
    public function getPhone()
79
    {
80
        return $this->phone;
81
    }
82
83
    public function getEmail()
84
    {
85
        return $this->email;
86
    }
87
88
    public function getUrl()
89
    {
90
        return $this->url;
91
    }
92
93
    public function getUrlLabel()
94
    {
95
        return $this->urlLabel;
96
    }
97
98
    public function getAvailabilityStarts()
99
    {
100
        return $this->availabilityStarts;
101
    }
102
103
    public function getAvailabilityEnds()
104
    {
105
        return $this->availabilityEnds;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function serialize()
112
    {
113
        return array_filter(
114
            [
115
              'phone' => $this->phone,
116
              'email' => $this->email,
117
              'url' => $this->url,
118
              'urlLabel' => $this->urlLabel,
119
              'availabilityStarts' => $this->availabilityStarts,
120
              'availabilityEnds' => $this->availabilityEnds,
121
            ]
122
        );
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public static function deserialize(array $data)
129
    {
130
        $defaults = [
131
            'url' => null,
132
            'urlLabel' => null,
133
            'phone' => null,
134
            'email' => null,
135
            'availabilityStarts' => null,
136
            'availabilityEnds' => null,
137
        ];
138
139
        $data = array_merge($defaults, $data);
140
141
        return new static(
142
            $data['url'],
143
            $data['urlLabel'],
144
            $data['phone'],
145
            $data['email'],
146
            $data['availabilityStarts'],
147
            $data['availabilityEnds']
148
        );
149
    }
150
151
    /**
152
     * {@inheritdoc}
153
     */
154
    public function toJsonLd()
155
    {
156
        return $this->serialize();
157
    }
158
159
    /**
160
     * @param BookingInfo $otherBookingInfo
161
     * @return bool
162
     */
163
    public function sameAs(BookingInfo $otherBookingInfo)
164
    {
165
        return $this->toJsonLd() === $otherBookingInfo->toJsonLd();
166
    }
167
168
    /**
169
     * @param Udb3ModelBookingInfo $udb3ModelBookingInfo
170
     * @return BookingInfo
171
     */
172
    public static function fromUdb3ModelBookingInfo(Udb3ModelBookingInfo $udb3ModelBookingInfo)
173
    {
174
        $url = null;
175
        $urlLabel = null;
176
        $phone = null;
177
        $email = null;
178
        $availabilityStarts = null;
179
        $availabilityEnds = null;
180
181
        if ($udb3ModelWebsite = $udb3ModelBookingInfo->getWebsite()) {
182
            $url = $udb3ModelWebsite->getUrl()->toString();
183
            $urlLabel = $udb3ModelWebsite->getLabel()->getTranslation(
184
                $udb3ModelWebsite->getLabel()->getOriginalLanguage()
185
            )->toString();
186
        }
187
188
        if ($udb3ModelPhone = $udb3ModelBookingInfo->getTelephoneNumber()) {
189
            $phone = $udb3ModelPhone->toString();
190
        }
191
192
        if ($udb3ModelEmail = $udb3ModelBookingInfo->getEmailAddress()) {
193
            $email = $udb3ModelEmail->toString();
194
        }
195
196
        if ($udb3ModelAvailability = $udb3ModelBookingInfo->getAvailability()) {
197
            $availabilityStarts = $udb3ModelAvailability->getFrom()->format(\DATE_ATOM);
198
            $availabilityEnds = $udb3ModelAvailability->getTo()->format(\DATE_ATOM);
199
        }
200
201
        return new BookingInfo(
202
            $url,
203
            $urlLabel,
204
            $phone,
205
            $email,
206
            $availabilityStarts,
207
            $availabilityEnds
208
        );
209
    }
210
211
    /**
212
     * @param $string
213
     * @return null|string
214
     */
215
    private function castEmptyStringToNull($string)
216
    {
217
        return is_string($string) && strlen($string) === 0 ? null : $string;
218
    }
219
}
220