Completed
Pull Request — master (#240)
by Kristof
08:25
created

CdbXMLImporter::importDescription()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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