Completed
Pull Request — master (#239)
by Luc
04:52
created

CdbXMLImporter::importLabels()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 2
1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use CultureFeed_Cdb_Data_File;
6
use CultuurNet\UDB3\Cdb\CdbId\EventCdbIdExtractorInterface;
7
use CultuurNet\UDB3\Cdb\DateTimeFactory;
8
use CultuurNet\UDB3\Cdb\PriceDescriptionParser;
9
use CultuurNet\UDB3\LabelImporter;
10
use CultuurNet\UDB3\Offer\ReadModel\JSONLD\CdbXMLItemBaseImporter;
11
use CultuurNet\UDB3\SluggerInterface;
12
use CultuurNet\UDB3\StringFilter\StringFilterInterface;
13
14
/**
15
 * Takes care of importing cultural events in the CdbXML format (UDB2)
16
 * into a UDB3 JSON-LD document.
17
 */
18
class CdbXMLImporter
19
{
20
    /**
21
     * @var CdbXMLItemBaseImporter
22
     */
23
    private $cdbXMLItemBaseImporter;
24
25
    /**
26
     * @var EventCdbIdExtractorInterface
27
     */
28
    private $cdbIdExtractor;
29
30
    /**
31
     * @var PriceDescriptionParser
32
     */
33
    private $priceDescriptionParser;
34
35
    /**
36
     * @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter
37
     * @param EventCdbIdExtractorInterface $cdbIdExtractor
38
     * @param PriceDescriptionParser $priceDescriptionParser
39
     */
40
    public function __construct(
41
        CdbXMLItemBaseImporter $dbXMLItemBaseImporter,
42
        EventCdbIdExtractorInterface $cdbIdExtractor,
43
        PriceDescriptionParser $priceDescriptionParser
44
    ) {
45
        $this->cdbXMLItemBaseImporter = $dbXMLItemBaseImporter;
46
        $this->cdbIdExtractor = $cdbIdExtractor;
47
        $this->priceDescriptionParser = $priceDescriptionParser;
48
    }
49
50
    /**
51
     * @var StringFilterInterface[]
52
     */
53
    private $descriptionFilters = [];
54
55
    /**
56
     * Imports a UDB2 event into a UDB3 JSON-LD document.
57
     *
58
     * @param \stdClass $base
59
     *   The JSON-LD document to start from.
60
     * @param \CultureFeed_Cdb_Item_Event $event
61
     *   The cultural event data from UDB2 to import.
62
     * @param PlaceServiceInterface $placeManager
63
     *   The manager from which to retrieve the JSON-LD of a place.
64
     * @param OrganizerServiceInterface $organizerManager
65
     *   The manager from which to retrieve the JSON-LD of an organizer.
66
     * @param SluggerInterface $slugger
67
     *   The slugger that's used to generate a sameAs reference.
68
     *
69
     * @return \stdClass
70
     *   The document with the UDB2 event data merged in.
71
     */
72
    public function documentWithCdbXML(
73
        $base,
74
        \CultureFeed_Cdb_Item_Event $event,
75
        PlaceServiceInterface $placeManager,
76
        OrganizerServiceInterface $organizerManager,
77
        SluggerInterface $slugger
78
    ) {
79
        $jsonLD = clone $base;
80
81
        /** @var \CultureFeed_Cdb_Data_EventDetail $detail */
82
        $detail = null;
83
84
        /** @var \CultureFeed_Cdb_Data_EventDetail[] $details */
85
        $details = $event->getDetails();
86
87
        foreach ($details as $languageDetail) {
88
            $language = $languageDetail->getLanguage();
89
90
            // The first language detail found will be used to retrieve
91
            // properties from which in UDB3 are not any longer considered
92
            // to be language specific.
93
            if (!$detail) {
94
                $detail = $languageDetail;
95
            }
96
97
            $jsonLD->name[$language] = $languageDetail->getTitle();
98
99
            $this->importDescription($languageDetail, $jsonLD, $language);
100
        }
101
102
        $this->cdbXMLItemBaseImporter->importAvailable($event, $jsonLD);
103
104
        $this->importPicture($detail, $jsonLD);
105
106
        $labelImporter = new LabelImporter();
107
        $labelImporter->importLabels($event, $jsonLD);
108
109
        $jsonLD->calendarSummary = $detail->getCalendarSummary();
110
111
        $this->importLocation($event, $placeManager, $jsonLD);
112
113
        $this->importOrganizer($event, $organizerManager, $jsonLD);
114
115
        $this->importBookingInfo($event, $detail, $jsonLD);
116
117
        $this->importPriceInfo($detail, $jsonLD);
118
119
        $this->importTerms($event, $jsonLD);
120
121
        $this->cdbXMLItemBaseImporter->importPublicationInfo($event, $jsonLD);
122
123
        $this->importCalendar($event, $jsonLD);
124
125
        $this->importTypicalAgeRange($event, $jsonLD);
126
127
        $this->importPerformers($detail, $jsonLD);
128
129
        $this->importLanguages($event, $jsonLD);
130
131
        $this->importUitInVlaanderenReference($event, $slugger, $jsonLD);
132
133
        $this->cdbXMLItemBaseImporter->importExternalId($event, $jsonLD);
134
135
        $this->importSeeAlso($event, $jsonLD);
136
137
        $this->importContactPoint($event, $jsonLD);
138
139
        $this->cdbXMLItemBaseImporter->importWorkflowStatus($event, $jsonLD);
140
141
        return $jsonLD;
142
    }
143
144
    /**
145
     * @param StringFilterInterface $filter
146
     */
147
    public function addDescriptionFilter(StringFilterInterface $filter)
148
    {
149
        $this->descriptionFilters[] = $filter;
150
    }
151
152
    /**
153
     * @param int $unixTime
154
     * @return \DateTime
155
     */
156
    private function dateFromUdb2UnixTime($unixTime)
157
    {
158
        $dateTime = new \DateTime(
159
            '@' . $unixTime,
160
            new \DateTimeZone('Europe/Brussels')
161
        );
162
163
        return $dateTime;
164
    }
165
166
    /**
167
     * @param \CultureFeed_Cdb_Data_EventDetail $languageDetail
168
     * @param \stdClass $jsonLD
169
     * @param string $language
170
     */
171
    private function importDescription($languageDetail, $jsonLD, $language)
172
    {
173
        $descriptions = [
174
            $languageDetail->getShortDescription(),
175
            $languageDetail->getLongDescription()
176
        ];
177
        $descriptions = array_filter($descriptions);
178
        $description = implode('<br/>', $descriptions);
179
180
        foreach ($this->descriptionFilters as $descriptionFilter) {
181
            $description = $descriptionFilter->filter($description);
182
        };
183
184
        $jsonLD->description[$language] = $description;
185
    }
186
187
    /**
188
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
189
     * @param \stdClass $jsonLD
190
     *
191
     * This is based on code found in the culturefeed theme.
192
     * @see https://github.com/cultuurnet/culturefeed/blob/master/culturefeed_agenda/theme/theme.inc#L266-L284
193
     */
194 View Code Duplication
    private function importPicture($detail, $jsonLD)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
195
    {
196
        $mainPicture = null;
197
198
        // first check if there is a media file that is main and has the PHOTO media type
199
        $photos = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO);
200
        foreach ($photos as $photo) {
201
            if ($photo->isMain()) {
202
                $mainPicture = $photo;
203
            }
204
        }
205
206
        // the IMAGEWEB media type is deprecated but can still be used as a main image if there is no PHOTO
207
        if (empty($mainPicture)) {
208
            $images = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB);
209
            foreach ($images as $image) {
210
                if ($image->isMain()) {
211
                    $mainPicture = $image;
212
                }
213
            }
214
        }
215
216
        // if there is no explicit main image we just use the oldest picture of any type
217
        if (empty($mainPicture)) {
218
            $pictures = $detail->getMedia()->byMediaTypes(
219
                [
220
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO,
221
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB
222
                ]
223
            );
224
225
            $pictures->rewind();
226
            $mainPicture = count($pictures) > 0 ? $pictures->current() : null;
227
        }
228
229
        if ($mainPicture) {
230
            $jsonLD->image = $mainPicture->getHLink();
231
        }
232
    }
