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

CdbXMLImporter::importDescription()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 8.439
c 0
b 0
f 0
cc 6
eloc 21
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
                $includeShortDescription =
220
                    !$this->longDescriptionStartsWithShortDescription(
221
                        $longDescription,
222
                        $shortDescription
223
                    );
224
225
                if ($includeShortDescription) {
226
                    $descriptions[] = $shortDescription;
227
                }
228
            }
229
        }
230
231
        if ($longDescription) {
232
            $descriptions[] = $longDescription;
233
        }
234
235
        $description = implode("\n\n", $descriptions);
236
237
        $jsonLD->description[$language] = $description;
238
    }
239
240
    /**
241
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
242
     * @param \stdClass $jsonLD
243
     *
244
     * This is based on code found in the culturefeed theme.
245
     * @see https://github.com/cultuurnet/culturefeed/blob/master/culturefeed_agenda/theme/theme.inc#L266-L284
246
     */
247 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...
248
    {
249
        $mainPicture = null;
250
251
        // first check if there is a media file that is main and has the PHOTO media type
252
        $photos = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO);
253
        foreach ($photos as $photo) {
254
            if ($photo->isMain()) {
255
                $mainPicture = $photo;
256
            }
257
        }
258
259
        // the IMAGEWEB media type is deprecated but can still be used as a main image if there is no PHOTO
260
        if (empty($mainPicture)) {
261
            $images = $detail->getMedia()->byMediaType(CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB);
262
            foreach ($images as $image) {
263
                if ($image->isMain()) {
264
                    $mainPicture = $image;
265
                }
266
            }
267
        }
268
269
        // if there is no explicit main image we just use the oldest picture of any type
270
        if (empty($mainPicture)) {
271
            $pictures = $detail->getMedia()->byMediaTypes(
272
                [
273
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_PHOTO,
274
                    CultureFeed_Cdb_Data_File::MEDIA_TYPE_IMAGEWEB
275
                ]
276
            );
277
278
            $pictures->rewind();
279
            $mainPicture = count($pictures) > 0 ? $pictures->current() : null;
280
        }
281
282
        if ($mainPicture) {
283
            $jsonLD->image = $mainPicture->getHLink();
284
        }
285
    }
286
287
    /**
288
     * @param \CultureFeed_Cdb_Item_Event $event
289
     * @param PlaceServiceInterface $placeManager
290
     * @param \stdClass $jsonLD
291
     */
292
    private function importLocation(\CultureFeed_Cdb_Item_Event $event, PlaceServiceInterface $placeManager, $jsonLD)
293
    {
294
        $location = array();
295
        $location['@type'] = 'Place';
296
297
        $location_id = $this->cdbIdExtractor->getRelatedPlaceCdbId($event);
298
299
        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...
300
            $location += (array)$placeManager->placeJSONLD($location_id);
301
        } else {
302
            $location_cdb = $event->getLocation();
303
            $location['name']['nl'] = $location_cdb->getLabel();
304
            $address = $location_cdb->getAddress()->getPhysicalAddress();
305
            if ($address) {
306
                $location['address'] = array(
307
                    'addressCountry' => $address->getCountry(),
308
                    'addressLocality' => $address->getCity(),
309
                    'postalCode' => $address->getZip(),
310
                    'streetAddress' =>
311
                        $address->getStreet() . ' ' . $address->getHouseNumber(
312
                        ),
313
                );
314
            }
315
        }
316
        $jsonLD->location = $location;
317
    }
318
319
    /**
320
     * @param \CultureFeed_Cdb_Item_Event $event
321
     * @param OrganizerServiceInterface $organizerManager
322
     * @param \stdClass $jsonLD
323
     */
