Completed
Pull Request — master (#253)
by Kristof
04:43
created

CdbXMLImporter::importDescription()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 16
nop 3
1
<?php
2
3
namespace CultuurNet\UDB3\Event\ReadModel\JSONLD;
4
5
use CultureFeed_Cdb_Data_File;
6
use CultuurNet\UDB3\CalendarFactoryInterface;
7
use CultuurNet\UDB3\Cdb\CdbId\EventCdbIdExtractorInterface;
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\BreakTagToNewlineStringFilter;
13
use CultuurNet\UDB3\StringFilter\CombinedStringFilter;
14
use CultuurNet\UDB3\StringFilter\ConsecutiveBlockOfTextStringFilter;
15
use CultuurNet\UDB3\StringFilter\StringFilterInterface;
16
use CultuurNet\UDB3\StringFilter\StripSourceStringFilter;
17
use CultuurNet\UDB3\StringFilter\StripSurroundingSpaceStringFilter;
18
19
/**
20
 * Takes care of importing cultural events in the CdbXML format (UDB2)
21
 * into a UDB3 JSON-LD document.
22
 */
23
class CdbXMLImporter
24
{
25
    /**
26
     * @var CdbXMLItemBaseImporter
27
     */
28
    private $cdbXMLItemBaseImporter;
29
30
    /**
31
     * @var EventCdbIdExtractorInterface
32
     */
33
    private $cdbIdExtractor;
34
35
    /**
36
     * @var PriceDescriptionParser
37
     */
38
    private $priceDescriptionParser;
39
40
    /**
41
     * @var StringFilterInterface
42
     */
43
    private $longDescriptionFilter;
44
45
    /**
46
     * @var StringFilterInterface
47
     */
48
    private $shortDescriptionFilter;
49
50
    /**
51
     * @var CalendarFactoryInterface
52
     */
53
    private $calendarFactory;
54
55
    /**
56
     * @param CdbXMLItemBaseImporter $dbXMLItemBaseImporter
57
     * @param EventCdbIdExtractorInterface $cdbIdExtractor
58
     * @param PriceDescriptionParser $priceDescriptionParser
59
     * @param CalendarFactoryInterface $calendarFactory
60
     */
61
    public function __construct(
62
        CdbXMLItemBaseImporter $dbXMLItemBaseImporter,
63
        EventCdbIdExtractorInterface $cdbIdExtractor,
64
        PriceDescriptionParser $priceDescriptionParser,
65
        CalendarFactoryInterface $calendarFactory
66
    ) {
67
        $this->cdbXMLItemBaseImporter = $dbXMLItemBaseImporter;
68
        $this->cdbIdExtractor = $cdbIdExtractor;
69
        $this->priceDescriptionParser = $priceDescriptionParser;
70
        $this->calendarFactory = $calendarFactory;
71
72
        $consecutiveBlockOfTextFilter = new ConsecutiveBlockOfTextStringFilter();
73
74
        $this->longDescriptionFilter = new CombinedStringFilter();
75
        $this->longDescriptionFilter->addFilter(
76
            new StripSourceStringFilter()
77
        );
78
        $this->longDescriptionFilter->addFilter(
79
            $consecutiveBlockOfTextFilter
80
        );
81
        $this->longDescriptionFilter->addFilter(
82
            new BreakTagToNewlineStringFilter()
83
        );
84
        $this->longDescriptionFilter->addFilter(
85
            new StripSurroundingSpaceStringFilter()
86
        );
87
88
        $this->shortDescriptionFilter = $consecutiveBlockOfTextFilter;
89
    }
90
91
    /**
92
     * Imports a UDB2 event into a UDB3 JSON-LD document.
93
     *
94
     * @param \stdClass $base
95
     *   The JSON-LD document to start from.
96
     * @param \CultureFeed_Cdb_Item_Event $event
97
     *   The cultural event data from UDB2 to import.
98
     * @param PlaceServiceInterface $placeManager
99
     *   The manager from which to retrieve the JSON-LD of a place.
100
     * @param OrganizerServiceInterface $organizerManager
101
     *   The manager from which to retrieve the JSON-LD of an organizer.
102
     * @param SluggerInterface $slugger
103
     *   The slugger that's used to generate a sameAs reference.
104
     *
105
     * @return \stdClass
106
     *   The document with the UDB2 event data merged in.
107
     */
108
    public function documentWithCdbXML(
109
        $base,
110
        \CultureFeed_Cdb_Item_Event $event,
111
        PlaceServiceInterface $placeManager,
112
        OrganizerServiceInterface $organizerManager,
113
        SluggerInterface $slugger
114
    ) {
115
        $jsonLD = clone $base;
116
117
        /** @var \CultureFeed_Cdb_Data_EventDetail $detail */
118
        $detail = null;
119
120
        /** @var \CultureFeed_Cdb_Data_EventDetail[] $details */
121
        $details = $event->getDetails();
122
123
        foreach ($details as $languageDetail) {
124
            $language = $languageDetail->getLanguage();
125
126
            // The first language detail found will be used to retrieve
127
            // properties from which in UDB3 are not any longer considered
128
            // to be language specific.
129
            if (!$detail) {
130
                $detail = $languageDetail;
131
            }
132
133
            $jsonLD->name[$language] = $languageDetail->getTitle();
134
135
            $this->importDescription($languageDetail, $jsonLD, $language);
136
        }
137
138
        $this->cdbXMLItemBaseImporter->importAvailable($event, $jsonLD);
139
140
        $this->importPicture($detail, $jsonLD);
141
142
        $labelImporter = new LabelImporter();
143
        $labelImporter->importLabels($event, $jsonLD);
144
145
        $jsonLD->calendarSummary = $detail->getCalendarSummary();
146
147
        $this->importLocation($event, $placeManager, $jsonLD);
148
149
        $this->importOrganizer($event, $organizerManager, $jsonLD);
150
151
        $this->importBookingInfo($event, $detail, $jsonLD);
152
153
        $this->importPriceInfo($detail, $jsonLD);
154
155
        $this->importTerms($event, $jsonLD);
156
157
        $this->cdbXMLItemBaseImporter->importPublicationInfo($event, $jsonLD);
158
159
        $calendar = $this->calendarFactory->createFromCdbCalendar($event->getCalendar());
160
        $jsonLD = (object)array_merge((array)$jsonLD, $calendar->toJsonLd());
161
162
        $this->importTypicalAgeRange($event, $jsonLD);
163
164
        $this->importPerformers($detail, $jsonLD);
165
166
        $this->importLanguages($event, $jsonLD);
167
168
        $this->importUitInVlaanderenReference($event, $slugger, $jsonLD);
169
170
        $this->cdbXMLItemBaseImporter->importExternalId($event, $jsonLD);
171
172
        $this->importSeeAlso($event, $jsonLD);
173
174
        $this->importContactPoint($event, $jsonLD);
175
176
        $this->cdbXMLItemBaseImporter->importWorkflowStatus($event, $jsonLD);
177
178
        return $jsonLD;
179
    }
180
181
    /**
182
     * @param int $unixTime
183
     * @return \DateTime
184
     */
185
    private function dateFromUdb2UnixTime($unixTime)
186
    {
187
        $dateTime = new \DateTime(
188
            '@' . $unixTime,
189
            new \DateTimeZone('Europe/Brussels')
190
        );
191
192
        return $dateTime;
193
    }
194
195
    /**
196
     * @param \CultureFeed_Cdb_Data_EventDetail $languageDetail
197
     * @param \stdClass $jsonLD
198
     * @param string $language
199
     */
200
    private function importDescription($languageDetail, $jsonLD, $language)
201
    {
202
        $longDescription = $languageDetail->getLongDescription();
203
204
        if ($longDescription) {
205
            $longDescription = $this->longDescriptionFilter->filter(
206
                $longDescription
207
            );
208
        }
209
210
        $descriptions = [];
211
212
        $shortDescription = $languageDetail->getShortDescription();
213
        if ($shortDescription) {
214
            $shortDescription = $this->shortDescriptionFilter->filter(
215
                $shortDescription
216
            );
217
218
            if ($longDescription) {
219
                if (!$this->longDescriptionStartsWithShortDescription($longDescription,
220
                    $shortDescription)
221
                ) {
222
                    $descriptions[] = $shortDescription;
223
                }
224
            }
225
        }
226
227
        if ($longDescription) {
228
            $descriptions[] = $longDescription;
229
        }
230
231
        $description = implode("\n\n", $descriptions);
232
233
        $jsonLD->description[$language] = $description;
234
    }
235
236
    /**
237
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
238
     * @param \stdClass $jsonLD
239
     *
240
     * This is based on code found in the culturefeed theme.
241
     * @see https://github.com/cultuurnet/culturefeed/blob/master/culturefeed_agenda/theme/theme.inc#L266-L284
242
     */
243 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...
244
    {
245
        $mainPicture = null;
246
247
        // first check if there is a media file that is main and has the PHOTO media type
248
        $photos = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO);
249
        foreach ($photos as $photo) {
250
            if ($photo->isMain()) {
251
                $mainPicture = $photo;
252
            }
253
        }
254
255
        // the IMAGEWEB media type is deprecated but can still be used as a main image if there is no PHOTO
256
        if (empty($mainPicture)) {
257
            $images = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB);
258
            foreach ($images as $image) {
259
                if ($image->isMain()) {
260
                    $mainPicture = $image;
261
                }
262
            }
263
        }
264
265
        // if there is no explicit main image we just use the oldest picture of any type
266
        if (empty($mainPicture)) {
267
            $pictures = $detail->getMedia()->byMediaTypes(
268
                [
269
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO,
270
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB
271
                ]
272
            );
273
274
            $pictures->rewind();
275
            $mainPicture = count($pictures) > 0 ? $pictures->current() : null;
276
        }
277
278
        if ($mainPicture) {
279
            $jsonLD->image = $mainPicture->getHLink();
280
        }
281
    }