233
234
    /**
235
     * @param \CultureFeed_Cdb_Item_Event $event
236
     * @param PlaceServiceInterface $placeManager
237
     * @param \stdClass $jsonLD
238
     */
239
    private function importLocation(\CultureFeed_Cdb_Item_Event $event, PlaceServiceInterface $placeManager, $jsonLD)
240
    {
241
        $location = array();
242
        $location['@type'] = 'Place';
243
244
        $location_id = $this->cdbIdExtractor->getRelatedPlaceCdbId($event);
245
246
        if ($location_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $location_id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
247
            $location += (array)$placeManager->placeJSONLD($location_id);
248
        } else {
249
            $location_cdb = $event->getLocation();
250
            $location['name']['nl'] = $location_cdb->getLabel();
251
            $address = $location_cdb->getAddress()->getPhysicalAddress();
252
            if ($address) {
253
                $location['address'] = array(
254
                    'addressCountry' => $address->getCountry(),
255
                    'addressLocality' => $address->getCity(),
256
                    'postalCode' => $address->getZip(),
257
                    'streetAddress' =>
258
                        $address->getStreet() . ' ' . $address->getHouseNumber(
259
                        ),
260
                );
261
            }
262
        }
263
        $jsonLD->location = $location;
264
    }
265
266
    /**
267
     * @param \CultureFeed_Cdb_Item_Event $event
268
     * @param OrganizerServiceInterface $organizerManager
269
     * @param \stdClass $jsonLD
270
     */