324
    private function importOrganizer(
325
        \CultureFeed_Cdb_Item_Event $event,
326
        OrganizerServiceInterface $organizerManager,
327
        $jsonLD
328
    ) {
329
        $organizer = null;
330
        $organizer_id = $this->cdbIdExtractor->getRelatedOrganizerCdbId($event);
331
        $organizer_cdb = $event->getOrganiser();
332
        $contact_info_cdb = $event->getContactInfo();
333
334
        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...
335
            $organizer = (array)$organizerManager->organizerJSONLD($organizer_id);
336
        } elseif ($organizer_cdb && $contact_info_cdb) {
337
            $organizer = array();
338
            $organizer['name'] = $organizer_cdb->getLabel();
339
340
            $emails_cdb = $contact_info_cdb->getMails();
341
            if (count($emails_cdb) > 0) {
342
                $organizer['email'] = array();
343
                foreach ($emails_cdb as $email) {
344
                    $organizer['email'][] = $email->getMailAddress();
345
                }
346
            }
347
348
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones_cdb */
349
            $phones_cdb = $contact_info_cdb->getPhones();
350
            if (count($phones_cdb) > 0) {
351
                $organizer['phone'] = array();
352
                foreach ($phones_cdb as $phone) {
353
                    $organizer['phone'][] = $phone->getNumber();
354
                }
355
            }
356
        }
357
358
        if (!is_null($organizer)) {
359
            $organizer['@type'] = 'Organizer';
360
            $jsonLD->organizer = $organizer;
361
        }
362
    }
363
364
    /**
365
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
366
     * @param \stdClass $jsonLD
367
     */
368
    private function importPriceInfo(
369
        \CultureFeed_Cdb_Data_EventDetail $detail,
370
        $jsonLD
371
    ) {
372
        $prices = array();
373
374
        $price = $detail->getPrice();
375
376
        if ($price) {
377
            $description = $price->getDescription();
378
379
            if ($description) {
380
                $prices = $this->priceDescriptionParser->parse($description);
381
            }
382
383
            // If price description was not interpretable, fall back to
384
            // price title and value.
385
            if (empty($prices) && $price->getValue() !== null) {
386
                $prices['Basistarief'] = floatval($price->getValue());
387
            }
388
        }
389
390
        if (!empty($prices)) {
391
            $priceInfo = array();
392
393
            /** @var \CultureFeed_Cdb_Data_Price $price */
394
            foreach ($prices as $title => $value) {
395
                $priceInfoItem = array(
396
                    'name' => $title,
397
                    'priceCurrency' => 'EUR',
398
                    'price' => $value,
399
                );
400
401
                $priceInfoItem['category'] = 'tariff';
402
403
                if ($priceInfoItem['name'] === 'Basistarief') {
404
                    $priceInfoItem['category'] = 'base';
405
                }
406
407
                $priceInfo[] = $priceInfoItem;
408
            }
409
410
            if (!empty($priceInfo)) {
411
                $jsonLD->priceInfo = $priceInfo;
412
            }
413
        }
414
    }
415
416
    /**
417
     * @param \CultureFeed_Cdb_Item_Event $event
418
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
419
     * @param \stdClass $jsonLD
420
     */
421
    private function importBookingInfo(
422
        \CultureFeed_Cdb_Item_Event $event,
423
        \CultureFeed_Cdb_Data_EventDetail $detail,
424
        $jsonLD
425
    ) {
426
        $bookingInfo = array();
427
428
        $price = $detail->getPrice();
429
        if ($price) {
430
            if ($price->getDescription()) {
431
                $bookingInfo['description'] = $price->getDescription();
432
            }
433
            if ($price->getTitle()) {
434
                $bookingInfo['name'] = $price->getTitle();
435
            }
436
            if ($price->getValue() !== null) {
437
                $bookingInfo['priceCurrency'] = 'EUR';
438
                $bookingInfo['price'] = floatval($price->getValue());
439
            }
440
            if ($bookingPeriod = $event->getBookingPeriod()) {
441
                $startDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateFrom());
442
                $endDate = $this->dateFromUdb2UnixTime($bookingPeriod->getDateTill());
443
444
                $bookingInfo['availabilityStarts'] = $startDate->format('c');
445
                $bookingInfo['availabilityEnds'] = $endDate->format('c');
446
            }
447
        }
448
449
        // Add reservation contact data.
450
        $contactInfo = $event->getContactInfo();