282
283
    /**
284
     * @param \CultureFeed_Cdb_Item_Event $event
285
     * @param PlaceServiceInterface $placeManager
286
     * @param \stdClass $jsonLD
287
     */
288
    private function importLocation(\CultureFeed_Cdb_Item_Event $event, PlaceServiceInterface $placeManager, $jsonLD)
289
    {
290
        $location = array();
291
        $location['@type'] = 'Place';
292
293
        $location_id = $this->cdbIdExtractor->getRelatedPlaceCdbId($event);
294
295
        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...
296
            $location += (array)$placeManager->placeJSONLD($location_id);
297
        } else {
298
            $location_cdb = $event->getLocation();
299
            $location['name']['nl'] = $location_cdb->getLabel();
300
            $address = $location_cdb->getAddress()->getPhysicalAddress();
301
            if ($address) {
302
                $location['address'] = array(
303
                    'addressCountry' => $address->getCountry(),
304
                    'addressLocality' => $address->getCity(),
305
                    'postalCode' => $address->getZip(),
306
                    'streetAddress' =>
307
                        $address->getStreet() . ' ' . $address->getHouseNumber(
308
                        ),
309
                );
310
            }
311
        }
312
        $jsonLD->location = $location;
313
    }
314
315
    /**
316
     * @param \CultureFeed_Cdb_Item_Event $event
317
     * @param OrganizerServiceInterface $organizerManager
318
     * @param \stdClass $jsonLD
319
     */