271
    private function importOrganizer(
272
        \CultureFeed_Cdb_Item_Event $event,
273
        OrganizerServiceInterface $organizerManager,
274
        $jsonLD
275
    ) {
276
        $organizer = null;
277
        $organizer_id = $this->cdbIdExtractor->getRelatedOrganizerCdbId($event);
278
        $organizer_cdb = $event->getOrganiser();
279
        $contact_info_cdb = $event->getContactInfo();
280
281
        if ($organizer_id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $organizer_id of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
282
            $organizer = (array)$organizerManager->organizerJSONLD($organizer_id);
283
        } elseif ($organizer_cdb && $contact_info_cdb) {
284
            $organizer = array();
285
            $organizer['name'] = $organizer_cdb->getLabel();
286
287
            $emails_cdb = $contact_info_cdb->getMails();
288
            if (count($emails_cdb) > 0) {
289
                $organizer['email'] = array();
290
                foreach ($emails_cdb as $email) {
291
                    $organizer['email'][] = $email->getMailAddress();
292
                }
293
            }
294
295
            $phones_cdb = $contact_info_cdb->getPhones();
296
            if (count($phones_cdb) > 0) {
297
                $organizer['phone'] = array();
298
                foreach ($phones_cdb as $phone) {
299
                    $organizer['phone'][] = $phone->getNumber();
300
                }
301
            }
302
        }
303
304
        if (!is_null($organizer)) {
305
            $organizer['@type'] = 'Organizer';
306
            $jsonLD->organizer = $organizer;
307
        }
308
    }
309
310
    /**
311
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
312
     * @param \stdClass $jsonLD
313
     */
314
    private function importPriceInfo(
315
        \CultureFeed_Cdb_Data_EventDetail $detail,
316
        $jsonLD
317
    ) {
318
        $prices = array();
319
320
        $price = $detail->getPrice();
321
322
        if ($price) {
323
            $description = $price->getDescription();
324
325
            if ($description) {
326
                $prices = $this->priceDescriptionParser->parse($description);
327
            }
328
329
            // If price description was not interpretable, fall back to
330
            // price title and value.
331
            if (empty($prices) && $price->getValue() !== null) {
332
                $prices['Basistarief'] = floatval($price->getValue());
333
            }
334
        }
335
336
        if (!empty($prices)) {
337
            $priceInfo = array();
338
339
            /** @var \CultureFeed_Cdb_Data_Price $price */
340
            foreach ($prices as $title => $value) {
341
                $priceInfoItem = array(
342
                    'name' => $title,
343
                    'priceCurrency' => 'EUR',
344
                    'price' => $value,
345
                );
346
347
                $priceInfoItem['category'] = 'tariff';
348
349
                if ($priceInfoItem['name'] === 'Basistarief') {
350
                    $priceInfoItem['category'] = 'base';
351
                }
352
353
                $priceInfo[] = $priceInfoItem;
354
            }
355
356
            if (!empty($priceInfo)) {
357
                $jsonLD->priceInfo = $priceInfo;
358
            }
359
        }
360
    }