451
        if ($contactInfo) {
452
            /** @var \CultureFeed_Cdb_Data_Url[] $urls */
453
            $urls = $contactInfo->getUrls();
454
            foreach ($urls as $url) {
455
                if ($url->isForReservations()) {
456
                    $bookingInfo['url'] = $url->getUrl();
457
                    break;
458
                }
459
            }
460
461
            if (array_key_exists('url', $bookingInfo)) {
462
                $bookingInfo['urlLabel'] = 'Reserveer plaatsen';
463
            }
464
465
            /** @var \CultureFeed_Cdb_Data_Phone[] $phones */
466
            $phones = $contactInfo->getPhones();
467
            foreach ($phones as $phone) {
468
                if ($phone->isForReservations()) {
469
                    $bookingInfo['phone'] = $phone->getNumber();
470
                    break;
471
                }
472
            }
473
474
            foreach ($contactInfo->getMails() as $mail) {
475
                if ($mail->isForReservations()) {
476
                    $bookingInfo['email'] = $mail->getMailAddress();
477
                    break;
478
                }
479
            }
480
        }
481
482
        if (!empty($bookingInfo)) {
483
            $jsonLD->bookingInfo = $bookingInfo;
484
        }
485
    }
486
487
    /**
488
     * @param \CultureFeed_Cdb_Item_Event $event
489
     * @param \stdClass $jsonLD
490
     */
491
    private function importContactPoint(
492
        \CultureFeed_Cdb_Item_Event $event,
493
        \stdClass $jsonLD
494
    ) {
495
        $contactInfo = $event->getContactInfo();
496
497
        $notForReservations = function ($item) {
498
            /** @var \CultureFeed_Cdb_Data_Url|\CultureFeed_Cdb_Data_Phone|\CultureFeed_Cdb_Data_Mail $item */
499
            return !$item->isForReservations();
500
        };
501
502
        if ($contactInfo) {
503
            $contactPoint = array();
504
505
            $emails = array_filter($contactInfo->getMails(), $notForReservations);
506
507 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...
508
                $contactPoint['email'] = array_map(
509
                    function (\CultureFeed_Cdb_Data_Mail $email) {
510
                        return $email->getMailAddress();
511
                    },
512
                    $emails
513
                );
514
                $contactPoint['email'] = array_values($contactPoint['email']);
515
            }
516
517
            $phones = array_filter($contactInfo->getPhones(), $notForReservations);
518
519 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...
520
                $contactPoint['phone'] = array_map(
521
                    function (\CultureFeed_Cdb_Data_phone $phone) {
522
                        return $phone->getNumber();
523
                    },
524
                    $phones
525
                );
526
                $contactPoint['phone'] = array_values($contactPoint['phone']);
527
            }
528
529
            $urls = array_filter($contactInfo->getUrls(), $notForReservations);
530
531 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...
532
                $contactPoint['url'] = array_map(
533
                    function (\CultureFeed_Cdb_Data_Url $url) {
534
                        return $url->getUrl();
535
                    },
536
                    $urls
537
                );
538
                $contactPoint['url'] = array_values($contactPoint['url']);
539
            }
540
541
            array_filter($contactPoint);
542
            if (!empty($contactPoint)) {
543
                $jsonLD->contactPoint = $contactPoint;
544
            }
545
        }
546
    }
547
548
    /**
549
     * @param \CultureFeed_Cdb_Item_Event $event
550
     * @param \stdClass $jsonLD
551
     */
552
    private function importTerms(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
553
    {
554
        $themeBlacklist = [
555
            'Thema onbepaald',
556
            'Meerder kunstvormen',
557
            'Meerdere filmgenres'
558
        ];
559
        $categories = array();
560 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...
561
            /* @var \Culturefeed_Cdb_Data_Category $category */
562
            if ($category && !in_array($category->getName(), $themeBlacklist)) {
563
                $categories[] = array(
564
                    'label' => $category->getName(),
565
                    'domain' => $category->getType(),
566
                    'id' => $category->getId(),
567
                );
568
            }
569
        }
570
        $jsonLD->terms = $categories;
571
    }
572
573
    /**
574
     * @param \CultureFeed_Cdb_Item_Event $event
575
     * @param \stdClass $jsonLD
576
     */