320
    private function importOrganizer(
321
        \CultureFeed_Cdb_Item_Event $event,
322
        OrganizerServiceInterface $organizerManager,
323
        $jsonLD
324
    ) {
325
        $organizer = null;
326
        $organizer_id = $this->cdbIdExtractor->getRelatedOrganizerCdbId($event);
327
        $organizer_cdb = $event->getOrganiser();
328
        $contact_info_cdb = $event->getContactInfo();
329
330
        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...
331
            $organizer = (array)$organizerManager->organizerJSONLD($organizer_id);
332
        } elseif ($organizer_cdb && $contact_info_cdb) {
333
            $organizer = array();
334
            $organizer['name'] = $organizer_cdb->getLabel();
335
336
            $emails_cdb = $contact_info_cdb->getMails();
337
            if (count($emails_cdb) > 0) {
338
                $organizer['email'] = array();
339
                foreach ($emails_cdb as $email) {
340
                    $organizer['email'][] = $email->getMailAddress();
341
                }
342
            }
343
344
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones_cdb */
345
            $phones_cdb = $contact_info_cdb->getPhones();
346
            if (count($phones_cdb) > 0) {
347
                $organizer['phone'] = array();
348
                foreach ($phones_cdb as $phone) {
349
                    $organizer['phone'][] = $phone->getNumber();
350
                }
351
            }
352
        }