361
362
    /**
363
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
364
     * @param \stdClass $jsonLD
365
     */
366
    private function importBookingInfo(
367
        \CultureFeed_Cdb_Item_Event $event,
368
        \CultureFeed_Cdb_Data_EventDetail $detail,
369
        $jsonLD
370
    ) {
371
        $price = $detail->getPrice();
372
373
        if ($price) {
374
            $jsonLD->bookingInfo = array();
375
            // Booking info.
376
            $bookingInfo = array();
377
            if ($price->getDescription()) {
378
                $bookingInfo['description'] = $price->getDescription();
379
            }
380
            if ($price->getTitle()) {
381
                $bookingInfo['name'] = $price->getTitle();
382
            }
383
            if ($price->getValue() !== null) {
384
                $bookingInfo['priceCurrency'] = 'EUR';
385
                $bookingInfo['price'] = floatval($price->getValue());
386
            }
387
            if ($bookingPeriod = $event->getBookingPeriod()) {
388
                $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom());
389
                $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill());
390
391
                $bookingInfo['availabilityStarts'] = $startDate->format('c');
392
                $bookingInfo['availabilityEnds'] = $endDate->format('c');
393
            }
394
395
            // Add reservation URL
396
            if ($contactInfo = $event->getContactInfo()) {
397
                if ($bookingUrl = $contactInfo->getReservationUrl()) {
398
                    $bookingInfo['url'] = $bookingUrl;
399
                }
400
            }
401
402
            $jsonLD->bookingInfo[] = $bookingInfo;
403
        }
404
    }
405
406
    /**
407
     * @param \CultureFeed_Cdb_Item_Event $event
408
     * @param \stdClass $jsonLD
409
     */
410
    private function importContactPoint(
411
        \CultureFeed_Cdb_Item_Event $event,
412
        \stdClass $jsonLD
413
    ) {
414
        $contactInfo = $event->getContactInfo();
415
416
        if ($contactInfo) {
417
            $reservationContactPoint = array();
418
            $leftoverContactPoint = array();
419
420
            foreach ($contactInfo->getMails() as $email) {
421
                /** @var \CultureFeed_Cdb_Data_Mail $email */
422
                $emailAddress = $email->getMailAddress();
423
424
                if ($email->isForReservations()) {
425
                    $reservationContactPoint['email'][] = $emailAddress;
426
                } else {
427
                    $leftoverContactPoint['email'][] = $emailAddress;
428
                }
429
            }
430
431
            foreach ($contactInfo->getPhones() as $phone) {
432
                /** @var \CultureFeed_Cdb_Data_Phone $phone */
433
                $phoneNumber = $phone->getNumber();
434
435
                if ($phone->isForReservations()) {
436
                    $reservationContactPoint['telephone'][] = $phoneNumber;
437
                } else {
438
                    $leftoverContactPoint['telephone'][] = $phoneNumber;
439
                }
440
            }
441
442
            array_filter($reservationContactPoint);
443
            if (count($reservationContactPoint) > 0) {
444
                $reservationContactPoint['contactType'] = "Reservations";
445
                $jsonLD->contactPoint[] = $reservationContactPoint;
446
            }
447
448
            array_filter($leftoverContactPoint);
449
            if (count($leftoverContactPoint) > 0) {
450
                $jsonLD->contactPoint[] = $leftoverContactPoint;
451
            }
452
        }
453
    }
454
455
    /**
456
     * @param \CultureFeed_Cdb_Item_Event $event
457
     * @param \stdClass $jsonLD
458
     */