577
    private function importTypicalAgeRange(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
578
    {
579
        $ageFrom = $event->getAgeFrom();
580
581
        if (isset($ageFrom) && is_int($ageFrom)) {
582
            if ($ageFrom <= 12) {
583
                $jsonLD->typicalAgeRange = "{$ageFrom}-12";
584
            } else if ($ageFrom <= 18) {
585
                $jsonLD->typicalAgeRange = "{$ageFrom}-18";
586
            } else if ($ageFrom <= 99) {
587
                $jsonLD->typicalAgeRange = "{$ageFrom}-99";
588
            } else {
589
                $jsonLD->typicalAgeRange = '99-99';
590
            }
591
        }
592
    }
593
594
    /**
595
     * @param \CultureFeed_Cdb_Data_EventDetail $detail
596
     * @param \stdClass $jsonLD
597
     */
598
    private function importPerformers(\CultureFeed_Cdb_Data_EventDetail $detail, $jsonLD)
599
    {
600
        /** @var \CultureFeed_Cdb_Data_Performer $performer */
601
        $performers = $detail->getPerformers();
602
        if ($performers) {
603
            foreach ($performers as $performer) {
604
                if ($performer->getLabel()) {
605
                    $performerData = new \stdClass();
606
                    $performerData->name = $performer->getLabel();
607
                    $jsonLD->performer[] = $performerData;
608
                }
609
            }
610
        }
611
    }
612
613
    /**
614
     * @param \CultureFeed_Cdb_Item_Event $event
615
     * @param \stdClass $jsonLD
616
     */
617
    private function importLanguages(\CultureFeed_Cdb_Item_Event $event, $jsonLD)
618
    {
619
        /** @var \CultureFeed_Cdb_Data_Language $udb2Language */
620
        $languages = $event->getLanguages();
621
        if ($languages) {
622
            $jsonLD->language = [];
623
            foreach ($languages as $udb2Language) {
624
                $jsonLD->language[] = $udb2Language->getLanguage();
625
            }
626
            $jsonLD->language = array_unique($jsonLD->language);
627
        }
628
    }
629
630
    /**
631
     * @param \CultureFeed_Cdb_Item_Event $event
632
     * @param \stdClass $jsonLD
633
     */
634
    private function importSeeAlso(
635
        \CultureFeed_Cdb_Item_Event $event,
636
        \stdClass $jsonLD
637
    ) {
638
        if (!property_exists($jsonLD, 'seeAlso')) {
639
            $jsonLD->seeAlso = [];
640
        }
641
642
        // Add contact info url, if it's not for reservations.
643
        if ($contactInfo = $event->getContactInfo()) {
644
            /** @var \CultureFeed_Cdb_Data_Url[] $contactUrls */
645
            $contactUrls = $contactInfo->getUrls();
646
            if (is_array($contactUrls) && count($contactUrls) > 0) {
647
                foreach ($contactUrls as $contactUrl) {
648
                    if (!$contactUrl->isForReservations()) {
649
                        $jsonLD->seeAlso[] = $contactUrl->getUrl();
650
                    }
651
                }
652
            }
653
        }
654
    }
655
656
    /**
657
     * @param \CultureFeed_Cdb_Item_Event $event
658
     * @param SluggerInterface $slugger
659
     * @param \stdClass $jsonLD
660
     */
661
    private function importUitInVlaanderenReference(
662
        \CultureFeed_Cdb_Item_Event $event,
663
        SluggerInterface $slugger,
664
        $jsonLD
665
    ) {
666
667
        // Some events seem to not have a Dutch name, even though this is
668
        // required. If there's no Dutch name, we just leave the slug empty as
669
        // that seems to be the behaviour on http://m.uitinvlaanderen.be
670
        if (isset($jsonLD->name['nl'])) {
671
            $name = $jsonLD->name['nl'];
672
            $slug = $slugger->slug($name);
673
        } else {
674
            $slug = '';
675
        }
676
677
        $reference = 'http://www.uitinvlaanderen.be/agenda/e/' . $slug . '/' . $event->getCdbId();
678
679
680
        if (!property_exists($jsonLD, 'sameAs')) {
681
            $jsonLD->sameAs = [];
682
        }
683
684
        if (!in_array($reference, $jsonLD->sameAs)) {
685
            array_push($jsonLD->sameAs, $reference);
686
        }
687
    }
688
689
    /**
690
     * @param string $longDescription
691
     * @param string $shortDescription
692
     * @return bool
693
     */
694
    private function longDescriptionStartsWithShortDescription(
695
        $longDescription,
696
        $shortDescription
697
    ) {
698
        $longDescription = strip_tags(html_entity_decode($longDescription));
699
700
        return 0 === strncmp($longDescription, $shortDescription, mb_strlen($shortDescription));
701
    }
702
}
703