Completed
Push — master ( 97eb75...afe27f )
by Kristof
09:46 queued 04:54
created

CdbXMLImporter::importTypicalAgeRange()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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