459
    private function importTerms(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
460
    {
461
        $themeBlacklist = [
462
            'Thema onbepaald',
463
            'Meerder kunstvormen',
464
            'Meerdere filmgenres'
465
        ];
466
        $categories = array();
467 View Code Duplication
        foreach ($event->getCategories() as $category) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
468
            /* @var \Culturefeed_Cdb_Data_Category $category */
469
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
470
                $categories[] = array(
471
                    'label' => $category->getName(),
472
                    'domain' => $category->getType(),
473
                    'id' => $category->getId(),
474
                );
475
            }
476
        }
477
        $jsonLD->terms = $categories;
478
    }
479
480
    /**
481
     * @param \CultureFeed_Cdb_Item_Event $event
482
     * @param \stdClass $jsonLD
483
     */
484
    private function importCalendar(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
485
    {
486
        // To render the front-end we make a distinction between 4 calendar types
487
        // Permanent and Periodic map directly to the Cdb calendar classes
488
        // Simple timestamps are divided into single and multiple
489
        $calendarType = 'unknown';
490
        $calendar = $event->getCalendar();
491
492
        if ($calendar instanceof \CultureFeed_Cdb_Data_Calendar_Permanent) {
493
            $calendarType = 'permanent';
494
        } elseif ($calendar instanceof \CultureFeed_Cdb_Data_Calendar_PeriodList) {
495
            $calendarType = 'periodic';
496
            $calendar->rewind();
497
            $firstCalendarItem = $calendar->current();
498
            $startDateString = $firstCalendarItem->getDateFrom() . 'T00:00:00';
499
            $startDate = DateTimeFactory::dateTimeFromDateString($startDateString);
500
501 View Code Duplication
            if (iterator_count($calendar) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
502
                $periodArray = iterator_to_array($calendar);
503
                $lastCalendarItem = end($periodArray);
504
            } else {
505
                $lastCalendarItem = $firstCalendarItem;
506
            }
507
508
            $endDateString = $lastCalendarItem->getDateTo() . 'T00:00:00';
509
            $endDate = DateTimeFactory::dateTimeFromDateString($endDateString);
510
511
            $jsonLD->startDate = $startDate->format('c');
512
            $jsonLD->endDate = $endDate->format('c');
513
        } elseif ($calendar instanceof \CultureFeed_Cdb_Data_Calendar_TimestampList) {
514
            $calendarType = 'single';
515
            $calendar->rewind();
516
            /** @var \CultureFeed_Cdb_Data_Calendar_Timestamp $firstCalendarItem */
517
            $firstCalendarItem = $calendar->current();
518
            if ($firstCalendarItem->getStartTime()) {
519
                $dateString =
520
                    $firstCalendarItem->getDate() . 'T' . $firstCalendarItem->getStartTime();
521
            } else {
522
                $dateString = $firstCalendarItem->getDate() . 'T00:00:00';
523
            }
524
525
            $startDate = DateTimeFactory::dateTimeFromDateString($dateString);
526
527 View Code Duplication
            if (iterator_count($calendar) > 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
528
                $periodArray = iterator_to_array($calendar);
529
                $lastCalendarItem = end($periodArray);
530
            } else {
531
                $lastCalendarItem = $firstCalendarItem;
532
            }
533
534
            $endDateString = null;
535
            if ($lastCalendarItem->getEndTime()) {
536
                $endDateString =
537
                    $lastCalendarItem->getDate() . 'T' . $lastCalendarItem->getEndTime();
538
            } else {
539
                if (iterator_count($calendar) > 1) {
540
                    $endDateString = $lastCalendarItem->getDate() . 'T00:00:00';
541
                }
542
            }
543
544
            if ($endDateString) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $endDateString of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
545
                $endDate = DateTimeFactory::dateTimeFromDateString($endDateString);
546
                $jsonLD->endDate = $endDate->format('c');
547
548
                if ($startDate->format('Ymd') != $endDate->format('Ymd')) {
549
                    $calendarType = 'multiple';
550
                }
551
            }
552
553
            $jsonLD->startDate = $startDate->format('c');
554
        }
555
556
        $jsonLD->calendarType = $calendarType;
557
    }