353
354
        if (!is_null($organizer)) {
355
            $organizer['@type'] = 'Organizer';
356
            $jsonLD->organizer = $organizer;
357
        }
358
    }
359
360
    /**
361
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
362
     * @param \stdClass $jsonLD
363
     */
364
    private function importPriceInfo(
365
        \CultureFeed_Cdb_Data_EventDetail $detail,
366
        $jsonLD
367
    ) {
368
        $prices = array();
369
370
        $price = $detail->getPrice();
371
372
        if ($price) {
373
            $description = $price->getDescription();
374
375
            if ($description) {
376
                $prices = $this->priceDescriptionParser->parse($description);
377
            }
378
379
            // If price description was not interpretable, fall back to
380
            // price title and value.
381
            if (empty($prices) && $price->getValue() !== null) {
382
                $prices['Basistarief'] = floatval($price->getValue());
383
            }
384
        }
385
386
        if (!empty($prices)) {
387
            $priceInfo = array();
388
389
            /** @var \CultureFeed_Cdb_Data_Price $price */
390
            foreach ($prices as $title => $value) {
391
                $priceInfoItem = array(
392
                    'name' => $title,
393
                    'priceCurrency' => 'EUR',
394
                    'price' => $value,
395
                );
396
397
                $priceInfoItem['category'] = 'tariff';
398
399
                if ($priceInfoItem['name'] === 'Basistarief') {
400
                    $priceInfoItem['category'] = 'base';
401
                }
402
403
                $priceInfo[] = $priceInfoItem;
404
            }
405
406
            if (!empty($priceInfo)) {
407
                $jsonLD->priceInfo = $priceInfo;
408
            }
409
        }
410
    }
411
412
    /**
413
     * @param \CultureFeed_Cdb_Item_Event $event
414
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
415
     * @param \stdClass $jsonLD
416
     */
417
    private function importBookingInfo(
418
        \CultureFeed_Cdb_Item_Event $event,
419
        \CultureFeed_Cdb_Data_EventDetail $detail,
420
        $jsonLD
421
    ) {
422
        $bookingInfo = array();
423
424
        $price = $detail->getPrice();
425
        if ($price) {
426
            if ($price->getDescription()) {
427
                $bookingInfo['description'] = $price->getDescription();
428
            }
429
            if ($price->getTitle()) {
430
                $bookingInfo['name'] = $price->getTitle();
431
            }
432
            if ($price->getValue() !== null) {
433
                $bookingInfo['priceCurrency'] = 'EUR';
434
                $bookingInfo['price'] = floatval($price->getValue());
435
            }
436
            if ($bookingPeriod = $event->getBookingPeriod()) {
437
                $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom());
438
                $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill());
439
440
                $bookingInfo['availabilityStarts'] = $startDate->format('c');
441
                $bookingInfo['availabilityEnds'] = $endDate->format('c');
442
            }
443
        }
444
445
        // Add reservation contact data.
446
        $contactInfo = $event->getContactInfo();
447
        if ($contactInfo) {
448
            /** @var \CultureFeed_Cdb_Data_Url[] $urls */
449
            $urls = $contactInfo->getUrls();
450
            foreach ($urls as $url) {
451
                if ($url->isForReservations()) {
452
                    $bookingInfo['url'] = $url->getUrl();
453
                    break;
454
                }
455
            }
456
457
            if (array_key_exists('url', $bookingInfo)) {
458
                $bookingInfo['urlLabel'] = 'Reserveer plaatsen';
459
            }
460
461
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones */
462
            $phones = $contactInfo->getPhones();
463
            foreach ($phones as $phone) {
464
                if ($phone->isForReservations()) {
465
                    $bookingInfo['phone'] = $phone->getNumber();
466
                    break;
467
                }
468
            }
469
470
            foreach ($contactInfo->getMails() as $mail) {
471
                if ($mail->isForReservations()) {
472
                    $bookingInfo['email'] = $mail->getMailAddress();
473
                    break;
474
                }
475
            }
476
        }