558
559
    /**
560
     * @param \CultureFeed_Cdb_Item_Event $event
561
     * @param \stdClass $jsonLD
562
     */
563
    private function importTypicalAgeRange(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
564
    {
565
        $ageFrom = $event->getAgeFrom();
566
        if ($ageFrom) {
567
            $jsonLD->typicalAgeRange = "{$ageFrom}-";
568
        }
569
    }
570
571
    /**
572
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
573
     * @param \stdClass $jsonLD
574
     */
575
    private function importPerformers(\CultureFeed_Cdb_Data_EventDetail $detail, $jsonLD)
576
    {
577
        /** @var \CultureFeed_Cdb_Data_Performer $performer */
578
        $performers = $detail->getPerformers();
579
        if ($performers) {
580
            foreach ($performers as $performer) {
581
                if ($performer->getLabel()) {
582
                    $performerData = new \stdClass();
583
                    $performerData->name = $performer->getLabel();
584
                    $jsonLD->performer[] = $performerData;
585
                }
586
            }
587
        }
588
    }
589
590
    /**
591
     * @param \CultureFeed_Cdb_Item_Event $event
592
     * @param \stdClass $jsonLD
593
     */
594
    private function importLanguages(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
595
    {
596
        /** @var \CultureFeed_Cdb_Data_Language $udb2Language */
597
        $languages = $event->getLanguages();
598
        if ($languages) {
599
            $jsonLD->language = [];
600
            foreach ($languages as $udb2Language) {
601
                $jsonLD->language[] = $udb2Language->getLanguage();
602
            }
603
            $jsonLD->language = array_unique($jsonLD->language);
604
        }
605
    }
606
607
    /**
608
     * @param \CultureFeed_Cdb_Item_Event $event
609
     * @param \stdClass $jsonLD
610
     */
611
    private function importSeeAlso(
612
        \CultureFeed_Cdb_Item_Event $event,
613
        \stdClass $jsonLD
614
    ) {
615
        if (!property_exists($jsonLD, 'seeAlso')) {
616
            $jsonLD->seeAlso = [];
617
        }
618
619
        // Add contact info url, if it's not for reservations.
620
        if ($contactInfo = $event->getContactInfo()) {
621
            /** @var \CultureFeed_Cdb_Data_Url[] $contactUrls */
622
            $contactUrls = $contactInfo->getUrls();
623
            if (is_array($contactUrls) && count($contactUrls) > 0) {
624
                foreach ($contactUrls as $contactUrl) {
625
                    if (!$contactUrl->isForReservations()) {
626
                        $jsonLD->seeAlso[] = $contactUrl->getUrl();
627
                    }
628
                }
629
            }
630
        }
631
    }
632
633
    /**
634
     * @param \CultureFeed_Cdb_Item_Event $event
635
     * @param SluggerInterface $slugger
636
     * @param \stdClass $jsonLD
637
     */
638
    private function importUitInVlaanderenReference(
639
        \CultureFeed_Cdb_Item_Event $event,
640
        SluggerInterface $slugger,
641
        $jsonLD
642
    ) {
643
644
        // Some events seem to not have a Dutch name, even though this is
645
        // required. If there's no Dutch name, we just leave the slug empty as
646
        // that seems to be the behaviour on http://m.uitinvlaanderen.be
647
        if (isset($jsonLD->name['nl'])) {
648
            $name = $jsonLD->name['nl'];
649
            $slug = $slugger->slug($name);
650
        } else {
651
            $slug = '';
652
        }
653
654
        $reference = 'http://www.uitinvlaanderen.be/agenda/e/' . $slug . '/' . $event->getCdbId();
655
656
657
        if (!property_exists($jsonLD, 'sameAs')) {
658
            $jsonLD->sameAs = [];
659
        }
660
661
        if (!in_array($reference, $jsonLD->sameAs)) {
662
            array_push($jsonLD->sameAs, $reference);
663
        }
664
    }
665
}
666