477
478
        if (!empty($bookingInfo)) {
479
            $jsonLD->bookingInfo = $bookingInfo;
480
        }
481
    }
482
483
    /**
484
     * @param \CultureFeed_Cdb_Item_Event $event
485
     * @param \stdClass $jsonLD
486
     */
487
    private function importContactPoint(
488
        \CultureFeed_Cdb_Item_Event $event,
489
        \stdClass $jsonLD
490
    ) {
491
        $contactInfo = $event->getContactInfo();
492
493
        $notForReservations = function ($item) {
494
            /** @var \CultureFeed_Cdb_Data_Url|\CultureFeed_Cdb_Data_Phone|\CultureFeed_Cdb_Data_Mail $item */
495
            return !$item->isForReservations();
496
        };
497
498
        if ($contactInfo) {
499
            $contactPoint = array();
500
501
            $emails = array_filter($contactInfo->getMails(), $notForReservations);
502
503 View Code Duplication
            if (!empty($emails)) {
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...
504
                $contactPoint['email'] = array_map(
505
                    function (\CultureFeed_Cdb_Data_Mail $email) {
506
                        return $email->getMailAddress();
507
                    },
508
                    $emails
509
                );
510
                $contactPoint['email'] = array_values($contactPoint['email']);
511
            }
512
513
            $phones = array_filter($contactInfo->getPhones(), $notForReservations);
514
515 View Code Duplication
            if (!empty($phones)) {
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...
516
                $contactPoint['phone'] = array_map(
517
                    function (\CultureFeed_Cdb_Data_phone $phone) {
518
                        return $phone->getNumber();
519
                    },
520
                    $phones
521
                );
522
                $contactPoint['phone'] = array_values($contactPoint['phone']);
523
            }
524
525
            $urls = array_filter($contactInfo->getUrls(), $notForReservations);
526
527 View Code Duplication
            if (!empty($urls)) {
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
                $contactPoint['url'] = array_map(
529
                    function (\CultureFeed_Cdb_Data_Url $url) {
530
                        return $url->getUrl();
531
                    },
532
                    $urls
533
                );
534
                $contactPoint['url'] = array_values($contactPoint['url']);
535
            }
536
537
            array_filter($contactPoint);
538
            if (!empty($contactPoint)) {
539
                $jsonLD->contactPoint = $contactPoint;
540
            }
541
        }
542
    }
543
544
    /**
545
     * @param \CultureFeed_Cdb_Item_Event $event
546
     * @param \stdClass $jsonLD
547
     */
548
    private function importTerms(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
549
    {
550
        $themeBlacklist = [
551
            'Thema onbepaald',
552
            'Meerder kunstvormen',
553
            'Meerdere filmgenres'
554
        ];
555
        $categories = array();
556 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...
557
            /* @var \Culturefeed_Cdb_Data_Category $category */
558
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
559
                $categories[] = array(
560
                    'label' => $category->getName(),
561
                    'domain' => $category->getType(),
562
                    'id' => $category->getId(),
563
                );
564
            }
565
        }
566
        $jsonLD->terms = $categories;
567
    }
568
569
    /**
570
     * @param \CultureFeed_Cdb_Item_Event $event
571
     * @param \stdClass $jsonLD
572
     */
573
    private function importTypicalAgeRange(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
574
    {
575
        $ageFrom = $event->getAgeFrom();
576
577
        if (isset($ageFrom) && is_int($ageFrom)) {
578
            if ($ageFrom <= 12) {
579
                $jsonLD->typicalAgeRange = "{$ageFrom}-12";
580
            } else if ($ageFrom <= 18) {
581
                $jsonLD->typicalAgeRange = "{$ageFrom}-18";
582
            } else if ($ageFrom <= 99) {
583
                $jsonLD->typicalAgeRange = "{$ageFrom}-99";
584
            } else {
585
                $jsonLD->typicalAgeRange = '99-99';
586
            }
587
        }
588
    }
589
590
    /**
591
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
592
     * @param \stdClass $jsonLD
593
     */
594
    private function importPerformers(\CultureFeed_Cdb_Data_EventDetail $detail, $jsonLD)
595
    {
596
        /** @var \CultureFeed_Cdb_Data_Performer $performer */
597
        $performers = $detail->getPerformers();
598
        if ($performers) {
599
            foreach ($performers as $performer) {
600
                if ($performer->getLabel()) {
601
                    $performerData = new \stdClass();
602
                    $performerData->name = $performer->getLabel();
603
                    $jsonLD->performer[] = $performerData;
604
                }
605
            }
606
        }
607
    }
608
609
    /**
610
     * @param \CultureFeed_Cdb_Item_Event $event
611
     * @param \stdClass $jsonLD
612
     */
613
    private function importLanguages(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
614
    {
615
        /** @var \CultureFeed_Cdb_Data_Language $udb2Language */
616
        $languages = $event->getLanguages();
617
        if ($languages) {
618
            $jsonLD->language = [];
619
            foreach ($languages as $udb2Language) {
620
                $jsonLD->language[] = $udb2Language->getLanguage();
621
            }
622
            $jsonLD->language = array_unique($jsonLD->language);
623
        }
624
    }
625
626
    /**
627
     * @param \CultureFeed_Cdb_Item_Event $event
628
     * @param \stdClass $jsonLD
629
     */
630
    private function importSeeAlso(
631
        \CultureFeed_Cdb_Item_Event $event,
632
        \stdClass $jsonLD
633
    ) {
634
        if (!property_exists($jsonLD, 'seeAlso')) {
635
            $jsonLD->seeAlso = [];
636
        }
637
638
        // Add contact info url, if it's not for reservations.
639
        if ($contactInfo = $event->getContactInfo()) {
640
            /** @var \CultureFeed_Cdb_Data_Url[] $contactUrls */
641
            $contactUrls = $contactInfo->getUrls();
642
            if (is_array($contactUrls) && count($contactUrls) > 0) {
643
                foreach ($contactUrls as $contactUrl) {
644
                    if (!$contactUrl->isForReservations()) {
645
                        $jsonLD->seeAlso[] = $contactUrl->getUrl();
646
                    }
647
                }
648
            }
649
        }
650
    }
651
652
    /**
653
     * @param \CultureFeed_Cdb_Item_Event $event
654
     * @param SluggerInterface $slugger
655
     * @param \stdClass $jsonLD
656
     */
657
    private function importUitInVlaanderenReference(
658
        \CultureFeed_Cdb_Item_Event $event,
659
        SluggerInterface $slugger,
660
        $jsonLD
661
    ) {
662
663
        // Some events seem to not have a Dutch name, even though this is
664
        // required. If there's no Dutch name, we just leave the slug empty as
665
        // that seems to be the behaviour on http://m.uitinvlaanderen.be
666
        if (isset($jsonLD->name['nl'])) {
667
            $name = $jsonLD->name['nl'];
668
            $slug = $slugger->slug($name);
669
        } else {
670
            $slug = '';
671
        }
672
673
        $reference = 'http://www.uitinvlaanderen.be/agenda/e/' . $slug . '/' . $event->getCdbId();
674
675
676
        if (!property_exists($jsonLD, 'sameAs')) {
677
            $jsonLD->sameAs = [];
678
        }
679
680
        if (!in_array($reference, $jsonLD->sameAs)) {
681
            array_push($jsonLD->sameAs, $reference);
682
        }
683
    }
684
685
    /**
686
     * @param string $longDescription
687
     * @param string $shortDescription
688
     * @return bool
689
     */
690
    private function longDescriptionStartsWithShortDescription(
691
        $longDescription,
692
        $shortDescription
693
    ) {
694
        $longDescription = strip_tags(html_entity_decode($longDescription));
695
696
        return 0 === strncmp($longDescription, $shortDescription, mb_strlen($shortDescription));
697
    }
698
}
699